整合JPA
大约 2 分钟Spring全家桶SpringBoot精讲细讲
JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA是一种规范。Spring Data JPA规范了怎么操作持久层。Hibernate框架遵循了这个规范。
添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
show-sql: true
hibernate:
# 程序启动时删除并且创建实体类对应的表
ddl-auto: create
编写接口继承JpaRepository或者CrudRepository
JpaRepository接口
public interface PersonRepository extends JpaRepository<Person,Long> {
}
CrudRepository接口
public interface PersonRepository2 extends CrudRepository<Person,Long> {
}
简单增删改查
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
void contextLoads() {
Person person = new Person(1L, "张三", "123456", "2d2e25", new Date(), new Date(), new Date());
personRepository.save(person);
personRepository.findAll().forEach(System.out::println);
}
}

数据库中也有了一张表。

自定义方法
public interface PersonRepository extends JpaRepository<Person,Long> {
/**
* 通过username查询Person
* @param username 用户名
* @return
*/
List<Person> findByUsername(String username);
}
测试
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
void contextLoads() {
Person person = new Person(1L, "张三", "123456", "2d2e25", new Date(), new Date(), new Date());
personRepository.save(person);
personRepository.findAll().forEach(System.out::println);
List<Person> personList = personRepository.findByUsername("张三");
personList.forEach(System.out::println);
}
}

分页
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
void contextLoads() {
Person person1 = new Person(1L, "张三", "123456", "2d2e25", new Date(), new Date(), new Date());
Person person2 = new Person(2L, "张三风", "123456", "2d2e25", new Date(), new Date(), new Date());
Person person3 = new Person(3L, "lisi", "123456", "2d2e25", new Date(), new Date(), new Date());
personRepository.saveAll(Arrays.asList(person1, person2, person3));
List<Person> personList = personRepository.findAll(PageRequest.of(0, 2)).getContent();
personList.forEach(System.out::println);
}


hql
单对象查询
public interface PersonRepository extends JpaRepository<Person,Long> {
/**
* hql
* @param num1
* @param num2
* @return
*/
@Query(value = "from Person where id > :num1 and id < :num2")
List<Person> find(@Param("num1") Long num1, @Param("num2") Long num2);
}
测试
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
private User user;
@Autowired
private PersonRepository personRepository;
@Test
void contextLoads() {
Person person1 = new Person(1L, "张三", "123456", "2d2e25", new Date(), new Date(), new Date());
Person person2 = new Person(2L, "张三风", "123456", "2d2e25", new Date(), new Date(), new Date());
Person person3 = new Person(3L, "lisi", "123456", "2d2e25", new Date(), new Date(), new Date());
personRepository.saveAll(Arrays.asList(person1, person2, person3));
List<Person> people = personRepository.find(1L,3L);
people.forEach(System.out::println);
}
}

多对象查询
public interface PersonRepository extends JpaRepository<Person,Long> {
/**
* hql
* @param did
* @return
*/
@Query(value = "from Person p left join Dept d on d.id = p.deptId where d.id = :did")
List<Person> find(@Param("did") Long did);
}
普通SQL查询
public interface PersonRepository extends JpaRepository<Person,Long> {
/**
* 普通SQL
* @param did
* @return
*/
@Query(value = "select * from t_user p left join t_dept d on d.id = p.dept_id where d.id = ?1", nativeQuery = true)
List<Person> find( Long did);
}