maven搭建JPA开发环境
1 依赖的maven pom文件
主要有hibernate-core、hibernate-entitymanager、javax-persistence、mysql驱动
jap-xx com.cmos 1.0-SNAPSHOT 4.0.0 jpa-01 UTF-8 4.3.8.Final 4.12 1.8 org.hibernate hibernate-core ${hibernate.version} org.hibernate hibernate-entitymanager ${hibernate.version} javax.persistence persistence-api 1.0.2 mysql mysql-connector-java 5.1.45 junit junit ${junit.version}
2 配置persistence.xml文件
在资源文件夹下创建META-INF文件夹,创建persistence.xml文件
3 创建实体类Employee类。
新建数据库,名为jpa,配置好上述的persistence.xml文件;新建实体类Employee
package com.cmos.jpa;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Tablepublic class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; private String password; ... 省略getter()和setter()}
4 新建测试类EmployeeTest
import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityTransaction;import javax.persistence.Persistence;import com.cmos.jpa.Employee;import org.junit.Test;public class EmployeeTest { @Test public void save() throws Exception { Employee employee=new Employee(); employee.setName("张三"); employee.setPassword("123456"); String persistenceUnitName="com.cmos.jpa"; // 1 获取实例管理工厂 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); // 2 获取实例管理对象 EntityManager entityManager = entityManagerFactory.createEntityManager(); // JAP默认事务不开启 EntityTransaction transaction = entityManager.getTransaction(); //3 开启事务 transaction.begin(); //4 持久化操作 entityManager.persist(employee); //5 提交事务 transaction.commit(); //6 关闭资源 entityManager.close(); entityManagerFactory.close(); }}
如下的结果显示:
查看数据库,发现存在测试数据!
5 注解解释
上述的注解解释:
① @Entity:表示实体对象,这个对象和数据库的表建立对应关系
② @Table(name="表名") 如果没有配置该注解或者该注解使用默认值关系,那么表的名称为类名称,首字母小写
③ @Id 表示主键
④ @GeneratedValue 配置主键的生成策略,默认值是@GeneratedValue(strategy = GenerationType.AUTO)
主键策略如果是:strategy = GenerationType.AUTO 在Mysql中就是:主键:AUTO_INCREMENT,在Oracle中就是序列;
表示主键自动
⑤ @Column注解:加在字段上,如果希望字段的名称和数据库对应的字段不一致,可以使用该注解,并且设置对应的列名;如果希望一致,可以不用加该注解
6 工具类抽取
public class JPAUtils { private static EntityManagerFactory entityManagerFactory; static { try { String persistenceUnitName="com.cmos.jpa"; entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); }catch (Exception e){ throw new RuntimeException("配置文件出错"+e.getMessage()); } } public static EntityManager getEntity(){ return entityManagerFactory.createEntityManager(); } public static void close(){ if(entityManagerFactory!=null){ entityManagerFactory.close(); } }}
7 测试CRUD操作
补充:建表策略是create:表示先删除表,然后建立新的表;
import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityTransaction;import javax.persistence.Persistence;import com.cmos.jpa.Employee;import com.cmos.jpa.JPAUtils;import org.junit.Before;import org.junit.Test;/*** 查询一个: find(T.class,Long id)* 修改: merge(T)* 保存:persistence(T)* 删除:remove(T) 直接删除对象**/public class EmployeeTest2 { /** * 保存 */ @Before public void save(){ Employee employee=new Employee(); employee.setName("张三"); employee.setPassword("123456"); Employee employee2=new Employee(); employee2.setName("里斯"); employee2.setPassword("123456"); EntityManager entity = JPAUtils.getEntity(); EntityTransaction transaction = entity.getTransaction();//获取事务 transaction.begin(); entity.persist(employee); transaction.commit(); } @Test public void queryAndUpdate(){ EntityManager entity = JPAUtils.getEntity(); entity.getTransaction().begin(); //查询 Employee employee = entity.find(Employee.class, 1L); System.out.println(employee); employee.setName("xyz"); employee.setPassword("99999"); //修改 entity.merge(employee); System.out.println(employee); entity.getTransaction().commit(); } @Test public void delete(){ EntityManager entity = JPAUtils.getEntity(); entity.getTransaction().begin(); Employee employee = entity.find(Employee.class, 1L); entity.remove(employee); System.out.println(employee); entity.getTransaction().commit(); } //JPQL查询 @Test public void queryJPQL(){ EntityManager entity = JPAUtils.getEntity(); entity.getTransaction().begin(); String jpql="select o from Employee o"; Query query = entity.createQuery(jpql); ListresultList = query.getResultList(); for (Employee e:resultList) { System.out.println("0000000000"); System.out.println(e); } entity.getTransaction().commit(); }}
7 配置自动建表
create-drop:删除表----建表----删除表(一般不用)
删除表的时间:EntityManageFactory关闭之后
create----删除表----建立表
update----用在项目中
validate----数据库已经存在(项目已经上线)
总结:JPA的CRUD操作:
查询:find或者使用JPQL查询;
保存:persistence
修改:merge
删除: remove