package com.springboot.in.action.dao import java.util.List import com.springboot.in.action.entity.HttpApi import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.CrudRepository import scala.language.implicitConversions trait 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 }
- //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex);
- public List<User> findByHeightAndSex(int height,char sex);
- // Or --- 等价于 SQL 中的 or 关键字,比如 findByHeightOrSex(int height,char sex);
- public List<User> findByHeightOrSex(int height,char sex);
- //Between --- 等价于 SQL 中的 between 关键字,比如 findByHeightBetween(int min, int max);
- public List<User> findByHeightBetween(int min,int max);
- //LessThan --- 等价于 SQL 中的 "<",比如 findByHeightLessThan(int max);
- public List<User> findByHeightLessThan(int max);
- //GreaterThan --- 等价于 SQL 中的">",比如 findByHeightGreaterThan(int min);
- public List<User> findByHeightGreaterThan(int min);
- //IsNull --- 等价于 SQL 中的 "is null",比如 findByNameIsNull();
- public List<User> findByNameIsNull();
- //IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByNameIsNotNull();
- public List<User> findByNameIsNotNull();
- //NotNull --- 与 IsNotNull 等价;
- public List<User> findByNameNotNull();
- //Like --- 等价于 SQL 中的 "like",比如 findByNameLike(String name);
- public List<User> findByNameLike(String name);
- //NotLike --- 等价于 SQL 中的 "not like",比如 findByNameNotLike(String name);
- public List<User> findByNameNotLike(String name);
- //OrderBy --- 等价于 SQL 中的 "order by",比如 findByNameNotNullOrderByHeightAsc();
- public List<User>findByNameNotNullOrderByHeightAsc();
- //Not --- 等价于 SQL 中的 "! =",比如 findByNameNot(String name);
- public List<User> findByNameNot(String name);
- //In --- 等价于 SQL 中的 "in",比如 findByNameIN(String name);
- public List<User> findByNameIn(String name);
- //NotIn --- 等价于 SQL 中的 "not in",比如 findByNameNotIN(String name);
- public List<User> findByNameNotIn(String name);
jpa 复杂查询
http://blog.csdn.net/yingxiake/article/details/51014223