5SpringBoot五spring data jpa的使用.docx

上传人:b****5 文档编号:8585438 上传时间:2023-01-31 格式:DOCX 页数:7 大小:19.83KB
下载 相关 举报
5SpringBoot五spring data jpa的使用.docx_第1页
第1页 / 共7页
5SpringBoot五spring data jpa的使用.docx_第2页
第2页 / 共7页
5SpringBoot五spring data jpa的使用.docx_第3页
第3页 / 共7页
5SpringBoot五spring data jpa的使用.docx_第4页
第4页 / 共7页
5SpringBoot五spring data jpa的使用.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

5SpringBoot五spring data jpa的使用.docx

《5SpringBoot五spring data jpa的使用.docx》由会员分享,可在线阅读,更多相关《5SpringBoot五spring data jpa的使用.docx(7页珍藏版)》请在冰豆网上搜索。

5SpringBoot五spring data jpa的使用.docx

5SpringBoot五springdatajpa的使用

在上篇文章springboot

(二):

web综合开发中简单介绍了一下springdatajpa的基础性使用,这篇文章将更加全面的介绍springdatajpa常见用法以及注意事项

使用springdatajpa开发时,发现国内对springbootjpa全面介绍的文章比较少案例也比较零碎,因此写文章总结一下。

本人也正在翻译SpringDataJPA参考指南,有兴趣的同学欢迎联系我,一起加入翻译中!

springdatajpa介绍

首先了解JPA是什么?

JPA(JavaPersistenceAPI)是Sun官方提出的Java持久化规范。

它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。

他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibernate,TopLink,JDO等ORM框架各自为营的局面。

值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。

从目前的开发社区的反应上看,JPA受到了极大的支持和赞扬,其中就包括了Spring与EJB3.0的开发团队。

注意:

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

springdatajpa

SpringDataJPA是Spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。

它提供了包括增删改查等在内的常用功能,且易于扩展!

学习并使用SpringDataJPA可以极大提高开发效率!

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

基本查询

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

预先生成方法

springdatajpa默认预先生成了一些基本的CURD的方法,例如:

增、删、改等等

1继承JpaRepository

publicinterfaceUserRepositoryextendsJpaRepository{}

2使用默认方法

