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
实现
排序前数据库
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
orderDesc()方法
注意其参数是一个column的集合。
代码修改为:
@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
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
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11142313
上一篇: 教我徒弟Android开发入门(二)
下一篇: Oracle物化视图详解