• Stream流的使用及Lambda表达式与QueryWrapper的配合使用


    实体类

    @Data
    public class WqWorkplace implements Serializable {
        /**
         * id
         */
        private Long id;
        /**
         * 职场
         */
        private String workplace;
        /**
         * 备注
         */
        private String remarks;
        /**
         * 时间
         */
        private String createTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    一、Stream

    去重

    根据集合对象中的某个元素进行去重

    • 写法一(去重后为String集合)
    List<WqWorkplace> workplaces=new ArrayList<>();
    List<String> strings= workplaces.stream().map(WqWorkplace::getWorkplace).distinct().collect(Collectors.toList());        
    
    • 1
    • 2
    • 写法二(去重后为Object集合)
    List<WqWorkplace> workplaces=new ArrayList<>();
    List<WqWorkplace> wqWorkplaceList = workplaces.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WqWorkplace::getWorkplace))), ArrayList::new));
    
    • 1
    • 2
    • 写法三(set接收)
    List<WqWorkplace> stringList=new ArrayList<>();
    Set<String> set=stringList.stream().map(f->(f.getWorkplace())).collect(Collectors.toSet());
    
    • 1
    • 2

    根据集合对象中的多个元素进行去重

    • 写法一
    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));
    
    • 1
    • 2

    循环

    • 写法一(循环list集合)
    List<WqWorkplace> workplaces=new ArrayList<>();
            List<String> strings=new ArrayList<>();
            workplaces.stream().forEach(f->{
                //条件判断
                if (StringUtils.isNotBlank(f.getRemarks())){
                    strings.add(f.getWorkplace());
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 写法二(循环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);
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    分页

    • 写法一(对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());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二、 Lambda与QueryWrapper

    前提条件使用的是mybatis-plus,引入service层

    @Autowired
    private WqWorkplaceService wqStationService;
    
    • 1
    • 2

    查询

    • 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)));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • getOne()方法查询单条数据
    WqWorkplace wqWorkplace =wqStationService.getOne(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值" ).last("LIMIT 1"));
    
    • 1
    • page()方法分页查询
    //页码
    int pageIndex=1;
    //页数
    int pageSize=10;
    Page<WqWorkplace> wqWorkplacePage = wqStationService.page(new Page<>(pageIndex , pageSize), new QueryWrapper<WqWorkplace>().lambda()
            .eq( WqWorkplace::getWorkplace, "查询的值")
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • getById()方法根据id查询
    WqWorkplace wqWorkplaces = wqStationService.getById(1);
    
    • 1
    • count()方法条数查询
    int count = wqStationService.count(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值"));
    
    • 1

    修改

    • 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()));
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • updateById()方法根据数据库主键id修改
    WqWorkplace wqWorkplace=new WqWorkplace();
    wqWorkplace.setId(new Long(1));
    wqWorkplace.setWorkplace("岗位1");
    wqStationService.updateById(wqWorkplace);
    
    • 1
    • 2
    • 3
    • 4

    删除

    • remove()方法根据条件删除
    WqWorkplace wqWorkplace=new WqWorkplace();
    wqWorkplace.setId(new Long(1));
    wqStationService.remove(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getId,wqWorkplace.getId()));
    
    • 1
    • 2
    • 3
    • removeById()方法根据数据库主键id删除
    WqWorkplace wqWorkplace=new WqWorkplace();
    wqWorkplace.setId(new Long(1));
    wqStationService.removeById(wqWorkplace);
    
    • 1
    • 2
    • 3
  • 相关阅读:
    mac安装rust环境
    关于idea2020.2创建springboot项目maven仓库和jdk版本不匹配大坑
    计组--计算机系统概述
    千人优学 | GBase 8s数据库2022年6月大学生专场实训圆满结束
    UI学习-学习内容
    使用 HelpLook Chatbot,让AI聊天机器人变成销售经理
    Spring的bean装配和bean的自动装配
    Sentinel实战
    vue+element如何实现可编辑表格?
    Ubuntu 18.04 + CUDA 11.3.0 + CUDNN 8.2.1 + Anaconda + Pytorch 1.10
  • 原文地址:https://blog.csdn.net/lixuanhong/article/details/128185695