Spring Data JPA学习笔记,记录学习到的知识

Spring Data JPA常用注解:

@Entity
作用:指定当前类是实体类。
@Table
作用:指定实体类和表之间的对应关系。
属性:
name:指定数据库表的名称
@Id
作用:指定当前字段是主键。
@GeneratedValue
作用:指定主键的生成方式。。
属性:
strategy :指定主键生成策略。
@Column
作用:指定实体类属性和数据库表之间的对应关系
属性:
name:指定数据库表的列名称。
unique:是否唯一
nullable:是否可以为空
inserttable:是否可以插入
updateable:是否可以更新
columnDefinition: 定义建表时创建此列的DDL
secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字搭建开发环境[重点]

Spring Data JPA工具类:

package cn.itcast.dao;

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

public final class JPAUtil {
	// JPA的实体管理器工厂:相当于Hibernate的SessionFactory
	private static EntityManagerFactory em;
	// 使用静态代码块赋值
	static {
		// 注意:该方法参数必须和persistence.xml中persistence-unit标签name属性取值一致
		em = Persistence.createEntityManagerFactory("myPersistUnit");
	}

	/**
	 * 使用管理器工厂生产一个管理器对象
	 * 
	 * @return
	 */
	public static EntityManager getEntityManager() {
		return em.createEntityManager();
	}
}

保存方法:

/**
	 * 保存一个实体
	 */
	@Test
	public void testAdd() {
		// 定义对象
		Customer c = new Customer();
		c.setCustName("传智学院");
		c.setCustLevel("VIP客户");
		c.setCustSource("网络");
		c.setCustIndustry("IT教育");
		c.setCustAddress("昌平区北七家镇");
		c.setCustPhone("010-84389340");
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			em.persist(c);
			// 提交事务
			tx.commit();
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

修改方法:

 @Test
    public void testMerge(){  
        //定义对象
        EntityManager em=null;  
        EntityTransaction tx=null;  
        try{  
          	//获取实体管理对象
          	em=JPAUtil.getEntityManager();
          	//获取事务对象
          	tx=em.getTransaction();
          	//开启事务
          	tx.begin();
          	//执行操作
          	Customer c1 = em.find(Customer.class, 6L);
          	c1.setCustName("江苏传智学院");
         	em.clear();//把c1对象从缓存中清除出去
          	em.merge(c1);
          	//提交事务
          	tx.commit(); 
        }catch(Exception e){
          	//回滚事务
          	tx.rollback();
          	e.printStackTrace();  
        }finally{  
        	//释放资源
        	em.close();  
        }    
    }

删除方法:

/**
	 * 删除
	 */
	@Test
	public void testRemove() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.find(Customer.class, 6L);
			em.remove(c1);
			// 提交事务
			tx.commit();
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

根据ID查询:

/**
	 * 查询一个: 使用立即加载的策略
	 */
	@Test
	public void testGetOne() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.find(Customer.class, 1L);
			// 提交事务
			tx.commit();
			System.out.println(c1); // 输出查询对象
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

	// 查询实体的缓存问题
	@Test
	public void testGetOne() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.find(Customer.class, 1L);
			Customer c2 = em.find(Customer.class, 1L);
			System.out.println(c1 == c2);// 输出结果是true,EntityManager也有缓存
			// 提交事务
			tx.commit();
			System.out.println(c1);
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

	// 延迟加载策略的方法:
	/**
	 * 查询一个: 使用延迟加载策略
	 */
	@Test
	public void testLoadOne() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.getReference(Customer.class, 1L);
			// 提交事务
			tx.commit();
			System.out.println(c1);
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_45593609/article/details/105537710