Spring-Data-JPA @Query注解 Sort排序

当我们使用方法名称很难,达到预期的查询结果,就可以使用@Query进行查询,@Query是一种添加自定义查询的便利方式

方法名称查询见http://blog.csdn.net/niugang0920/article/details/79426521

使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQL的语言.

1.User.java 实体类

  1. package com.niugang.entity;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. //javax.persistence.Entity
  9. @Entity
  10. @Table(name="user")
  11. public class User {
  12. /**
  13. *mysql 自定生成主键
  14. *指示持久性提供者必须分配使用数据库标识列的实体的主键
  15. */
  16. @Id
  17. @GeneratedValue(strategy = GenerationType.IDENTITY)  
  18. @Column(name="id" ,insertable=false,updatable=false)
  19. private Integer id;
  20. /**
  21. * @Column中的name默认和字段名相同
  22. */
  23. @Column(name = "name")
  24. private String name;
  25. @Column(name = "age")
  26. private Integer age;
  27. @Column(name = "phone")
  28. private String phone;
  29. @Column(name = "password")
  30.  
  31. private String password;
  32.  
  33.         public String getPassword() {
  34. return password;
  35. }
  36. public void setPassword(String password) {
  37. this.password = password;
  38. }
  39. public Integer getId() {
  40. return id;
  41. }
  42. public void setId(Integer id) {
  43. this.id = id;
  44. }
  45. public String getName() {
  46. return name;
  47. }
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51. public Integer getAge() {
  52. return age;
  53. }
  54. public void setAge(Integer age) {
  55. this.age = age;
  56. }
  57. public String getPhone() {
  58. return phone;
  59. }
  60. public void setPhone(String phone) {
  61. this.phone = phone;
  62. }
  63. @Override
  64. public String toString() {
  65. return "User [id=" + id + ", name=" + name + ", age=" + age + ", phone=" + phone + ", password=" + password
  66. + "]";
  67. }
  68.  
  69. }

2.UserDao.java

  1. package com.niugang.dao;
  2. import java.util.List;
  3. import org.springframework.data.domain.Page;
  4. import org.springframework.data.domain.Pageable;
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6. import org.springframework.data.jpa.repository.Query;
  7. import org.springframework.data.repository.query.Param;
  8. import org.springframework.stereotype.Repository;
  9. import com.niugang.entity.User;
  10. /**
  11.  * JpaRepository springboot已经自动配置了,也已经注入数据源了
  12.  * 
  13.  * @author niugang
  14.  *
  15.  */
  16. @Repository
  17. public interface UserDao extends JpaRepository<User, Integer> {
  18. /**
  19. * ?加数字表示占位符,?1代表在方法参数里的第一个参数,区别于其他的index,这里从1开始
  20. */
  21. // select * from User where name = ?1 注意不能写成*
  22. @Query(value = "select u from  User u where u.name = ?1")
  23. User findUserByName1(String name);
  24.  
  25.  
  26. /**
  27. * =:加上变量名,这里是与方法参数中有@Param的值匹配的,而不是与实际参数匹配的 和上面第一个原理是一样的
  28. */
  29. @Query(value = "select u from  User u where u.name = :name")
  30.  
  31. User findUserByName2(@Param("name") String username);
  32.  
  33.        /**
  34. * 通过原生sql 进行查询
  35. * 开启nativeQuery=true,在value里可以用原生SQL语句完成查询
  36. */
  37. @Query(nativeQuery = true, value = "select * from  user u where u.name = :name")
  38. User findUserByNativeSQL(@Param("name") String username);
  39.  
  40.  
  41. /**
  42. * 模糊查询 这里的%只能放在占位的前面,后面不行
  43. */
  44. @Query(value = "select u from  User u where u.name like  %?1% ")
  45. List findUserLike(String name);
  46.  
  47.  
  48. /**
  49. * 调条件分页查询 Pageable:spring-data-jpa自带接口
  50. * @param name
  51. *            :查询条件name
  52. * @param page:分页对象
  53. * @return
  54. */
  55. @Query(value = "select u from  User u where u.name like  %?1% ")
  56.  
  57. Page findUserLikeByPage(String name, Pageable page);
  58.  
  59.      /**
  60. * @Query查询不支持,排序并且分页
  61. * 源码分析
  62. * if (hasParameterOfType(method, Pageable.class)) {
  63.  
  64.  
  65. if (!isStreamQuery()) {
  66. assertReturnTypeAssignable(method, Slice.class, Page.class, List.class);
  67. }
  68.  
  69.  
  70. if (hasParameterOfType(method, Sort.class)) {
  71. throw new IllegalStateException(String.format("Method must not have Pageable *and* Sort parameter. "
  72. + "Use sorting capabilities on Pageble instead! Offending method: %s", method.toString()));
  73. }
  74. }
  75. * @param sort :排序对象 org.springframework.data.domain.Sort
  76. * @return
  77. */
  78. @Query(value = "select u from  User u")
  79. List findUserLikeBySort(Sort sort);
  80.  
  81. }

3.   findUserLikeByPage(String name, Pageable page)接口解释:

  1.   public Page findUserByPage(String name) {
  2.   PageRequest pageRequest = new PageRequest(0, 4);
  3.   return userDao.findUserLikeByPage(name, pageRequest);
  4.  
  5. }

4.排序测试代码

  1. /**
  2. * 单条件排序
  3. */
  4. public List findListSortSingleCondition(){
  5. //id升序查询
  6. Sort sort = new Sort(Sort.Direction.ASC,"id");
  7. return userDao.findUserLikeBySort(sort);
  8. }
  9. /**
  10. * 多条件排序
  11. */
  12. public List findListSortMultiCondition(){
  13. List orders= new ArrayList();
  14. Order orderId = new Sort.Order(Sort.Direction.DESC,"id");
  15. Order orderAge = new Sort.Order(Sort.Direction.DESC,"age");
  16. orders.add(orderId);
  17. orders.add(orderAge);
  18. Sort sort = new Sort(orders);
  19. return userDao.findUserLikeBySort(sort);
  20. }

你可能感兴趣的:(Spring-Data-JPA @Query注解 Sort排序)