实体类
@Data
public class WqWorkplace implements Serializable {
/**
* id
*/
private Long id;
/**
* 职场
*/
private String workplace;
/**
* 备注
*/
private String remarks;
/**
* 时间
*/
private String createTime;
}
一、Stream
去重
根据集合对象中的某个元素进行去重
- 写法一(去重后为String集合)
List<WqWorkplace> workplaces=new ArrayList<>();
List<String> strings= workplaces.stream().map(WqWorkplace::getWorkplace).distinct().collect(Collectors.toList());
- 写法二(去重后为Object集合)
List<WqWorkplace> workplaces=new ArrayList<>();
List<WqWorkplace> wqWorkplaceList = workplaces.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WqWorkplace::getWorkplace))), ArrayList::new));
- 写法三(set接收)
List<WqWorkplace> stringList=new ArrayList<>();
Set<String> set=stringList.stream().map(f->(f.getWorkplace())).collect(Collectors.toSet());
根据集合对象中的多个元素进行去重
- 写法一
List<WqWorkplace> workplaces=new ArrayList<>();
List<WqWorkplace> wqWorkplaceList = workplaces.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getWorkplace() + "-" + f.getRemarks()))), ArrayList::new));
循环
- 写法一(循环list集合)
List<WqWorkplace> workplaces=new ArrayList<>();
List<String> strings=new ArrayList<>();
workplaces.stream().forEach(f->{
//条件判断
if (StringUtils.isNotBlank(f.getRemarks())){
strings.add(f.getWorkplace());
}
});
- 写法二(循环map集合)
List<String> stringList=new ArrayList<>();
Map<String, WqWorkplace> wqWorkplaceHashMap = new HashMap<>();
wqWorkplaceHashMap.put("key",new WqWorkplace());
wqWorkplaceHashMap.forEach((name, wqWorkplace) -> {
String orginalName = wqWorkplace.getWorkplace();
stringList.add(orginalName);
});
分页
- 写法一(对list集合进行分页为map集合)
List<WqWorkplace> stringList=new ArrayList<>();
Map<String,Object> map = Maps.newHashMap();
//list -> 当前页所有数据 skip页码 limit条数
int pageNo=1;
int pageSize=10;
map.put("list",stringList.stream().skip((long) (pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList()));
//count -> 记录总条数
map.put("count",stringList.size());
二、 Lambda与QueryWrapper
前提条件使用的是mybatis-plus,引入service层
@Autowired
private WqWorkplaceService wqStationService;
查询
- list()方法条件查询
List<WqWorkplace> wqWorkplace =wqStationService.list(new QueryWrapper<WqWorkplace>().lambda()
.nested(f -> f.eq(WqWorkplace::getWorkplace,"查询的值")
.or().like(WqWorkplace::getWorkplace,"查询的值")
)
.apply((ObjectUtil.isNotNull(wqWorkplace.getCreateTime())), "DATE_FORMAT(create_time, '%Y-%m-%d') >= {0}", "查询的值")
.apply((ObjectUtil.isNotNull(wqWorkplace.getCreateTime())), "DATE_FORMAT(create_time, '%Y-%m-%d') <= {0}", "查询的值")
.isNotNull(WqWorkplace::getWorkplace)
.in(WqWorkplace::getId, Arrays.asList(2)));
- getOne()方法查询单条数据
WqWorkplace wqWorkplace =wqStationService.getOne(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值" ).last("LIMIT 1"));
- page()方法分页查询
//页码
int pageIndex=1;
//页数
int pageSize=10;
Page<WqWorkplace> wqWorkplacePage = wqStationService.page(new Page<>(pageIndex , pageSize), new QueryWrapper<WqWorkplace>().lambda()
.eq( WqWorkplace::getWorkplace, "查询的值")
);
- getById()方法根据id查询
WqWorkplace wqWorkplaces = wqStationService.getById(1);
- count()方法条数查询
int count = wqStationService.count(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值"));
修改
- update()方法根据条件修改
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqWorkplace.setWorkplace("沙发沙发");
wqStationService.update(wqWorkplace,new UpdateWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getId,wqWorkplace.getId()).eq(WqWorkplace::getWorkplace,wqWorkplace.getWorkplace()));
- updateById()方法根据数据库主键id修改
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqWorkplace.setWorkplace("岗位1");
wqStationService.updateById(wqWorkplace);
删除
- remove()方法根据条件删除
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqStationService.remove(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getId,wqWorkplace.getId()));
- removeById()方法根据数据库主键id删除
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqStationService.removeById(wqWorkplace);