jpa常用的增删查

来源:互联网 发布:mac机械硬盘开机速度 编辑:程序博客网 时间:2024/04/28 00:40
package com.springboot.in.action.daoimport java.util.Listimport com.springboot.in.action.entity.HttpApiimport org.springframework.data.jpa.repository.Queryimport org.springframework.data.repository.CrudRepositoryimport scala.language.implicitConversionstrait HttpApiDao extends CrudRepository[HttpApi, Integer] {
//查询所有数据 def findAll(): List[HttpApi] // JavaConversions//保存 def save(t: HttpApi): HttpApi
//查找根据ID查询 def findOne(id: Integer): HttpApi
//根据实体属性查询
findByProperty(type property)
条件查询  and/or/findByAgeLessThan/LessThanEqual 等, 
总数 查询 count()  或者 根据某个属性的值查询总数countByAge(int age);
删除: 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);
@Query(value = "SELECT * FROM http_api where http_suite_id = ?1", nativeQuery = true) def listByHttpSuiteId(id: Integer): List[HttpApi] @Query(value = "SELECT id FROM http_api where http_suite_id = ?1", nativeQuery = true) def listTestCaseId(httpSuiteId: Integer): List[Integer] // 隐式转换,直接用scala的List会报错:javax.persistence.NonUniqueResultException: result returns more than one elements] with root cause @Query(value = "SELECT * FROM http_api where name like %?1% ", nativeQuery = true) // like '%?%' def findByName(name: String): List[HttpApi] @Query(value = "select count(*) from http_api where http_suite_id = ?1 and state = 1", nativeQuery = true) def countPass(httpSuiteId: Integer): Int @Query(value = "select count(*) from http_api where http_suite_id = ?1 and state = 0", nativeQuery = true) def countFail(httpSuiteId: Integer): Int}
  1. //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex);  
  2.  public List<User> findByHeightAndSex(int height,char sex);  
  3.   
  4. // Or --- 等价于 SQL 中的 or 关键字,比如 findByHeightOrSex(int height,char sex);  
  5.  public List<User> findByHeightOrSex(int height,char sex);  
  6.   
  7.  //Between --- 等价于 SQL 中的 between 关键字,比如 findByHeightBetween(int min, int max);  
  8.  public List<User> findByHeightBetween(int min,int max);  
  9.   
  10.  //LessThan --- 等价于 SQL 中的 "<",比如 findByHeightLessThan(int max);  
  11.  public List<User> findByHeightLessThan(int max);  
  12.   
  13.  //GreaterThan --- 等价于 SQL 中的">",比如 findByHeightGreaterThan(int min);  
  14.  public List<User> findByHeightGreaterThan(int min);  
  15.   
  16.  //IsNull --- 等价于 SQL 中的 "is null",比如 findByNameIsNull();  
  17.  public List<User> findByNameIsNull();  
  18.   
  19.  //IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByNameIsNotNull();  
  20.  public List<User> findByNameIsNotNull();  
  21.   
  22.  //NotNull --- 与 IsNotNull 等价;  
  23.  public List<User> findByNameNotNull();  
  24.   
  25.  //Like --- 等价于 SQL 中的 "like",比如 findByNameLike(String name);  
  26.  public List<User> findByNameLike(String name);  
  27.   
  28.  //NotLike --- 等价于 SQL 中的 "not like",比如 findByNameNotLike(String name);  
  29.  public List<User> findByNameNotLike(String name);  
  30.   
  31.  //OrderBy --- 等价于 SQL 中的 "order by",比如 findByNameNotNullOrderByHeightAsc();  
  32.  public List<User>findByNameNotNullOrderByHeightAsc();  
  33.   
  34.  //Not --- 等价于 SQL 中的 "! =",比如 findByNameNot(String name);  
  35.  public List<User> findByNameNot(String name);  
  36.   
  37.  //In --- 等价于 SQL 中的 "in",比如 findByNameIN(String name);  
  38.  public List<User> findByNameIn(String name);  
  39.   
  40.  //NotIn --- 等价于 SQL 中的 "not in",比如 findByNameNotIN(String name);  
  41.  public List<User> findByNameNotIn(String name);  

jpa 复杂查询

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

原创粉丝点击