JPA接口查询

JPA接口查询

本人首次玩,有不对的地方请见谅,不喜勿喷,谢谢.

有时可以看到在dao里面的接口方法,有些是没有用注解写sql语句也没有在配置文件写sql语句的,而只是有个光秃秃的接口名,但是它既然还可以查询到数据库里的数据.因为这是用到了jpa,接口名遵循jpa方法命名规则,通过接口名就可以解析成sql语句,然后查到数据.

如有需要可以找度娘先了解下jpa

接下来本人通过下面的接口类来讲解下:

public interface StudentDao extends JpaRepository<Student, String>{
    Student findById(String id);
    Student findByIdIs(String id);
    Student findByIdEquals(String id);
    List<Student> findByNameAndAge(String name, String age);
    List<Student> findByNameOrAge(String name, String age);
    List<Student> findByNameAndAgeAndSex(String name, String age, String sex);
    List<Student> findByNameInAndAgeIsNull(List<String> names,String age);
    List<Student> findByNameAndAgeInAndSexIn(String name, List<String> ages, List<String> sexs);
    List<Student> findByNameOrderByAgeDesc(List<String> names);
    List<Student> findByAgeNotIn(List<Age> age);
    List<Student> findByStatusTrue();
    List<Student> findByStatusFalse();
    List<Student> findByNameNot(String name);
    List<Student> findByAgeBetween(String maxAge,String minAge);
    List<Student> findByAgeLessThan(String age);
    List<Student> findByAgeLessThanEqual(String age);
    List<Student> findByAgeGreaterThan(String age);
    List<Student> findByAgeGreaterThanEqual(String age);
    List<Student> findByAgeAfter(String age);
    List<Student> findByAgeBefore(String age);
    List<Student> findByNameIsNull(String name);
    List<Student> findByNameNotNull(String name);
    List<Student> findByNameLike(String name);
    List<Student> findByNameNotLike(String name);
    List<Student> findByNameStartingWith(String name);
    List<Student> findByNameEndingWith(String name);
    List<Student> findByNameContaining(String name);
}

代码中的JpaRepository中Student是一个相关的dto数据传输对象,如不知什么是dto可以百度下,这个Student换个说法,就是你要操作的数据库表.

下面根据代码依次列出方法名表明的意思,如有不对请指出,勿喷,谢谢.

方法名关键字sql
findByIdwhere id=?
findByIdIsiswhere id=?
findByIdEqualsequalswhere id=?
findByNameAndAgeandwhere name=? and age=?
findByNameOrAgeorwhere name=? or age=?
findByNameOrderByAgeDescorder bywhere name=? order by age desc
findByAgeNotInnot inwhere age not in(?)
findByStatusTruetruewhere status=true
findByStatusFalsefalsewhere status=false
findByAgeBetweenbetweenwhere age between ? and ?
findByNameNotnotwhere name <> ?
findByAgeLessThanLessThanwhere age< ?
findByAgeLessThanEqualLessThanEqualwhere age<=?
findByAgeGreaterThanGreaterThanwhere age>?
findByAgeGreaterThanEqualGreaterThanEqualwhere age>=?
findByAgeAfterafterwhere age>?
findByAgeBeforebeforewhere age< ?
findByNameIsNullis nullwhere name is null
findByNameNotNullnot nullwhere name not null
findByNameLikelikewhere name like ?
findByNameNotLikenot likewhere name not like ?
findByNameStartingWithStartingWithwhere name like ‘?%’
findByNameEndingWithEndingWithwhere name like ‘%?’
findByNameContainingContainingwhere name like ‘%?%’

代码中几个复杂的.
findByNameAndAgeAndSex:表示where name=? and age=? and sex=?
findByNameInAndAgeIsNull :表示where name in (?) and age is null
findByNameAndAgeInAndSexIn:表示where name=? and age in(?) and sex in(?)

可以看出关键字是可以连用的,查找都是用findBy+表中列名,表的列名还有关键字等等拼接时,它们的首字母要大写.
本次就暂时写到这,如有不对请见谅,在错误中成长.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值