![15e3a22d210a37172335e33562dda7f2.png](https://img-blog.csdnimg.cn/img_convert/15e3a22d210a37172335e33562dda7f2.png)
引言
在hibernate中,有三种方式来实现crud。
- hibernate api+ hbm.xml(xml配置文件)
- hibernate api+Annotation Mappings(注解)
- Java Persistence API (JPA)
熟悉hibernate的同学都知道,前两种是用的比较多的。jpa呢在spring data 中也用过,但是原生的jpa这套api本人还没有用过。所以今天趁机来玩一玩,应该对学spring data jpa有些帮助。
![2747ab01e5962c82ec33085797f148d5.png](https://img-blog.csdnimg.cn/img_convert/2747ab01e5962c82ec33085797f148d5.png)
什么是JPA
JPA是Java Persistence API的简称,早期有很多orm框架,这些框架各自为王,自己都有自己标准,因此sun为了统一orm框架,因此提出了这套规范,很明显hibernate就实现了这套规范。
环境搭建
下载jar
http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download
导入jar包
![a81ab2df115c9b90d306c78965193aae.png](https://img-blog.csdnimg.cn/img_convert/a81ab2df115c9b90d306c78965193aae.png)
特别注意的是 hibernate-entitymanager-5.0.7.Final.jar,一定要导。在jpa文件下面。
新建persistence.xml
这里需要注意的,此文件必须放在META-INF包下,比如:
![9bf6019e8c5f5f25647246d3a725d0b4.png](https://img-blog.csdnimg.cn/img_convert/9bf6019e8c5f5f25647246d3a725d0b4.png)
至于为什么嘛,乌龟的屁股,规定啊。
编写persistence.xml
org.hibernate.jpa.HibernatePersistenceProvidercom.javazzh.hibernate.demo1.CstCustomer
![7d3e65ade91b305c489f649c1b6b9102.png](https://img-blog.csdnimg.cn/img_convert/7d3e65ade91b305c489f649c1b6b9102.png)
![57f732e3060a1f2c177b5adc01177b70.png](https://img-blog.csdnimg.cn/img_convert/57f732e3060a1f2c177b5adc01177b70.png)
创建持久化类
![dcbe0037afea81c8185d5aceb2201bdb.png](https://img-blog.csdnimg.cn/img_convert/dcbe0037afea81c8185d5aceb2201bdb.png)
import javax.persistence.*;@Entity@Table(name="cst_customer")public class Customer { @Id @Column(name = "cust_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long custId; @Column(name = "cust_name") private String custName; @Column(name = "cust_user_id") private Long custUserId; @Column(name = "cust_create_Id") private Long custCreateId; @Column(name = "cust_source") private String custSource; @Column(name = "cust_industry") private String custIndustry; @Column(name = "cust_level") private String custLevel; @Column(name = "cust_linkman") private String custLinkman; @Column(name = "cust_phone") private String custPhone; @Column(name = "cust_mobile") private String custMobile; 省略getter/setter...}
api使用
保存
![f4bab02bc90a8e28e036ac10be9bbfdf.png](https://img-blog.csdnimg.cn/img_convert/f4bab02bc90a8e28e036ac10be9bbfdf.png)
查询单个对象
![ad91ac727b983a3659bca2c3f2f63b52.png](https://img-blog.csdnimg.cn/img_convert/ad91ac727b983a3659bca2c3f2f63b52.png)
修改
![bfacb0c76c7a81f893d940e4f6305727.png](https://img-blog.csdnimg.cn/img_convert/bfacb0c76c7a81f893d940e4f6305727.png)
删除
![4e74583a8436781baf36fbb0575599e5.png](https://img-blog.csdnimg.cn/img_convert/4e74583a8436781baf36fbb0575599e5.png)
查询列表
![13455c45469fb0ee7975b3eebfbfd42f.png](https://img-blog.csdnimg.cn/img_convert/13455c45469fb0ee7975b3eebfbfd42f.png)
这里的 from Customer 看上去和hql一样,但是实际上叫做 jpql,哈哈 换汤不换药。
![ef2f2a884516d0a7ffcfd556e69bc15b.png](https://img-blog.csdnimg.cn/img_convert/ef2f2a884516d0a7ffcfd556e69bc15b.png)
分页查询
![bd27465af5bc200c51b8fb530d46c0ac.png](https://img-blog.csdnimg.cn/img_convert/bd27465af5bc200c51b8fb530d46c0ac.png)
代码如下:
/** * 保存客户 */@Testpublic void test0(){ /*1.获取实体管理工厂*/ EntityManagerFactory factory = Persistence. createEntityManagerFactory("jpa"); /*2.获取实体管理*/ EntityManager entityManager = factory.createEntityManager(); /*3.获取事务对象*/ EntityTransaction transaction = entityManager.getTransaction(); /*4.开启事务*/ transaction.begin(); Customer customer = new Customer(); customer.setCustName("学友"); customer.setCustLevel("王者"); /*5.操作*/ entityManager.persist(customer); /*6.提交事务*/ transaction.commit(); /*7.关闭实体管理*/ entityManager.close();}/** * 根据id查询客户 */@Testpublic void test1(){ /*1.获取实体管理工厂*/ EntityManagerFactory factory = Persistence. createEntityManagerFactory("jpa"); /*2.获取实体管理*/ EntityManager entityManager = factory.createEntityManager(); /*3.获取事务对象*/ EntityTransaction transaction = entityManager.getTransaction(); /*4.开启事务*/ transaction.begin(); /*5.操作*/ //find == session.get //Customer customer = entityManager.find(Customer.class, 2L); //getReference == session.load Customer customer = entityManager.getReference(Customer.class, 2L); System.out.println(customer.getCustName()); /*6.提交事务*/ transaction.commit(); /*7.关闭实体管理*/ entityManager.close();}/** * 修改客户 */@Testpublic void test2(){ /*1.获取实体管理工厂*/ EntityManagerFactory factory = Persistence. createEntityManagerFactory("jpa"); /*2.获取实体管理*/ EntityManager entityManager = factory.createEntityManager(); /*3.获取事务对象*/ EntityTransaction transaction = entityManager.getTransaction(); /*4.开启事务*/ transaction.begin(); /*5.操作*/ Customer customer = entityManager.find(Customer.class, 2L); customer.setCustName("学友"); entityManager.persist(customer); /*6.提交事务*/ transaction.commit(); /*7.关闭实体管理*/ entityManager.close();}/** * 删除客户 */@Testpublic void test3(){ /*1.获取实体管理工厂*/ EntityManagerFactory factory = Persistence. createEntityManagerFactory("jpa"); /*2.获取实体管理*/ EntityManager entityManager = factory.createEntityManager(); /*3.获取事务对象*/ EntityTransaction transaction = entityManager.getTransaction(); /*4.开启事务*/ transaction.begin(); /*5.操作*/ Customer customer = entityManager.find(Customer.class, 2L); entityManager.remove(customer); /*6.提交事务*/ transaction.commit(); /*7.关闭实体管理*/ entityManager.close();}/** * 查询列表 */@Testpublic void test4(){ /*1.获取实体管理工厂*/ EntityManagerFactory factory = Persistence. createEntityManagerFactory("jpa"); /*2.获取实体管理*/ EntityManager entityManager = factory.createEntityManager(); /*3.获取事务对象*/ EntityTransaction transaction = entityManager.getTransaction(); /*4.开启事务*/ transaction.begin(); /*5.操作*/ List list = entityManager.createQuery("from Customer