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 |
---|---|---|
findById | where id=? | |
findByIdIs | is | where id=? |
findByIdEquals | equals | where id=? |
findByNameAndAge | and | where name=? and age=? |
findByNameOrAge | or | where name=? or age=? |
findByNameOrderByAgeDesc | order by | where name=? order by age desc |
findByAgeNotIn | not in | where age not in(?) |
findByStatusTrue | true | where status=true |
findByStatusFalse | false | where status=false |
findByAgeBetween | between | where age between ? and ? |
findByNameNot | not | where name <> ? |
findByAgeLessThan | LessThan | where age< ? |
findByAgeLessThanEqual | LessThanEqual | where age<=? |
findByAgeGreaterThan | GreaterThan | where age>? |
findByAgeGreaterThanEqual | GreaterThanEqual | where age>=? |
findByAgeAfter | after | where age>? |
findByAgeBefore | before | where age< ? |
findByNameIsNull | is null | where name is null |
findByNameNotNull | not null | where name not null |
findByNameLike | like | where name like ? |
findByNameNotLike | not like | where name not like ? |
findByNameStartingWith | StartingWith | where name like ‘?%’ |
findByNameEndingWith | EndingWith | where name like ‘%?’ |
findByNameContaining | Containing | where 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+表中列名,表的列名还有关键字等等拼接时,它们的首字母要大写.
本次就暂时写到这,如有不对请见谅,在错误中成长.