Fork me on GitHub

十三、Spring Boot整合Spring-data-jpa详解

img

Spring Boot Jpa 介绍

首先了解 Jpa 是什么?

Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范。它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据。它的出现主要是为了简化现有的持久化开发工作和整合 ORM 技术,结束现在 Hibernate,TopLink,JDO 等 ORM 框架各自为营的局面。

值得注意的是,Jpa是在充分吸收了现有 Hibernate,TopLink,JDO 等 ORM 框架的基础上发展而来的,具有易于使用,伸缩性强等优点。从目前的开发社区的反应上看,Jpa 受到了极大的支持和赞扬,其中就包括了 Spring 与 EJB3. 0的开发团队。

注意:Jpa 是一套规范,不是一套产品,那么像 Hibernate,TopLink,JDO 他们是一套产品,如果说这些产品实现了这个 Jpa 规范,那么我们就可以叫他们为 Jpa 的实现产品。

Spring Boot Jpa

Spring Boot Jpa 是 Spring 基于 ORM 框架、Jpa 规范的基础上封装的一套 Jpa 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用 Spring Data Jpa 可以极大提高开发效率!

Spring Boot Jpa 让我们解脱了 DAO 层的操作,基本上所有 CRUD 都可以依赖于它来实现

基本查询

基本查询也分为两种,一种是 Spring Data 默认已经实现,一种是根据查询的方法来自动解析成 SQL。

预先生成方法

Spring Boot Jpa 默认预先生成了一些基本的CURD的方法,例如:增、删、改等等

1 继承 JpaRepository

1
2
public interface UserRepository extends JpaRepository<User, Long> {}
//集成JpaRepository,泛型里面第一个是User类,第二个Long是int类型的值

2 使用默认方法

