jpa入门
1.与表对应的实体类
@Entity //声明实体类 @Table(name = "cst_customer") //实体类与表建立映射关系 public class Customer { @Id //主键 @GeneratedValue(strategy = GenerationType.IDENTITY)//主键自增 @Column(name = "cust_id") //字段名与属性名 private Long custId; @Column(name = "cust_name") private String custName; @Column(name = "cust_source") private String custSource; @Column(name = "cust_industry") private String custIndustry; @Column(name ="cust_level") private String custLevel; @Column(name = "cust_address") private String custAddress; @Column(name = "cust_phone") private String custPhone;
//getter setter方法
2.配置文件在resources下META-INF下的persistence.xml中(固定位置名称)
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <!--resource_local本地事务管理--> <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="root"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <!--update有表就不创建表了,没有就创建表 create无论有没有都创建新表,只会存最新的数据--> <property name="hibernate.hb2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
3.工具类
/** * jpa工具类 */ public class JpaUtil { private static EntityManagerFactory factory; static{ //加载配置文件,创建出工厂对象 factory = Persistence.createEntityManagerFactory("myJpa"); } public static EntityManager getEntityManager(){ return factory.createEntityManager(); } }
4.测试
@Test public void testPersist(){ Customer customer = new Customer(); customer.setCustName("传智播客"); customer.setCustAddress("江苏"); customer.setCustLevel("high"); customer.setCustIndustry("IT教育"); EntityManager em =null; EntityTransaction tx=null; try { em=JpaUtil.getEntityManager(); tx=em.getTransaction(); tx.begin(); //开启 em.persist(customer); tx.commit();//提交 } catch (Exception e) { e.printStackTrace(); tx.rollback();//回滚 } finally { em.close();//释放资源 } } @Test public void find(){ //立即加载 EntityManager em =null; EntityTransaction tx=null; try { em=JpaUtil.getEntityManager(); tx=em.getTransaction(); tx.begin(); Customer customer = em.find(Customer.class, 2L); Customer customer1 = em.find(Customer.class, 2L); System.out.println(customer==customer1); //true EntityManager有缓存存在 tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); } finally { em.close(); } } @Test public void getReference(){//延迟加载 EntityManager em =null; EntityTransaction tx=null; try { em=JpaUtil.getEntityManager(); tx=em.getTransaction(); tx.begin(); Customer customer = em.getReference(Customer.class, 3L); Customer customer1 = em.getReference(Customer.class, 3L); tx.commit(); System.out.println(customer==customer1);//true 有缓存 System.out.println(customer); } catch (Exception e) { e.printStackTrace(); tx.rollback(); } finally { em.close(); } } @Test public void remove(){ EntityManager em =null; EntityTransaction tx=null; try { em=JpaUtil.getEntityManager(); tx=em.getTransaction(); tx.begin(); Customer customer = em.find(Customer.class, 1L); em.remove(customer); tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); } finally { em.close(); } } @Test public void merge(){ EntityManager em =null; EntityTransaction tx=null; try { em=JpaUtil.getEntityManager(); tx=em.getTransaction(); tx.begin(); Customer c1 = em.find(Customer.class, 3L); c1.setCustName("黑马来了"); em.merge(c1); tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); } finally { em.close(); } }
find与getReference区别
1.find 是立即加载,getReference是延迟加载
2.find返回的是真实的实体对象,getReference返回的是代理对象
3.如果查询对象不存在,find返回null,getReference报错