SpringDataJap常用的【命名规则】

SpringDataJap常用的命名规则

最近遇到开发遇到一个JPA的项目,我们的研发主管还明确要求能用JPA的原生语法尽量不用它的原生SQL语法

好家伙!!!我也不太熟啊!!好吧,好记性不如烂笔头,自己总结下吧!!

一.SpringDataJpa的常用命名规则汇总

这边是复制粘贴过来的

关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualfindByIdLessThanEqualwhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualfindByIdGreaterThanEqualwhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?
StartingWithfindByNameStartingWithwhere 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 (?)
TruefindByAaaTuewhere aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)

二.应用规则

这边就简单的举几个例子吧!

重点说下查小于和大于的日期的SQL

需求: 交易日期<核算日期<到期日期
JPA 的语法: findByPortCodeAndTradeDateBeforeAndSettleDueDateAfterAndTradeModeAndTradeTypeSub(String portCode,LocalDate lessBusinessDate, LocalDate greaterBusinessDate,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值