常用写法
xxxDO=实体类, 相当于:
select name, id from xxx group by name where delete_flag = 0 and status = 0
LambdaQueryWrapper<xxxDO> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.select(xxxDO::getName, DO::getId);
queryWrapper.eq(xxxDO::getDeleteFlag, 0);
queryWrapper.eq(xxxDO::getStatus, 0);
queryWrapper.groupBy(xxxDO::getName);
List<xxxDO> list = getBaseMapper().selectList(queryWrapper);
自定义sql
xxxDO=实体类,相当于:
select sex, ifnull(count(id), 0) AS num, CASE WHEN sex = 1 THEN '男' WHEN sex = 0 THEN '女' ELSE '未知' END AS sexStr from xxx group by sex?where?status = 0
LambdaQueryWrapper<xxxDO> queryWrapper = new QueryWrapper<xxxDO>()
.select("sex, ifnull(count(id), 0) AS num, CASE WHEN sex = 1 THEN '男' WHEN sex = 0 THEN '女' ELSE '未知' END AS sexStr").lambda();
queryWrapper.eq(xxxDO::getStatus, 0);
queryWrapper.groupBy(xxxDO::getSex);
List<Map<String, Object>> mapList = getBaseMapper().selectMaps(queryWrapper);
Map<String, String> registerNumMap = new HashMap<>(mapList.size());
for (Map<String, Object> map : mapList) {
String key = String.valueOf(map.get("sexStr"));
String num = String.valueOf(map.get("num"));
registerNumMap.put(key, num);
}
返回结果是一个list, 里面的map key是查询的属性名, value是参数值。伪代码示意:
list[0]
? ? ? ? map[0] (sex=1)
????????map[1] (num=5)
????????map[2] (sexStr=男)
list[1]
? ? ? ? map[0] (sex=1)
????????map[1] (num=5)
????????map[2] (sexStr=男)
偶然而发, 记录一下
?
?
|