SpringBoot项目 使用Sprin Data Jpa ——JpaRepository查询功能
2018-04-03 11:11
459 查看
关键字 | 方法命名 | sql where字句 |
And | findByNameAndPwd | where name= ? and pwd =? |
Or | findByNameOrSex | where name= ? or sex=? |
Is,Equals | findById,findByIdEquals | where id= ? |
Between | findByIdBetween | where id between ? and ? |
LessThan | findByIdLessThan | where id < ? |
LessThanEquals | findByIdLessThanEquals | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEquals | findByIdGreaterThanEquals | where id > = ? |
After | findByIdAfter | where id > ? |
Before | findByIdBefore | where id < ? |
IsNull | findByNameIsNull | where name is null |
isNotNull,NotNull | findByNameNotNull | where name is not null |
Like | findByNameLike | where name like ? |
NotLike | findByNameNotLike | where name not like ? |
StartingWith | findByNameStartingWith | where name like '?%' |
EndingWith | findByNameEndingWith | where name like '%?' |
Containing | findByNameContaining | where name like '%?%' |
OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
Not | findByNameNot | where name <> ? |
In | findByIdIn(Collection<?> c) | where id in (?) |
NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) |
True | findByAaaTue | where aaa = true |
False | findByAaaFalse | where aaa = false |
IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
2.JpaRepository相关查询功能a.Spring DataJPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析。b.假如创建如下的查询:findByUserDepUuid(),框架在解析该方法时,首先剔除findBy,然后对剩下的属性进行解析,假设查询实体为Doc。1:先判断userDepUuid (根据POJO规范,首字母变为小写)是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,继续第二步;2:从右往
4000
左截取第一个大写字母开头的字符串此处为Uuid),然后检查剩下的字符串是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,则重复第二步,继续从右往左截取;最后假设user为查询实体的一个属性;3:接着处理剩下部分(DepUuid),先判断user所对应的类型是否有depUuid属性,如果有,则表示该方法最终是根据“Doc.user.depUuid” 的取值进行查询;否则继续按照步骤2的规则从右往左截取,最终表示根据“Doc.user.dep.uuid” 的值进行查询。4:可能会存在一种特殊情况,比如Doc包含一个user的属性,也有一个userDep 属性,此时会存在混淆。可以明确在属性之间加上"_"以显式表达意图,比如"findByUser_DepUuid()"或者"findByUserDep_uuid()"c.特殊的参数: 还可以直接在方法的参数上加入分页或排序的参数,比如:Page<UserModel>findByName(String name, Pageable pageable);List<UserModel>findByName(String name, Sort sort);d.也可以使用JPA的NamedQueries,方法如下:1:在实体类上使用@NamedQuery,示例如下:@NamedQuery(name ="UserModel.findByAge",query = "select o from UserModelo where o.age >=?1")2:在自己实现的DAO的Repository接口里面定义一个同名的方法,示例如下:publicList<UserModel> findByAge(int age);3:然后就可以使用了,Spring会先找是否有同名的NamedQuery,如果有,那么就不会按照接口定义的方法来解析。e.还可以使用@Query来指定本地查询,只要设置nativeQuery为true,比如:@Query(value="select* from tbl_user where name like %?1" ,nativeQuery=true)publicList<UserModel> findByUuidOrAge(String name);注意:当前版本的本地查询不支持翻页和动态的排序f.使用命名化参数,使用@Param即可,比如:@Query(value="selecto from UserModel o where o.name like %:nn")publicList<UserModel> findByUuidOrAge(@Param("nn") String name);g.同样支持更新类的Query语句,添加@Modifying即可,比如:@Modifying@Query(value="updateUserModel o set o.name=:newName where o.name like %:nn")public intfindByUuidOrAge(@Param("nn") String name,@Param("newName")StringnewName);注意:1:方法的返回值应该是int,表示更新语句所影响的行数2:在调用的地方必须加事务,没有事务不能正常执行f.创建查询的顺序Spring Data JPA在为接口创建代理对象时,如果发现同时存在多种上述情况可用,它该优先采用哪种策略呢?<jpa:repositories>提供了query-lookup-strategy 属性,用以指定查找的顺序。它有如下三个取值:1:create-if-not-found:如果方法通过@Query指定了查询语句,则使用该语句实现查询;如果没有,则查找是否定义了符合条件的命名查询,如果找到,则使用该命名查询;如果两者都没有找到,则通过解析方法名字来创建查询。这是querylookup-strategy 属性的默认值2:create:通过解析方法名字来创建查询。即使有符合的命名查询,或者方法通过@Query指定的查询语句,都将会被忽略3:use-declared-query:如果方法通过@Query指定了查询语句,则使用该语句实现查询;如果没有,则查找是否定义了符合条件的命名查询,如果找到,则使用该命名查询;如果两者都没有找到,则抛出异常
、、
相关文章推荐
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- 快速构建Spring boot项目(功能是检测访问页面的设备)
- spring boot集成velocity及使用layout和2.X的toolbox功能
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- 使用IDEA从零配置springboot项目环境
- 使用SpringBoot,优质快速开发Java项目
- [转] 使用Spring Boot和Gradle创建项目
- SpringBoot+Freemarker 项目ftl中使用include的异常
- springboot+angular项目 使用token方式进行权限验证
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- 使用Spring中的IoC功能来实现我们所开发项目系统的国际化
- 从零学spring boot--集成mybatis--项目--使用注解方式
- 《Gradle教程系列》四.使用gradle搭建SpringBoot项目
- 使用Spring Boot和Gradle创建AngularJS项目
- 使用spring boot 改造老项目,多个数据源问题
- 使用Dockerfile构建Springboot项目(视频)
- 使用spring boot快速构建spring框架的maven项目
- 如何在Spring Boot项目使用参数校验
- JavaWeb企业实训 简易股市自选查询分析系统(六)使用Shiro控制登录等权限管理与Spring项目整合