查询数据:
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.eq(User::getName, “zhangsan”);
List list = mapper.selectList(wrapper );
或者:
List users = list(Wrappers.lambdaQuery().eq(User::getUserId, userId));
public List selectList(DTO inDTO) {
List list = dao.selectList(Wrappers.lambdaQuery()
.eq(StringUtil.isNotEmpty(inDTO.getAa()), Entity::getAa, inDTO.getAa())
.eq(StringUtil.isNotEmpty(inDTO.getBb()), Entity::getBb, inDTO.getBb()));
return converter.toListDTO(list);
}
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(UserAggr.class)
.select(UserAggr::getDeptId, UserAggr::getCount)
.groupBy(UserAggr::getDeptId);
List userAggrList = userAggrMapper.selectList(wrapper);
List list = produceService.list(
Wrappers.lambdaQuery().eq(Product::getProductId, product));
获取List中某个字段的列表:
List nameList = userList.stream().map(User::getName).collect(Collectors.toList());// 取出其中一列
判断List中是否有name=“张三"的数据,返回Boolean
Boolean flag = nameList.stream().filter(m->m.getName().equals(”"张三)).findAny().isPresent();
过滤空值
List nameList = userList.stream().map(User::getName).filter(item-> !item.isEmpty()).collect(Collectors.toList());
分组:
// 实际执行 SQL : SELECT id,name,age,gender FROM user GROUP BY id,name
List users1 = userMapper.selectList(new QueryWrapper()
.lambda().groupBy(User::getId, User::getName));
//List 以ID分组 Map<Integer,List>
Map<Integer, List> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
差集 交集 并集
list1 - list
List res = list1.stream().filter(item-> !list.contains(item)).collect(Collectors.toList());
listA 与 listB交集
List intersection = listA.stream().filter(listB::contains).collect(Collectors.toList());
System.out.println("listA和listB交集 = " + intersection);
// listA和listB并集(不去重)
listA.addAll(listB);
System.out.println("listA和listB并集 = " + listA);
// listA和listB并集(去重)
List distinctList = listA.stream().distinct().collect(Collectors.toList());
System.out.println("listA和listB去重并集 = " + distinctList);
list转为map
1.key和value都是对象中的某个属性值。
Map<String, String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
2.key是对象中的某个属性值,value是对象本身(使用返回本身的lambda表达式)。
Map<String, User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId, User -> User));
3.key是对象中的某个属性值,value是对象本身(使用Function.identity()的简洁写法)。
Map<String, User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
4.key是对象中的某个属性值,value是对象本身,当key冲突时选择第二个key值覆盖第一个key值。
Map<String, User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (oldValue, newValue) -> newValue));
查询某条数据是否存在
@Override
public boolean checkUserName(String userName) {
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(User::getUserName, userName);
return this.baseMapper.exists(lambdaQueryWrapper);
}