您的位置:首页 > 编程语言 > Java开发

SpringBoot项目 使用Sprin Data Jpa ——JpaRepository查询功能

2018-04-03 11:11 459 查看
关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?
StartingWith
findByNameStartingWithwhere name like '?%'
EndingWithfindByNameEndingWithwhere name like '%?'
ContainingfindByNameContainingwhere name like '%?%'
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not  in (?)
TruefindByAaaTue
where aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)
在方法上加@Query就不需要遵守上面的规则了,可以进行自定义sql。

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指定了查询语句,则使用该语句实现查询;如果没有,则查找是否定义了符合条件的命名查询,如果找到,则使用该命名查询;如果两者都没有找到,则抛出异常

、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: