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());