11.Springboot 之 使用JPA对数据进行排序
2017-04-18 10:22
417 查看
本文所属【知识林】:http://www.zslin.com/web/article/detail/18
在网站系统的开发过程中经常会用到排序操作,下面就我个人的理解介绍一下使用强大的
pom.xml
application.properties
项目入口
数据实体对象
数据接口
以上这些配置在前面很多测试例子中都已描述过,在此不再做过多赘述。
先初始化10条数据到数据库中,方便后面做排序和分布的操作。
基本测试
注意: 当继承
单排序封装
为更好的使用排序功能,可将
这样封装的好处是:不用每次进行排序时都new个Sort对象作为参数传入;比较方便灵活的根据自己需求传入要排序的方式与字段。
封装后的测试方法:
多排序封装
上面的封装方法只能满足单一排序的需求,如果有多条件排序就无法满足了,下面改进一下封装:
创建Sort的DTO对象
增加basicSort方法
测试方法
上面这个测试方法就是首先按
在网站系统的开发过程中经常会用到排序操作,下面就我个人的理解介绍一下使用强大的
JPA是如何进行排序的。
基础搭建
pom.xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <dependencies> <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>
application.properties
server.port=80 # Hibernate 相关配置 ## 自动扫描的包前缀 entitymanager.packagesToScan= com.zslin ## 数据库连接 spring.datasource.url=jdbc:mysql://localhost:3306/study05?\ useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true ## 用户名 spring.datasource.username=root ## 密码 spring.datasource.password=123 ## 数据库驱动 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; /** * Created by 钟述林 393156105@qq.com on 2016/10/21 10:54. */ @SpringBootApplication public class RootApplication { public static void main(String [] args) { SpringApplication.run(RootApplication.class, args); } }
数据实体对象
package com.zslin.model; import javax.persistence.*; /** * * Created by 钟述林 393156105@qq.com on 2016/10/21 10:55. */ @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; ……省去get和set方法…… }
数据接口
package com.zslin.service; import com.zslin.model.User; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by 钟述林 393156105@qq.com on 2016/10/21 10:56. */ public interface IUserService extends JpaRepository<User, Integer> { }
以上这些配置在前面很多测试例子中都已描述过,在此不再做过多赘述。
数据准备
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; /** * Created by 钟述林 393156105@qq.com on 2016/10/21 11:08. */ @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); } f018 } }
先初始化10条数据到数据库中,方便后面做排序和分布的操作。
排序
基本测试@Test public void testPage() { Sort sort = new Sort(Sort.Direction.DESC, "id"); List<User> list = userService.findAll(sort); for(User u : list) { System.out.println(u.getUserName()); } }
注意: 当继承
JpaRepository之后,
IUserService拥有了几个
findAll()的重载方法,其中一个可以传
Sort对象,该方法则是排序的方法。
Sort sort = new Sort(Sort.Direction.DESC, "id");:表示通过
id进行降序排序,将
DESC换成
ASC则表示升序,将
id换成其他
User对象的属性后,则通过其他字段排序。
单排序封装
为更好的使用排序功能,可将
Sort进行二次封装以解决代码重用的问题。
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; } }
这样封装的好处是:不用每次进行排序时都new个Sort对象作为参数传入;比较方便灵活的根据自己需求传入要排序的方式与字段。
封装后的测试方法:
@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.getUserName()); } }
多排序封装
上面的封装方法只能满足单一排序的需求,如果有多条件排序就无法满足了,下面改进一下封装:
创建Sort的DTO对象
package com.zslin.tools; /** * Created by 钟述林 393156105@qq.com on 2016/10/21 11:41. */ 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"; } }
增加basicSort方法
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; }
测试方法
@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()); } }
上面这个测试方法就是首先按
userName的降序排,再按
id的降序排,在此
basicSort方法中可以传任意个
new SortDto()
相关文章推荐
- Springboot 之 使用JPA对数据进行排序
- SpringBoot开发详解(十) -- 使用JPA访问数据库下篇及使用Page进行数据分页
- Hello hadoop——使用hadoop进行大规模数据的全局排序
- 使用hadoop进行大规模数据的全局排序
- 如何使用 DataView 进行数据排序和检索
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- 大数据之使用hadoop对海量数据进行统计并排序
- 使用hadoop进行大规模数据的全局排序
- 使用自定义Comparator对TreeSet中的数据进行多条件排序
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- 8. 使用JPA保存数据【从零开始学Spring Boot】
- 使用Collections.sort()方法扩展list集合中的数据并对其进行排序
- JPA学习笔记---JPA数据的操作:增加,删除,修改,获取,使用JPQL进行查询
- 使用vim对数据进行排序
- 解决使用HIBERNATE对ORACLE数据库中数据进行排序分页错乱的问题
- 使用hadoop进行大规模数据的全局排序
- jsData 使用教程(六) 对数据进行排序
- (8). 使用JPA保存数据【从零开始学Spring Boot】
- 使用angularjs对数据进行排序筛选
- 使用Hadoop进行大规模数据的全局排序