1
2
3
4
5
6
7
8
9
@Testpublic void testBaseQuery() throws Exception {
User user=new User();
userRepository.findAll();
userRepository.findOne(1l);
userRepository.save(user);
userRepository.delete(user);
userRepository.count();
userRepository.exists(1l);
// ...}

就不解释了根据方法名就看出意思来

自定义简单查询

自定义的简单查询就是根据方法名来自动生成 SQL,主要的语法是 findXXBy, readAXXBy, queryXXBy, countXXBy, getXXBy后面跟属性名称:

1
User findByUserName(String userName);

也使用一些加一些关键字 AndOr

1
User findByUserNameOrEmail(String username, String email);

修改、删除、统计也是类似语法

1
Long deleteById(Long id);Long countByUserName(String userName);

基本上 SQL 体系中的关键词都可以使用,例如: LIKEIgnoreCaseOrderBy

1
List<User> findByEmailLike(String email);User findByUserNameIgnoreCase(String userName);List<User> findByUserNameOrderByEmailDesc(String email);

具体的关键字,使用方法和生产成SQL如下表所示

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
TRUE findByActiveTrue() … where x.active = true
FALSE findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

复杂查询

在实际的开发中我们需要用到分页、删选、连表等查询的时候就需要特殊的方法或者自定义 SQL

分页查询

分页查询在实际使用中非常普遍了,Spring Boot Jpa 已经帮我们实现了分页的功能,在查询的方法中,需要传入参数 Pageable ,当查询中有多个参数的时候 Pageable建议做为最后一个参数传入.

1
Page<User> findALL(Pageable pageable);Page<User> findByUserName(String userName,Pageable pageable);

Pageable 是 Spring 封装的分页实现类,使用的时候需要传入页数每页条数排序规则

1
2
3
4
5
6
@Testpublic void testPageQuery() throws Exception {
int page=1,size=10;
Sort sort = new Sort(Direction.DESC, "id");
Pageable pageable = new PageRequest(page, size, sort); userRepository.findALL(pageable);
userRepository.findByUserName("testName", pageable);
}

限制查询

有时候我们只需要查询前N个元素,或者支取前一个实体。

1
2
3
4
5
User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
List<User> findFirst10ByLastname(String lastname, Sort sort);
List<User> findTop10ByLastname(String lastname, Pageable pageable);

自定义SQL查询

其实 Spring Data 觉大部分的 SQL 都可以根据方法名定义的方式来实现,但是由于某些原因我们想使用自定义的 SQL 来查询,Spring Data 也是完美支持的;在 SQL 的查询方法上面使用 @Query注解,如涉及到删除和修改在需要加上 @Modifying.也可以根据需要添加 @Transactional对事物的支持,查询超时的设置等。

1
2
3
4
5
6
7
8
9
@Modifying
@Query("update User u set u.userName = ?1 where u.id = ?2")
int modifyByIdAndUserId(String userName, Long id);
@Transactional
@Modifying@Query("delete from User where id = ?1")
void deleteByUserId(Long id);
@Transactional(timeout = 10)
@Query("select u from User u where u.emailAddress = ?1")
User findByEmailAddress(String emailAddress);

多表查询

多表查询 Spring Boot Jpa 中有两种实现方式,第一种是利用 Hibernate 的级联查询来实现,第二种是创建一个结果集的接口来接收连表查询后的结果,这里主要第二种方式。

首先需要定义一个结果集的接口类。

1
2
3
4
5
6
7
8
public interface HotelSummary {
City getCity();
String getName();
Double getAverageRating();
default Integer getAverageRatingRounded() {
return getAverageRating() == null ? null : (int) Math.round(getAverageRating());
}
}

查询的方法返回类型设置为新创建的接口

1
2
@Query("select h.city as city, h.name as name, avg(r.rating) as averageRating "        - "from Hotel h left outer join h.reviews r where h.city = ?1 group by h")Page<HotelSummary> findByCity(City city, Pageable pageable);
@Query("select h.name as name, avg(r.rating) as averageRating " - "from Hotel h left outer join h.reviews r group by h")Page<HotelSummary> findByCity(Pageable pageable);

使用

1
2
3
4
Page<HotelSummary> hotels = this.hotelRepository.findByCity(new PageRequest(0, 10, Direction.ASC, "name"));
for(HotelSummary summay:hotels){
System.out.println("Name" +summay.getName());
}

在运行中 Spring 会给接口(HotelSummary)自动生产一个代理类来接收返回的结果,代码汇总使用 getXX的形式来获取

多数据源的支持

同源数据库的多源支持

日常项目中因为使用的分布式开发模式,不同的服务有不同的数据源,常常需要在一个项目中使用多个数据源,因此需要配置 Spring Boot Jpa 对多数据源的使用,一般分一下为三步:

  • 1 配置多数据源
  • 2 不同源的实体类放入不同包路径
  • 3 声明不同的包路径下使用不同的数据源、事务支持

异构数据库多源支持

比如我们的项目中,即需要对 mysql 的支持,也需要对 Mongodb 的查询等。

实体类声明 @Entity 关系型数据库支持类型、声明 @Document 为 Mongodb 支持类型,不同的数据源使用不同的实体就可以了
1
2
3
4
interface PersonRepository extends Repository<Person, Long> { …}
@Entitypublic class Person { …}
interface UserRepository extends Repository<User, Long> { …}
@Documentpublic class User { …}

但是,如果 User 用户既使用 Mysql 也使用 Mongodb 呢,也可以做混合使用

1
2
3
interface JpaPersonRepository extends Repository<Person, Long> { …}
interface MongoDBPersonRepository extends Repository<Person, Long> { …}
@Entity@Documentpublic class Person { …}

也可以通过对不同的包路径进行声明,比如 A 包路径下使用 mysql,B包路径下使用 MongoDB

1
@EnableJpaRepositories(basePackages = "com.neo.repositories.jpa")@EnableMongoRepositories(basePackages = "com.neo.repositories.mongo")interface Configuration { }

其它

使用枚举

使用枚举的时候,我们希望数据库中存储的是枚举对应的 String 类型,而不是枚举的索引值,需要在属性上面添加 @Enumerated(EnumType.STRING) 注解

1
@Enumerated(EnumType.STRING) @Column(nullable = true)private UserType type;

不需要和数据库映射的属性

正常情况下我们在实体类上加入注解 @Entity,就会让实体类和表相关连如果其中某个属性我们不需要和数据库来关联只是在展示的时候做计算,只需要加上 @Transient属性既可。

1
@Transientprivate String  userName;

使用Spring Boot Jpa的步骤

1、配置数据源:

在application.properties或者application.yaml配置文件中配置数据库连接配置,如下代码

1
2
3
4
5
6
7
8
9
#数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3307/spring_boot_demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# JPA
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto= update:修改代码后,sql语句自动更新

2.配置pom文件:

在pom文件中添加如下依赖:

1
2
3
4
5
6
7
8
9
10
<!-- 数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

3.创建数据库:

创建一个spring_boot_demo的数据库

4.在bean包里面创建一个RoncooUserLog的实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.roncoo.education.bean;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class RoncooUserLog {

@Id
@GeneratedValue
private Integer id;

@Column
private Date createTime;

@Column
private String userName;

@Column
private String userIp;

//省略getter、setter、toString方法

}

@Entity:实体类声明 @Entity 关系型数据库支持类型

@Id :标识这个属性是主键

@GeneratedValue(strategy = GenerationType.IDENTITY)自增长策略

@Column(nullable = false, length = 20) // 映射为字段,值不能为空 //设置那些字段不能为空

5.创建RoncooUserLogDao的接口

RoncooUserLogDao继承JPA中的CrudRepository接口,接口的泛型是<RoncooUserLog,Integer>,既然它继承的接口里面已经提供了方法,那个之前在接口中提供的方法就可以删除掉了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.roncoo.education.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.roncoo.education.bean.RoncooUserLog;

public interface RoncooUserLogDao extends JpaRepository<RoncooUserLog, Integer> {
/**
* @param
* @return
* 如果JPA中没有满足的需求,我们就会用到@Query()注解,括号里面可以写sql语句
*/
@Query(value = "select u from RoncooUserLog u where u.userName=?1")
List<RoncooUserLog> findByUserName(String userName);

/**
* @param string
* @param string2
* @return
*/
List<RoncooUserLog> findByUserNameAndUserIp(String string, String string2);

/**
* @param
* @param pageable
* @return
*/
Page<RoncooUserLog> findByUserName(String userName, Pageable pageable);

}

6.创建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.roncoo.education;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.junit4.SpringRunner;

import com.roncoo.education.bean.RoncooUserLog;
import com.roncoo.education.dao.RoncooUserLogDao;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemo141ApplicationTests {

@Autowired
private RoncooUserLogDao roncooUserLogDao;

@Test
public void insert() {
RoncooUserLog entity = new RoncooUserLog();
entity.setUserName("无境");
entity.setUserIp("192.168.0.1");
entity.setCreateTime(new Date());
roncooUserLogDao.save(entity);
}

@Test
public void delete() {
roncooUserLogDao.delete(1);
}

@Test
public void update() {
RoncooUserLog entity = new RoncooUserLog();
entity.setId(2);
entity.setUserName("无境2");
entity.setUserIp("192.168.0.1");
entity.setCreateTime(new Date());
roncooUserLogDao.save(entity);
}

@Test
public void select() {
RoncooUserLog result = roncooUserLogDao.findOne(1);
System.out.println(result);
}

@Test
public void select2() {
List<RoncooUserLog> result = roncooUserLogDao.findByUserName("无境");
System.out.println(result);
}

@Test
public void select3() {
List<RoncooUserLog> result = roncooUserLogDao.findByUserNameAndUserIp("无境", "192.168.0.1");
System.out.println(result);
}

// 分页
@Test
public void queryForPage() {
//Pageable:分页类,分页类的参数类型 0 代表当前第几页,20代表当前页有第几条 ,
new Sort是排序,排序是按组件倒序来排
Pageable pageable = new PageRequest(0, 20, new Sort(new Order(Direction.DESC, "id")));
Page<RoncooUserLog> result = roncooUserLogDao.findByUserName("无境", pageable);
System.out.println(result.getContent());
}

}
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!