select作用:选取指定的数据库字段,避免查询冗余的字段。
下面简单叙述一下select的用法;
1、查询除了指定字段以外的所有字段
方法:QueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)
@GetMapping(value = "/test")
public List<Animal> test(Animal arg) {
QueryWrapper<Animal> wrapper = (arg)
.select(, item -> ! ().equals("age"));
return (wrapper);
}
控制台打印的sql语句如下:
SELECT
id,
create_time,
NAME,
update_time,
del_flag,
hobby
FROM
animal
WHERE
del_flag =0
2、查询指定字段
方法:QueryWrapper<T> select(String... columns)
@GetMapping(value = "/test")
public List<Animal> test(Animal arg) {
QueryWrapper<Animal> wrapper = (arg)
.select("id", "name", "hobby");
return (wrapper);
}
控制台打印的sql语句如下:
SELECT id,name,hobby FROM animal WHERE del_flag=0
3、结合聚合函数查询
方法:QueryWrapper<T> select(String... columns)
查询各个部门员工的平均年龄,然后按照部门id降序排序
@GetMapping(value = "/test")
public List<Animal> test(Animal arg) {
QueryWrapper<Animal> wrapper = (arg)
.select("dept_id", "AVG(age) AS avgAge")
.groupBy("dept_id")
.orderByDesc("dept_id");
return (wrapper);
}
控制台打印的sql语句如下:
SELECT
dept_id,
AVG( age ) AS avgAge
FROM
animal
WHERE
del_flag = 0
GROUP BY
dept_id
ORDER BY
dept_id DESC