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

实现
排序前数据库

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


orderDesc()方法
注意其参数是一个column的集合。

代码修改为:

@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

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


源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11142313
 

你可能感兴趣的:(mybaits-plus,mybaits-plus)