MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用

场景

项目搭建专栏:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194

基础搭建:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89407994

条件构造器介绍使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89482201

实现

排序前数据库

MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用_第1张图片

orderBy()方法

默认升序。

/***
  *条件构造器 排序操作
  */
 @Test
 public void testEntitymapperOrderBy() {
  
  List employeeList=employeeMapper.selectList(new EntityWrapper()
    .eq("gender",1)
    .like("name", "霸")
    .orderBy("age")
    );
  System.out.println("*******************"+employeeList);
  for (Employee employee : employeeList) {
   System.out.println(employee.getAge());
  }
 }

运行效果

SELECT id AS id,`name`,email,gender,age FROM employee WHERE (gender = ? AND name LIKE ?) ORDER BY age

MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用_第2张图片

orderDesc()方法

注意其参数是一个column的集合。

MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用_第3张图片

代码修改为:

@Test
 public void testEntitymapperOrderBy() {
  
  List employeeList=employeeMapper.selectList(new EntityWrapper()
    .eq("gender",1)
    .like("name", "霸")
    //.orderBy("age")
    .orderDesc(Arrays.asList(new String[] {"age"}))
    );
  System.out.println("*******************"+employeeList);
  for (Employee employee : employeeList) {
   System.out.println(employee.getAge());
  }
 }

运行效果

SELECT id AS id,`name`,email,gender,age FROM employee WHERE (gender = ? AND name LIKE ?) ORDER BY age DESC

MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用_第4张图片

 

orderByAsc()方法

注意其参数是一个column的集合。

升序排列。

代码测试:

@Test
 public void testEntitymapperOrderBy() {
  
  List employeeList=employeeMapper.selectList(new EntityWrapper()
    .eq("gender",1)
    .like("name", "霸")
    //.orderBy("age")
    //.last("desc")
    //.orderDesc(Arrays.asList(new String[] {"age"}))
    .orderAsc(Arrays.asList(new String[] {"age"}))
    );
  System.out.println("*******************"+employeeList);
  for (Employee employee : employeeList) {
   System.out.println(employee.getAge());
  }
 }

运行结果

SELECT id AS id,`name`,email,gender,age FROM employee WHERE (gender = ? AND name LIKE ?) ORDER BY age ASC

MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用_第5张图片

源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11142313

你可能感兴趣的:(MyBatisPlus)