欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-03 09:04:36
...

场景

项目搭建专栏:

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使用MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用

orderBy()方法

默认升序。

/***
  *条件构造器 排序操作
  */
 @Test
 public void testEntitymapperOrderBy() {
  
  List<Employee> employeeList=employeeMapper.selectList(new EntityWrapper<Employee>()
    .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使用MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用

orderDesc()方法

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

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

代码修改为:

@Test
 public void testEntitymapperOrderBy() {
  
  List<Employee> employeeList=employeeMapper.selectList(new EntityWrapper<Employee>()
    .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使用MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用

 

orderByAsc()方法

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

升序排列。

代码测试:

@Test
 public void testEntitymapperOrderBy() {
  
  List<Employee> employeeList=employeeMapper.selectList(new EntityWrapper<Employee>()
    .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使用MyBatisPlus条件构造器带条件排序方法orderBy、orderByDesc、orderByAsc使用

源码下载

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