MyBatis-Plus条件构造器select方法返回指定字段
条件构造器select用法
1.返回特定的几个字段 select(字段…)
2.排除某几个字段 select(entiyClass,predicate判断)
3.分组聚合函数 select(“聚合函数”)
搭配service的.listMaps方法,将返回的数据封装到map中
避免了多余字段的返回
实例
实例一:
public AjaxResult listAll(ProjectBuild projectBuild) {
//只返回了id和name两个字段
List<Map<String,Object>> list = this.iProjectBuildService.listMaps(
WrAppers.lambdaQuery(projectBuild).select(ProjectBuild::getBuildId,ProjectBuild::getBuildName).orderByAsc(ProjectBuild::getBuildId)
);
return AjaxResult.success(list);
}
示例2
@Test
public void selectByQueryWrapper8(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper();
queryWrapper.lambda()
.select(Employee.class,fieldInfo->!fieldInfo.getColumn().equals("birthday")&&!fieldInfo.getColumn().equals("gender"))
.gt(Employee::getSalary,3500)
.like(Employee::getName,"小");
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
System.out.println(employeeList);
}
示例3
期望的效果是:
SELECT departmentId,AVG(salary) AS avg_salary FROM t_employee GROUP BY department_id;
如下实现
@Test
public void selectByQueryWrapper9(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper();
queryWrapper.lambda()
.select(Employee::getDepartmentId,"AVG(salary) AS avg_salary")
.groupBy(Employee::getDepartmentId);
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
System.out.println(employeeList);
}
问题解决
问题描述: map中的key没有使用驼峰命名
解决方法: 使用hutool工具包中的 MapUtil.toCamelCaseMap(map);
//处理map中key未驼峰命名
list = list.stream().map(MapUtil::toCamelCaseMap).collect(Collectors.toList());