JPA增删改查的简单实现

工具IDEA

1.依赖引入:


            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
            runtime
        

2.

在resource下创建文件/META-INF/persistence.xml

PS:必须是在META-INF文件夹下

3.persistence.xml配置



    
        
        com.demo.entity.User
        
            
            
            
            
            
            
            
            
        

    

 

4.公共类EntityManagerUtils

package com.demo.utils;


import org.hibernate.Transaction;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class EntityManagerUtils {
    public static EntityManagerFactory entityManagerFactory=null;
    public static EntityManager entityManager=null;
    public static EntityTransaction transaction;

//    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JPA");
//    EntityManager entityManager =entityManagerFactory.createEntityManager();
//    EntityTransaction transactiond =entityManager.getTransaction();
//    transaction.
    public static void  begin(){
        if (entityManagerFactory==null){
            //“JPA”是persistence.xml的标签的name属性
            entityManagerFactory=Persistence.createEntityManagerFactory("JPA");
        }
        if (entityManager==null){
            entityManager=entityManagerFactory.createEntityManager();
        }
        if (transaction==null){
            transaction= entityManager.getTransaction();
        }
        transaction.begin();
    }

    public static void close(){
        entityManager.close();
        entityManagerFactory.close();
        //实例是静态的,赋值后不会随着factory的关闭而变化,所以设置为null
        entityManagerFactory=null;
        entityManager=null;
        transaction=null;
    }


}

5.实现增删改查的controller

@RequestMapping(value = "save")
    public void save(){
        EntityManagerUtils.begin();
        User user=new User(1,"dy",21);
        EntityManagerUtils.entityManager.persist(user);
        EntityManagerUtils.transaction.commit();
        EntityManagerUtils.close();
    }

    @RequestMapping(value = "query")
    public void query(){
        EntityManagerUtils.begin();
        User user=EntityManagerUtils.entityManager.find(User.class,1);
        EntityManagerUtils.transaction.commit();
        EntityManagerUtils.close();
        System.out.println(user);
    }

    @RequestMapping(value = "update")
    public void update(){
        EntityManagerUtils.begin();
       User user= EntityManagerUtils.entityManager.find(User.class,1);
       user.setName("updateName");
       EntityManagerUtils.transaction.commit();
       EntityManagerUtils.close();
    }

    @RequestMapping(value = "remove")
    public void remove(){
        EntityManagerUtils.begin();
        User user=EntityManagerUtils.entityManager.find(User.class,1);
        EntityManagerUtils.entityManager.remove(user);
        EntityManagerUtils.transaction.commit();
        EntityManagerUtils.close();
    }

 

 

 

 

 

 

 

你可能感兴趣的:(JPA)