方法1:只需要查询出name和phone两个字段:使用queryWrapper的select()方法指定要查询的字段
public void selectByWrapper1() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name", "phone").eq("age",25);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
这种方法会造成除“name”之外的字段的值为null
方法2:查询出除age和address外其它所有字段的数据:同样使用queryWrapper的select()方法
public void selectByWrapper2() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select(User.class, info -> !info.getColumn().equals("age")
&& !info.getColumn().equals("address")).eq("name","jack");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
这种方法会造成除“age”"address"的字段的值为null
方法3:selectMaps
public void selectMaps2(){
QueryWrapper<User> userQueryWrapper = Wrappers.query();
userQueryWrapper.select("avg(age) avg_age" , "min(age) min_age" , "max(age) max_age")
.groupBy("age")
.having("sum(age) < {0}" , 60);
List<Map<String , Object>> mapList = userMapper.selectMaps(userQueryWrapper);
mapList.forEach(System.out::println);
}