赞
踩
文章介绍:主要用于条件查询时条件的构造
LambdaQueryWrapper是mybatis plus中的一个条件构造器对象,只是是需要使用Lambda 语法使用 Wrapper
三、代码示例
- public Page<Merchants> shopList(Query info) {
- LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
- //单字段查询
- queryWrapper.eq(User::getState, 0);
- queryWrapper.eq(User::getSmsType, 1);
- //不等于
- queryWrapper.ne(User::getDietTypeId,0);
- // 模糊搜索条件
- if (StringUtils.notEmpty(info.getKeyword())) {
- queryWrapper.and(c -> c.like(User::getMallName, info.getKeyword())
- .or().like(User::getOwnerId, info.getKeyword())
- .or().like(User::getOwnerName, info.getKeyword()));
- }
- // 时间范围查询
- if (StringUtils.notEmpty(info.getCreateTimeStart()) && StringUtils.notEmpty(info.getCreateTimeEnd())) {
- queryWrapper.ge(User::getCreateTime, info.getCreateTimeStart());
- queryWrapper.le(User::getCreateTime, info.getCreateTimeEnd());
- }
- // 不为空
- queryWrapper.isNotNull(User::getStartTime);
- // 排序条件
- if (StringUtils.notEmpty(info.getSort()) && StringUtils.notEmpty(info.getOrder())) {
- // sort ---> "create_time" , order -> desc,asc
- queryWrapper.last(" order by ".concat(info.getSort()).concat(" ").concat(info.getOrder()));
- }
- // 排序(另一种写法)
- if (Func.isEmpty(req.getSortType()) || req.getSortType() == 0) {
- queryWrapper.orderByDesc(User::getTestTime);
- } else {
- queryWrapper.orderByAsc(User::getTestTime);
- }
- //limit
- queryWrapper.last(" limit 1");
- return merchantService.page(new Page<>(info.getPage(), info.getSize()), queryWrapper);
- }
1、过滤示例按条件筛选数据
- 在Java8中对集合的操作可以这样操作
-
- 1.查找集合中的第一个对象
-
- Optional<Object> first= ObjectList.stream() .filter(a -> "hanmeimei".equals(a.getUserName())) .findFirst();
-
- 2.返回符合查询条件的集合
-
- //所有名字叫hanmeimei的集合
-
- List<Object> firstObject= ObjectList.stream() .filter(a -> "hanmeimei".equals(a.getUserName())) .collect(Collectors.toList());
-
- //所有名字为空的集合
-
- List<Object> firstObject= ObjectList.stream() .filter(a -> StringUtils.isEmpty(a.getUserName())) .collect(Collectors.toList());
2、过滤数据大于20的数据
- private List<Integer> integers = Lists.list(30, 40, 10, 20);
- Set<Integer> collect = integers.stream()
- .filter(i -> i > 20).collect(Collectors.toSet());
- assertEquals(Sets.newTreeSet(30, 40), collect);
3、 map 对每个元素执行操作,不会改变原本数量
- private List<Integer> integers = Lists.list(30, 40, 10, 20);
-
- List<String> collect = integers.stream().map(i -> i + "哈哈").collect(Collectors.toList());
-
- assertEquals(Lists.list("30哈哈", "40哈哈", "10哈哈", "20哈哈"), collect);
4、mapToInt 转成IntSream类型转换
- ArrayList<Integer> list = Lists.list(2, 5, 1, 6);
- IntStream intStream = list.stream().mapToInt(i -> i);
- List<Integer> collect = intStream.boxed()
- .collect(Collectors.toList()); // boxed() 把int转成Integer??
- System.out.println(collect);
- // mapToDouble mapToLong 同理
- ArrayList<ArrayList<Integer>> list = Lists.list(Lists.list(1, 2, 3), Lists.list(1, 2, 3));
-
- Stream<Integer> integerStream = list.stream().flatMap(Collection::stream);
- List<Integer> collect = integerStream.collect(Collectors.toList());
- System.out.println(collect);//[1, 2, 3, 1, 2, 3]
-
- // list.stream().flatMapToInt() TODO
6、 distinct 去重
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
- List<Integer> collect = integers.stream().distinct().collect(Collectors.toList());
- System.out.println(collect);//[30, 40, 10, 20, 50]
7、sorted 排序中间40判断可不要
-
- collect = integers.stream().sorted().collect(Collectors.toList());
- System.out.println(collect);// [10, 10, 20, 20, 20, 30, 40, 50]
-
- collect = integers.stream().sorted((x, y) -> {//这里认为40 不吉利 自定义排到最后
- if (y == 40) {
- return -1; // 注意 排序比较时 结果 1排在前,-1排在后
- }else if (x==40){
- return 1;
- }
- return x.compareTo(y);
- }).collect(Collectors.toList());
- System.out.println(collect);// [10, 10, 20, 20, 20, 30, 50, 40]
8、limit 限定流元素数量
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
- List<Integer> collect = integers.stream().limit(3).collect(Collectors.toList());
- System.out.println(collect); // [30, 40, 10]
- List<Integer> q = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
- List<Integer> collect5 = q.stream().skip(2).collect(Collectors.toList());
- System.out.println(collect5); // [10, 20, 20, 10, 50, 20]
10. foreach foreachOrdered 遍历
- Stream<Integer> limit = Stream.iterate(0,x->x+1).limit(100);
- List list = new ArrayList();
-
- // limit.parallel().forEachOrdered(list::add);// [0, 1, 2, 3, 4...
- limit.parallel().forEach(list::add);// [62, 63, 64, 65,...
- System.out.println(list);
a,b,c -> (a+b),c -> (a+b+c) -> result
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
-
- Integer reduce = integers.stream().reduce(0, (x, y) -> x + y);
- Integer integer = integers.stream().reduce((x, y) -> x + y).orElse(-1);
- Integer reduce1 = integers.stream().reduce(0, (x, y) -> x + y,Integer::sum);
- System.out.println(reduce); // 200
- System.out.println(integer); // 200
- System.out.println(reduce1); // 200
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
-
- ArrayList<Object> collect1 = integers.stream()
- .collect(ArrayList::new, ArrayList::add, ArrayList::add);
- // 在combiner使用时,你的Stream是平行的,因为在这种情况下,
- // 多个线程收集的元素Stream到最终输出的子列表ArrayList,
- // 并且这些子列表必须被组合以产生最终的ArrayList。
-
- System.out.println(collect1);// [30, 40, 10, 20, 20, 10, 50, 20]
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
-
- Integer integer1 = integers.stream().min(Integer::compareTo).orElse(-1);
- System.out.println(integer1); //10
- long count = integers.stream().count();
- System.out.println(count);//8
- List<Integer> integers = Lists.list(30, 40, 10, 20, 20, 10, 50, 20);
-
- Integer integer2 = integers.stream().findFirst().orElse(-1);
- System.out.println(integer2);
- Integer integer3 = integers.stream().findAny().orElse(-1);
- System.out.println(integer3);//30
- List<Integer> collect3 = Stream.iterate(0, (x) -> ++x).limit(100).collect(Collectors.toList());
- System.out.println(collect3);
-
- List<Integer> collect4 = Stream.generate(MyStreamTest::get).limit(200)
- .collect(Collectors.toList());
- System.out.println(collect4);
-
-
- private static Integer get() {
- return RandomUtils.nextInt(1, 1000);
- }
如果两个都排好序 那组合的也排好序
- Stream<Integer> limit = Stream.generate(MyStreamTest::get).limit(10).sorted();
- Stream<Integer> limit1 = Stream.generate(MyStreamTest::get).limit(10).sorted();
- List<Integer> collect = Stream.concat(limit, limit1).collect(Collectors.toList());
- System.out.println(collect);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。