spring-data-jpa 新增 修改 删除 查询 分页

 

1、查询所有数据 findAll()

2、分页查询 findAll(new PageRequest(0, 2))      

3、根据id查询 findOne()      

4、根据实体类属性查询: findByProperty (type Property);   例如:findByAge(int age);

5、排序:  findAll(sort )

      Sort sort = new Sort(Sort.Direction.DESC, "age").and (new Sort(Sort.Direction.DESC, "id"));

6、条件查询  and/or/findByAgeLessThan/LessThanEqual 等, 

     例如: findByUsernameAndPassword(String username , String password)

7、总数 查询 count()  或者 根据某个属性的值查询总数countByAge(int age);

8、是否存在某个id   exists()

二、修改,删除,新增
新增:直接使用 save(T) 方法
删除: delete()  或者  deleteByProperty   例如:deleteByAge(int age)  ;
更新(1):@Modifying 
           @Query("update Customer u set u.age = ?1 where u.id = ?2")
           int update(int age1 , long id);
更新(2):@Modifying 
           @Query("update Customer u set u.age = :age where u.id = :id")
           int update(Param("age ")int age1 , Param("id")long id);
 


官网上写出了所有的方法,非常详细,这里稍作列举

And  =>  等价于 SQL 中的 and 关键字 例如:findByUsernameAndPassword(String user, Striang pwd);

Or     =>  等价于 SQL 中的 or 关键字,例如:findByUsernameOrAddress(String user, String addr);

Between   => 等价于 SQL 中的 between 关键字,例如:SalaryBetween(int max, int min);

LessThan  => 等价于 SQL 中的 "<",例如: findBySalaryLessThan(int max);

GreaterThan  => 等价于 SQL 中的">",例如: findBySalaryGreaterThan(int min);

IsNull => 等价于 SQL 中的 "is null",例如: findByUsernameIsNull();

IsNotNull => 等价于 SQL 中的 "is not null",例如: findByUsernameIsNotNull();

NotNull=> 与 IsNotNull 等价;

Like => 等价于 SQL 中的 "like",例如: findByUsernameLike(String user);

NotLike => 等价于 SQL 中的 "not like",例如: findByUsernameNotLike(String user);

OrderBy => 等价于 SQL 中的 "order by",例如: findByUsernameOrderBySalaryAsc(String user);

Not => 等价于 SQL 中的 "! =",例如: findByUsernameNot(String user);

In => 等价于 SQL 中的 "in",例如: findByUsernameIn(Collection<String> userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;

NotIn => 等价于 SQL 中的 "not in",例如: findByUsernameNotIn(Collection<String> userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;

创建一个按单字段排序的Sort对象: new Sort(Sort.Direction.DESC, "description").and(new Sort(Sort.Direction.ASC, "id"))

jpa 复杂查询

http://blog.csdn.net/yingxiake/article/details/51014223

原文地址:https://www.cnblogs.com/xdcr/p/5915847.html