@TestpublicvoidtestBaseQuery()throwsException{

Useruser=newUser();

userRepository.findAll();

userRepository.findOne(1l);

userRepository.save(user);

userRepository.delete(user);

userRepository.count();

userRepository.exists(1l);

//...}

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

自定义简单查询

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

UserfindByUserName(StringuserName);

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

UserfindByUserNameOrEmail(Stringusername,Stringemail);

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

LongdeleteById(Longid);

LongcountByUserName(StringuserName)

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

LIKE、 IgnoreCase、 OrderBy。

ListfindByEmailLike(Stringemail);

UserfindByUserNameIgnoreCase(StringuserName);

ListfindByUserNameOrderByEmailDesc(Stringemail);

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

Keyword

Sample

JPQLsnippet

And

findByLastnameAndFirstname

…wherex.lastname=?

1andx.firstname=?

2

Or

findByLastnameOrFirstname

…wherex.lastname=?

1orx.firstname=?

2

Is,Equals

findByFirstnameIs,findByFirstnameEquals

…wherex.firstname=?

1

Between

findByStartDateBetween

…wherex.startDatebetween?

1and?

2

LessThan

findByAgeLessThan

…wherex.age

1

LessThanEqual

findByAgeLessThanEqual

…wherex.age⇐?

1

GreaterThan

findByAgeGreaterThan

…wherex.age>?

1

GreaterThanEqual

findByAgeGreaterThanEqual

…wherex.age>=?

1

After

findByStartDateAfter

…wherex.startDate>?

1

Before

findByStartDateBefore

…wherex.startDate

1

IsNull

findByAgeIsNull

…wherex.ageisnull

IsNotNull,NotNull

findByAge(Is)NotNull

…wherex.agenotnull

Like

findByFirstnameLike

…wherex.firstnamelike?

1

NotLike

findByFirstnameNotLike

…wherex.firstnamenotlike?

1

StartingWith

findByFirstnameStartingWith

…wherex.firstnamelike?

1 (parameterboundwithappended %)

EndingWith

findByFirstnameEndingWith

…wherex.firstnamelike?

1 (parameterboundwithprepended %)

Containing

findByFirstnameContaining

…wherex.firstnamelike?

1 (parameterboundwrappedin %)

OrderBy

findByAgeOrderByLastnameDesc

…wherex.age=?

1orderbyx.lastnamedesc

Not

findByLastnameNot

…wherex.lastname<>?

1

In

findByAgeIn(Collection ages)

…wherex.agein?

1

NotIn

findByAgeNotIn(Collection age)

…wherex.agenotin?

1

TRUE

findByActiveTrue()

…wherex.active=true

FALSE

findByActiveFalse()

…wherex.active=false

IgnoreCase

findByFirstnameIgnoreCase

…whereUPPER(x.firstame)=UPPER(?

1)

复杂查询

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

分页查询

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

PagefindALL(Pageablepageable);

PagefindByUserName(StringuserName,Pageablepageable);

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

@TestpublicvoidtestPageQuery()throwsException{

intpage=1,size=10;

Sortsort=newSort(Direction.DESC,"id");

Pageablepageable=newPageRequest(page,size,sort);

userRepository.findALL(pageable);

userRepository.findByUserName("testName",pageable);}

限制查询

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

serfindFirstByOrderByLastnameAsc();

UserfindTopByOrderByAgeDesc();

PagequeryFirst10ByLastname(Stringlastname,Pageablepageable);

ListfindFirst10ByLastname(Stringlastname,Sortsort);

ListfindTop10ByLastname(Stringlastname,Pageablepageable);

自定义SQL查询

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

@Modifying@Query("updateUserusetu.userName=?

1wherec.id=?

2")intmodifyByIdAndUserId(StringuserName,Longid);

@Transactional@Modifying@Query("deletefromUserwhereid=?

1")voiddeleteByUserId(Longid);

@Transactional(timeout=10)@Query("selectufromUseruwhereu.emailAddress=?

1")

UserfindByEmailAddress(StringemailAddress);

多表查询

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

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

publicinterfaceHotelSummary{

CitygetCity();

StringgetName();

DoublegetAverageRating();

defaultIntegergetAverageRatingRounded(){

returngetAverageRating()==null?

null:

(int)Math.round(getAverageRating());

}

}

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

@Query("selecth.cityascity,h.nameasname,avg(r.rating)asaverageRating"

-"fromHotelhleftouterjoinh.reviewsrwhereh.city=?

1groupbyh")PagefindByCity(Citycity,Pageablepageable);

@Query("selecth.nameasname,avg(r.rating)asaverageRating"

-"fromHotelhleftouterjoinh.reviewsrgroupbyh")PagefindByCity(Pageablepageable);

使用

Pagehotels=this.hotelRepository.findByCity(newPageRequest(0,10,Direction.ASC,"name"));for(HotelSummarysummay:

hotels){

System.out.println("Name"+summay.getName());

}

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

多数据源的支持

同源数据库的多源支持

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

∙1配置多数据源

∙2不同源的实体类放入不同包路径

∙3声明不同的包路径下使用不同的数据源、事务支持

这里有一篇文章写的很清楚:

SpringBoot多数据源配置与使用

异构数据库多源支持

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

实体类声明@Entity 关系型数据库支持类型、声明@Document 为mongodb支持类型,不同的数据源使用不同的实体就可以了

interfacePersonRepositoryextendsRepository{

…}

@EntitypublicclassPerson{

…}

interfaceUserRepositoryextendsRepository{

…}

@DocumentpublicclassUser{

…}

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

interfaceJpaPersonRepositoryextendsRepository{

…}

interfaceMongoDBPersonRepositoryextendsRepository{

…}

@Entity@DocumentpublicclassPerson{

…}

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

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

其它

使用枚举

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

@Enumerated(EnumType.STRING)@Column(nullable=true)privateUserTypetype;

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

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

@TransientprivateStringuserName;

源码案例

这里有一个开源项目几乎使用了这里介绍的所有标签和布局,大家可以参考:

 cloudfavorites

参考

SpringDataJPA-ReferenceDocumentation

SpringDataJPA——参考文档中文版

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1