通些关键字实现SQL命令
//And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex);
publicList<User> findByHeightAndSex(intheight,charsex);
// Or --- 等价于 SQL 中的 or 关键字,比如 findByHeightOrSex(int height,char sex);
publicList<User> findByHeightOrSex(intheight,charsex);
//Between --- 等价于 SQL 中的 between 关键字,比如 findByHeightBetween(int min, int max);
publicList<User> findByHeightBetween(intmin,intmax);
//LessThan --- 等价于 SQL 中的 "<",比如 findByHeightLessThan(int max);
publicList<User> findByHeightLessThan(intmax);
//GreaterThan --- 等价于 SQL 中的">",比如 findByHeightGreaterThan(int min);
publicList<User> findByHeightGreaterThan(intmin);
//IsNull --- 等价于 SQL 中的 "is null",比如 findByNameIsNull();
publicList<User> findByNameIsNull();
//IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByNameIsNotNull();
publicList<User> findByNameIsNotNull();
//NotNull --- 与 IsNotNull 等价;
publicList<User> findByNameNotNull();
//Like --- 等价于 SQL 中的 "like",比如 findByNameLike(String name);
publicList<User> findByNameLike(String name);
//NotLike --- 等价于 SQL 中的 "not like",比如 findByNameNotLike(String name);
publicList<User> findByNameNotLike(String name);
//OrderBy --- 等价于 SQL 中的 "order by",比如 findByNameNotNullOrderByHeightAsc();
publicList<User>findByNameNotNullOrderByHeightAsc();
//Not --- 等价于 SQL 中的 "! =",比如 findByNameNot(String name);
publicList<User> findByNameNot(String name);
//In --- 等价于 SQL 中的 "in",比如 findByNameIN(String name);
publicList<User> findByNameIn(String name);
//NotIn --- 等价于 SQL 中的 "not in",比如 findByNameNotIN(String name);
publicList<User> findByNameNotIn(String name);
jpa本身自带分页方法
关键字方法命名sql where字句
![](https://img.haomeiwen.com/i15080417/184ec0972ff849f7.png)
![](https://img.haomeiwen.com/i15080417/e6a6d4c96282234e.png)
查询是JPA中重要的内容,JPA中可以执行两种方式的查询,一种是使用是基于Entity对象的查询,一种是使用SQL语句去查询。
JPA中执行查询的步骤如下:
1) 编写查询语句
SELECT s FROM Student s
2) 创建查询
命名查询是一种优化的查询方式,可以提高查询的效率,命名查询最重要的是首先定义查询,JPA使用@Query注解来定义命名查询,然后通过Entity来获创建命名参数,如下
@Query("select o.serialNumber from Orders o where o.publicOpenid = ?1 order by o.createTime desc ")
List<String> findSerialNumberByPublicOpenId(String publicOpenid);
1、 必须在实体类上定义命名查询
注意:
u 自定义对象必须为POJO类,提供必要的构造函数;
u JPQL语句中要加NEW关键子,且类为全限定名;
在查询时,往往需要动态传入多个条件,此时需要组装JPQL。在组装JPQL时,有一个小技巧,可以巧妙利用”1=1”这个表达式,如下
SELECT s FROM Student s WHERE 1=1 AND s.id=:id AND s.name=:name
当增加查询条件时,只需要增加“AND查询条件即可”,无需做额外的逻辑判断。
网友评论