您现在的位置是:首页 > java技术交流java技术交流
spring data jpa 自定义查询自动解析
上善若水2021-01-29 13:38:45【java技术交流】 2638人已围观
简介JpaRepositoryg会自动解析编写的接口,直接实现里面的方法.直接调用直接可用,还是非常强大的.规则: findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写)
JpaRepositoryg会自动解析编写的接口,直接实现里面的方法.直接调用直接可用,还是非常强大的.
规则:
findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写)
@Repository
public interface ArticleRepository extends JpaRepository<Article, Integer> {
List<Article> findByTitle(String title);
List<Article> findByTitleLike(String title);
List<Article> findByCreateTimeBetween(LocalDateTime start, LocalDateTime end);
List<Article> findByCreateTimeGreaterThanEqual(LocalDateTime start);
List<Article> findByCreateTimeNotNull();
}
@Test
public void testFindByTitle(){
List<Article> articles = articleRepository.findByTitle("该如何才能成功");
}
@Test
public void testFindByTitleLike(){
List<Article> articles = articleRepository.findByTitleLike("该如何"+'%');
}
@Test
public void testFindByCreateTimeBetween(){
LocalDateTime start=LocalDateTime.of(2019, Month.DECEMBER,17,16,39,44);
LocalDateTime end=LocalDateTime.parse("2020-12-17 16:39:44", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
List<Article> articles = articleRepository.findByCreateTimeBetween(start,end);
}
@Test
public void testFindByCreateTimeCreateTimeAfter(){
LocalDateTime start=LocalDateTime.of(2019, Month.DECEMBER,17,16,39,44);
List<Article> articles = articleRepository.findByCreateTimeGreaterThanEqual(start);
}
@Test
public void testFindByCreateTimeNotNull(){
List<Article> articles = articleRepository.findByCreateTimeNotNull();
}
都是可以正常调用的.不存在这种findByCreateTimeAfterEqual
大于等于的写法,程序会运行不起来
关键字 | 方法命名 | sql where 字句 |
---|---|---|
And | findByNameAndPwd | where name= ? and pwd =? |
Or | findByNameOrSex | where name= ? or sex=? |
Is,Equal | findById, | findByIdEquals |
Between | findByIdBetween | where id between ? and ? |
LessThan | findByIdLessThan | where id < ? |
LessThanEqual | findByIdLessThanEquals | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEqual | findByIdGreaterThanEquals | where id > = ? |
After | findByIdAfter | where id > ? |
Before | findByIdBefore | where id < ? |
IsNull | findByNameIsNull | where name is null |
isNotNull,Not Null | findByNameNotNull | where name is not |
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(?) |
Tags: jpa
很赞哦! (1)
随机图文
-
开怀一笑,让你开心的幽默故事三则
1.信不信由你 现在让骗子闹的人们对任何事情都不敢轻信,真是多次被忽悠,遇事要三思。如果拿着一瓶矿泉水告诉你,这是某某大师施过法术的,可以益寿延年,你肯定不信,如果说这瓶水经过高科技纳米技术处理,又由美国科学院认证,你八成就信了。 你爱人买了件连衣裙,大家都说她穿上真好看,她不一定信,如果有一个说她穿上不好看,她肯定信了。 明星访谈,说他们夫妻如何相亲相爱,你大概不太相信,如果有人说他们俩闹 -
让人瞬间爆笑的笑话
1、“王爷,王妃被您送去快手已经三天了。”“她认错了吗?”“没有,王妃说花花世界迷人眼,没有实力你别赛脸,今朝你薄情无义把我放逐,他日我涅槃重生必把你铲除。送你一句话,铁汁,做王呢,是该傲,但是给你脸呢,你得要!”2、去掉一个最高温,去掉一个最低温,今天的天气预报播放完了!3、上课的时候,小明被老师叫起来答题,小明:谢邀。4、你要做自己杀手小张接到了这样的命令。5、一男 -
爆笑喷饭的长篇笑话集锦
1.高中的时候,一次下课,同学们都抢着到外面买盒饭。一女生为了比别人先到,绕了个近道走,结果前面窨井盖没盖好,掉了下去!一会儿她撑着井沿往上爬,很是狼狈,一群初中小孩惊骇地从身边走过,她竟急中生智,一边爬一边说:哎!真难修啊……2.还是中学时,夏天裸睡被蚊子咬到DD,上课奇痒难忍,可总不能伸进去抓痒吧,我忍!痛不欲生!课间偷了同桌(女生)的一盒清凉油狂奔厕所,抹上之后才追悔莫及————那个刺激 -
笑话几则:哄女孩开心的最佳方式,你知道么
1,老婆买了条狗,没事干就拿着人民币让狗闻。我感觉很奇怪,就问:老婆,你这是干嘛,让狗去给你大马路上捡钱?老婆神秘的一笑:“以后你就知道了!”没过几天,我的私房钱不见了,不说了,说多了都是泪。。。2,刚老婆妇儿拿圆珠笔不停的捣我的头,我说:别捣了,老是捣一个地方疼。老婆一巴掌过来:你也知道老是捣一个地方疼?3,千年后,在某山里住着一位修道千年的老神仙。某天,他徒弟突然问他:师傅,是什么让你