springboot(五):spring data jpa的使用

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: 在上篇文章 springboot(二):web综合开发 中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spring data jpa 开发时,发现国内对spring boot jpa全面介绍的文章比较少案例也比较零碎,因此写文章总结一下。

在上篇文章 springboot(二):web综合开发 中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项
使用spring data jpa 开发时,发现国内对spring boot jpa全面介绍的文章比较少案例也比较零碎,因此写文章总结一下。本人也正在翻译 Spring Data JPA 参考指南 ,有兴趣的同学欢迎联系我,一起加入翻译中!

spring data 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 data jpa

Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用 Spring Data JPA 可以极大提高开发效率!
spring data jpa让我们解脱了DAO层的操作,基本上所有CRUD都可以依赖于它来实现

基本查询

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

预先生成方法

spring data jpa 默认预先生成了一些基本的CURD的方法,例如:增、删、改等等
1 继承JpaRepository

public interface UserRepository extends JpaRepository<User, Long> {

}

2 使用默认方法

@Test

public 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 后面跟属性名称:

User findByUserName(String userName);

也使用一些加一些关键字 And 、 Or

User findByUserNameOrEmail(String username, String email);

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

Long deleteById(Long id);

Long countByUserName(String userName)

基本上SQL体系中的关键词都可以使用,例如: LIKE 、 IgnoreCase 、 OrderBy 。

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 data jpa已经帮我们实现了分页的功能,在查询的方法中,需要传入参数 Pageable
,当查询中有多个参数的时候 Pageable 建议做为最后一个参数传入

Page<User> findALL(Pageable pageable);

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

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

@Test

public 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个元素,或者支取前一个实体。

ser 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 对事物的支持,查询超时的设置等

@Modifying

@Query("update User u set u.userName = ?1 where c.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 data jpa中有两种实现方式,第一种是利用hibernate的级联查询来实现,第二种是创建一个结果集的接口来接收连表查询后的结果,这里主要第二种方式。
首先需要定义一个结果集的接口类。

public interface HotelSummary {

City getCity();

String getName();

Double getAverageRating();

default Integer getAverageRatingRounded() {

return getAverageRating() == null ? null : (int) Math.round(getAverageRating());

}

}

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

@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);

使用

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 的形式来获取

多数据源的支持

同源数据库的多源支持

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

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

异构数据库多源支持

比如我们的项目中,即需要对mysql的支持,也需要对mongodb的查询等。
实体类声明 @Entity 关系型数据库支持类型、声明 @Document 为mongodb支持类型,不同的数据源使用不同的实体就可以了

interface PersonRepository extends Repository<Person, Long> {

 …

}

@Entity

public class Person {

  …

}

interface UserRepository extends Repository<User, Long> {

 …

}

@Document

public class User {

  …

}

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

interface JpaPersonRepository extends Repository<Person, Long> {

 …

}

interface MongoDBPersonRepository extends Repository<Person, Long> {

 …

}

@Entity

@Document

public class Person {

  …

}

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

@EnableJpaRepositories(basePackages = "com.neo.repositories.jpa")

@EnableMongoRepositories(basePackages = "com.neo.repositories.mongo")

interface Configuration { }

其它

使用枚举

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

@Enumerated(EnumType.STRING) 

@Column(nullable = true)

private UserType type;

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

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

@Transient

private String  userName;

作者:纯洁的微笑
原文链接

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
3天前
|
移动开发 前端开发 NoSQL
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
44 0
|
4天前
|
XML Java 数据库连接
Spring框架与Spring Boot的区别和联系
Spring框架与Spring Boot的区别和联系
15 0
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
18天前
|
Java Maven Docker
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
26 2
|
1月前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
1月前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
107 2
|
1月前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
42 0
|
2月前
|
Java Maven 微服务
springboot项目开启远程调试-jar包
springboot项目开启远程调试-jar包
26 0
|
4天前
|
消息中间件 JSON Java
RabbitMQ的springboot项目集成使用-01
RabbitMQ的springboot项目集成使用-01