您现在的位置是:首页 > 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(?)

产考文章:https://www.cnblogs.com/chenglc/p/11226693.html

Tags: jpa

很赞哦! (1)

随机图文

文章评论

站点信息

  • 建站时间:2019-10-24
  • 网站程序:Thinkphp6 Layui
  • 文章统计247篇文章
  • 标签管理标签云
  • 统计数据cnzz统计
  • 微信公众号:扫描二维码,关注我们