SpringDataJap
常用的命名规则
最近遇到开发遇到一个
JPA
的项目,我们的研发主管还明确要求能用JPA
的原生语法尽量不用它的原生SQL
语法好家伙!!!我也不太熟啊!!好吧,好记性不如烂笔头,自己总结下吧!!
一.SpringDataJpa
的常用命名规则汇总
这边是复制粘贴过来的
关键字 | 方法命名 | 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 < ? |
LessThanEqual | findByIdLessThanEqual | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEqual | findByIdGreaterThanEqual | 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(?) |
二.应用规则
这边就简单的举几个例子吧!
重点说下查小于和大于的日期的SQL
需求: 交易日期<核算日期<到期日期
JPA 的语法: findByPortCodeAndTradeDateBefore
AndSettleDueDateAfter
AndTradeModeAndTradeTypeSub(String portCode,LocalDatelessBusinessDate
, LocalDategreaterBusinessDate
,String tradeType, String tradeTypeSub);
//流水查询
//查询条件需要更改(添加业务类型)
List<Entity> entityList = Repository.findByPortCodeAndTradeDateBeforeAndSettleDueDateAfterAndTradeModeAndTradeTypeSub(
queryParam.getPortCode(), queryParam.getBusinessDate(),queryParam.getBusinessDate(),tradeType,tradeTypeSub);
return cktzEntityList;
原生的sql
:
select * from table where id=12344 and name='帅气少年';
转换成JPA
:
--说明下:id 和 name 就是对应的上面的1233 和'帅气少年' 两个你要传入的参数
findByidAndName(Integer id,String name);
再来一个啊!!!
select * from table where age <=30 ;--查询年龄小于30岁的
-----------JPA写法 和上面是对应的哈!!!
findByAgeLessThanEqual(String age);
好嘞!!上班了
![在这里插入图片描述](https://img-blog.csdnimg.cn/cbebe1c2215743639b3ec55d40fec1ce.jpeg