一、简介

1.1 什么是JPA

JPA (Java Persistence API)Java持久化API。是一套Sun公司Java官方制定的ORM方案,是规范,是标准 ,sun公司自己并没有实现

1.2 JPA的实现者

JPA是一套标准,意味着,它只是一套实现ORM理论的接口,没有实现的代码。
那么必须要有具体的实现者才可以完成ORM操作功能的实现!

市场上的主流的JPA框架(实现者)有:
Hibernate (JBoos)、EclipseTop(Eclipse社区)、OpenJPA (Apache基金会)。
其中Hibernate是众多实现者之中,性能最好的。

1.3 JPA的作用

JPA是ORM的一套标准,既然JPA为ORM而生,那么JPA的作用就是实现使用对象操作数据库,不用写SQL

二、简单测试

2.1 新建Maven项目

项目结构
image.png

2.2 Type实体类

  1. @Entity
  2. @Data
  3. @Table(name = "ticket_type")
  4. public class Type {
  5. @Id
  6. private int id;
  7. @Column
  8. private String name;
  9. }

2.3 xml配置文件

persistence.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
  3. <persistence-unit name="jpaunit" transaction-type="RESOURCE_LOCAL">
  4. <properties>
  5. <property name="javax.persistence.jdbc.user" value="root"/>
  6. <property name="javax.persistence.jdbc.password" value="123456"/>
  7. <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
  8. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/wn75_ticket_movie?serverTimezone=UTC"/>
  9. <property name="hibernate.show_sql" value="true" />
  10. </properties>
  11. </persistence-unit>
  12. </persistence>

2.4 TypeTest测试类

public class TypeTest {

    private EntityManager em;

    @Before
    public void setUp(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaunit");
        em = emf.createEntityManager();
        //开启事务
        em.getTransaction().begin();
    }

    @After
    public void tearDown(){
        //提交事务
        em.getTransaction().commit();
    }

    @Test
    public void testAdd(){
        Type type = new Type();
        type.setName("Type1");
        //执行新增语句
        em.persist(type);
    }

    @Test
    public void testUpdate(){
        Type type = new Type();
        type.setId(33);
        type.setName("Type2");
        //执行修改语句
        em.merge(type);
    }

    /**
     * jpql:java持久化查询语言
     */
    @Test
    public void testGetAll(){
        em.createQuery("from Type").getResultList().forEach(t->{
            System.out.println(t);
        });
    }

    @Test
    public void testGetAll2(){
        em.createQuery("from Type",Type.class).getResultList().forEach(t->{
            System.out.println(t);
        });
    }

    @Test
    public void testGetOne(){
        System.out.println(em.find(Type.class, 15));
    }

    @Test
    public void testDelete(){
        em.remove(em.find(Type.class,33));
    }
}

注:Persistence.createEntityManagerFactory(“jpaunit”)中的”jpaunit”要与persistence.xml中persistence-unit标签的name属性对应上
**

三、Spring Data Jpa

3.1 新建SpringBoot项目

项目结构
image.png

3.2 yml配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/wn75_ticket_movie?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: 123456

3.3 Type实体类

同上

3.4 TypeDao

public interface TypeDao extends JpaRepository<Type,Integer> {
    @Query(value = "from Type where name = :name")
    Type getByName(@Param("name") String name);

    List<Type> findByIdBetween(int start,int end);
}

3.5 TypeTest测试类

@SpringBootTest
@RunWith(SpringRunner.class)
class SpringbootJpaApplicationTests {

    @Resource
    private TypeDao typeDao;

    @Test
    void testAdd() {
        Type type = new Type();
        type.setName("Spring Data Jpa");
        typeDao.save(type);
        System.out.println(type);
    }

    @Test
    void testGetByName(){
        Type type = typeDao.getByName("喜剧");
        System.out.println(type);
    }

    @Test
    void testFindByIdBetween(){
        List<Type> types = typeDao.findByIdBetween(1,5);
        types.forEach(e->{
            System.out.println(e);
        });
    }

}