mybatis plus常用操作

获取指定列示例:

QueryWrapper<DistrictOrg> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("district_id", districtId);
queryWrapper.select("org_id");
List<DistrictOrg> list = districtDistrictOrgMapper.selectList(queryWrapper);
List<Long> orgIds = list.stream().map(DistrictOrg::getOrgId).collect(Collectors.toList());
return orgIds;

group by示例:

queryWrapper.select("org_id as orgId,org_name as orgName,IFNULL(sum(amount),0) as amountTotal");
    queryWrapper.groupBy("org_id");
    List<Map<String, Object>> listMap = this.listMaps(queryWrapper);
    for (Map<String, Object> map : listMap) {
        System.out.println(map.get("orgId"));
        System.out.println(map.get("orgName"));
        System.out.println(BigDecimalUtils.getBigDecimal(map.get("amountTotal")));
    }

apply查询方式示例:

//根据传进来的开始日期,查询所有该日期是数据,但是数据库中保存是时间,所以需要使用apply查询方式并格式化。
queryWrapper.apply("date_format(create_date, '%Y-%m-%d') = {0}", curTime);

count统计示例:

QueryWrapper<Employee> queryWrapper=new QueryWrapper();
queryWrapper.gt("salary",3500).like("name","小");
Integer count = employeeMapper.selectCount(queryWrapper);
System.out.println(count);

查询某列不为空的总数:

QueryWrapper<Org> queryWrapper = Wrappers.query();
queryWrapper.isNotNull("列名");

查询指定列示例:

QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_id", "last_name");
List<Map<String, Object>> listMap = listMaps(queryWrapper);
map.get("user_id");
map.get("last_name");

SUM(AVG)示例:

QueryWrapper<Org> queryWrapper= new QueryWrapper<Org>();
queryWrapper.eq("org_id", orgId);
queryWrapper.select("IFNULL(sum(balance),0) as total ");
Map<String, Object> map = getMap(queryWrapper);
BigDecimal total = new BigDecimal(String.valueOf(map.get("total")));

And,Or一起用

QueryWrapper<Org> queryWrapper= new QueryWrapper<Org>();
queryWrapper.and(wrapper -> wrapper.eq("type", 1).or().eq("type", 2));

LIMIT 取1条

QueryWrapper<Org> queryWrapper= new QueryWrapper<Org>();
queryWrapper.last("limit 1");
上一篇:Oracle组函数通过nvl对Null进行忽略


下一篇:Fetch 别名查找