关于spring-data-jpa的排序问题
Posted Mr_伍先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于spring-data-jpa的排序问题相关的知识,希望对你有一定的参考价值。
本测试基于springBoot框架实现。
pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaowu</groupId> <artifactId>spring-boot-test-master</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 字符集 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.properties
server.port=80 entitymanager.packagesToScan= com.zslin spring.datasource.url=jdbc:mysql://localhost:3306/demo?/useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
项目入口,启动类:
package com.zslin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @author Administrator * */ @SpringBootApplication public class RootApplication { public static void main(String [] args) { SpringApplication.run(RootApplication.class, args); } }
实体对象:
package com.zslin.model; import javax.persistence.*; /** * * @author Administrator * */ @Entity @Table(name = "t_user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "user_name") private String userName; private String password; @Column(name = "nick_name") private String nickName; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
service接口:
import com.zslin.model.User; import org.springframework.data.jpa.repository.JpaRepository; /** * * @author Administrator * */ public interface IUserService extends JpaRepository<User, Integer> { }
封装SortDto对象
package com.zslin.tools; /** * * @author Administrator * */ public class SortDto { //排序方式 private String orderType; //排序字段 private String orderField; public String getOrderField() { return orderField; } public void setOrderField(String orderField) { this.orderField = orderField; } public String getOrderType() { return orderType; } public void setOrderType(String orderType) { this.orderType = orderType; } public SortDto(String orderType, String orderField) { this.orderType = orderType; this.orderField = orderField; } //默认为DESC排序 public SortDto(String orderField) { this.orderField = orderField; this.orderType = "desc"; } }
封装 SortTools工具类:
package com.zslin.tools; import org.springframework.data.domain.Sort; /** * * @author Administrator * */ public class SortTools { public static Sort basicSort() { return basicSort("desc", "id"); } public static Sort basicSort(String orderType, String orderField) { Sort sort = new Sort(Sort.Direction.fromString(orderType), orderField); return sort; } public static Sort basicSort(SortDto... dtos) { Sort result = null; for(int i=0; i<dtos.length; i++) { SortDto dto = dtos[i]; if(result == null) { result = new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()); } else { result = result.and(new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField())); } } return result; } }
直接上测试类:
package com.zslin; import com.zslin.model.User; import com.zslin.service.IUserService; import com.zslin.tools.SortDto; import com.zslin.tools.SortTools; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("dev") public class UserSortTest { @Autowired private IUserService userService;
//ID 升序 @Test public void testSort() { Sort sort = new Sort(Sort.Direction.ASC, "id"); List<User> list = userService.findAll(sort); for(User u : list) { System.out.println(u.getUserName()); } }
//ID 降序 @Test public void testSort2() { List<User> list = userService.findAll(SortTools.basicSort()); for(User u : list) { System.out.println(u.getUserName()); } }
//用户名 倒序 @Test public void testSort3() { List<User> list = userService.findAll(SortTools.basicSort("desc", "userName")); for(User u : list) { System.out.println(u.getId()+"===="+u.getUserName()); } }
//组合排列 @Test public void testSort4() { List<User> list = userService.findAll(SortTools.basicSort(new SortDto("desc", "userName"), new SortDto("id"))); for(User u : list) { System.out.println(u.getId()+"===="+u.getUserName()); } } }
如果没有测试数据,可以创建一个测试类进行数据的添加:
package com.zslin; import com.zslin.model.User; import com.zslin.service.IUserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * * @author Administrator * */ @SpringBootTest @RunWith(SpringRunner.class) public class UserInitTest { @Autowired private IUserService userService; @Test public void initTest() { for(int i=1; i<=10; i++) { User u = new User(); u.setEmail("email"+i+"@domain.com"); u.setPassword("pwd"+i); u.setUserName("user"+i); u.setNickName("nickname"+i); userService.save(u); } } }
项目结构图:
示例代码(GitHub位置): https://github.com/xiaowu135/spring-boot-test-master.git
以上是关于关于spring-data-jpa的排序问题的主要内容,如果未能解决你的问题,请参考以下文章