使用JPA持久化对象的步骤

  1. 创建 persistence.xml, 在这个文件中配置持久化单元

需要指定跟哪个数据库进行交互;
需要指定 JPA 使用哪个持久化的框架以及配置该框架的基本属性

  1. 创建实体类, 使用 annotation 来描述实体类跟数据库表之间的映射关系.
  2. 使用 JPA API 完成数据增加、删除、修改和查询操作

创建 EntityManagerFactory (对应 Hibernate 中的 SessionFactory);
创建 EntityManager (对应 Hibernate 中的Session);

HelloWorld开始

1.创建persistence.xml 文件

这个要放到类路径下面的META-INF目录下面.
JPA规范要求在类路径的META-INF目录下面放persistence.xml,文件名称是固定的。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence version="2.0"
  3. xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  5. <!--
  6. name 属性用于定义持久化单元的名字, 必选
  7. transaction-type:指定 JPA 的事务处理策略。
  8. RESOURCE_LOCAL:默认值,数据库级别的事务,只能针对一种数据库,不支持分布式事务。
  9. 如果需要支持分布式事务,使用JTAtransaction-type="JTA"
  10. -->
  11. <persistence-unit name="jpa-1" transaction-type="RESOURCE_LOCAL">
  12. <!--
  13. 配置使用什么 ORM 产品来作为 JPA 的实现
  14. 1. 实际上配置的是 javax.persistence.spi.PersistenceProvider 接口的实现类
  15. 2. JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点.
  16. -->
  17. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  18. <!-- 添加持久化类 -->
  19. <class>cn.carven.jpa.helloworld.Customer</class>
  20. <properties>
  21. <!-- 连接数据库的基本信息 -->
  22. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  23. <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>
  24. <property name="javax.persistence.jdbc.user" value="root"/>
  25. <property name="javax.persistence.jdbc.password" value="123456"/>
  26. <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 -->
  27. <property name="hibernate.format_sql" value="true"/>
  28. <property name="hibernate.show_sql" value="true"/>
  29. <property name="hibernate.hbm2ddl.auto" value="update"/>
  30. </properties>
  31. </persistence-unit>
  32. </persistence>

2.导入相关依赖

02 入门HelloWorld - 图1

3.创建持久化类

  1. @Table(name = "jpa_customer")
  2. @Entity
  3. public class Customer {
  4. private Integer id;
  5. private String lastName;
  6. private String email;
  7. private int age;
  8. @Id
  9. @Column(name = "id")
  10. @GeneratedValue(strategy = GenerationType.AUTO)
  11. public Integer getId() {
  12. return id;
  13. }
  14. public void setId(Integer id) {
  15. this.id = id;
  16. }
  17. @Column(name = "last_name")
  18. public String getLastName() {
  19. return lastName;
  20. }
  21. public void setLastName(String lastName) {
  22. this.lastName = lastName;
  23. }
  24. public String getEmail() {
  25. return email;
  26. }
  27. public void setEmail(String email) {
  28. this.email = email;
  29. }
  30. public int getAge() {
  31. return age;
  32. }
  33. public void setAge(int age) {
  34. this.age = age;
  35. }
  36. }

4.执行持久化操作

  1. public static void main(String[] args) {
  2. // 1.创建EntityManagerFactory
  3. EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa-1");
  4. // 2.创建EntityManager
  5. EntityManager entityManager = entityManagerFactory.createEntityManager();
  6. // 3.开启事务
  7. EntityTransaction transaction = entityManager.getTransaction();
  8. transaction.begin();
  9. // 4.进行持久化操作
  10. Customer customer = new Customer();
  11. customer.setAge(12);
  12. customer.setEmail("123@qq.com");
  13. customer.setLastName("zs");
  14. customer.setBirth(new Date());
  15. customer.setCreatedTime(new Date());
  16. entityManager.persist(customer);
  17. // 5.提交事务
  18. transaction.commit();
  19. // 6.关闭EntityManager
  20. entityManager.close();
  21. // 7.关闭EntityManagerFactory
  22. entityManagerFactory.close();
  23. }