赞
踩
LambdaQueryWrapper是Mybatis-plus框架中的一个查询构造器,它提供了一种更加简洁、易于理解的Lambda表达式方式来构建查询条件
- // 查询名字为张三的用户
- LambdaQueryWrapper<User> lambdaQuery = new LambdaQueryWrapper<>();
- lambdaQuery.eq(User::getName, "张三");
- List<User> userList = userMapper.selectList(lambdaQuery);
ne:不等于 gt:大于 ge:大于等于 lt:小于 le:小于等于
- // 查询年龄大于等于18岁的用户,查询条件可按需求更换
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.ge(User::getAge, 18);
- List<User> userList = userMapper.selectList(lambdaQuery);
- // 查询名字包含“张”的用户
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.like(User::getName, "张%");
- List<User> userList = userMapper.selectList(lambdaQuery);
- // 查询年龄在18岁到30岁之间的用户(含边界值)
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.between(User::getAge, 18, 30);
- List<User> userList = userMapper.selectList(lambdaQuery);
- // 查询年龄大于等于18岁的用户,或者名字为张三的用户
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.or(user -> user.getAge() >= 18, () -> Wrappers.eq(User::getName, "张三"));
- List<User> userList = userMapper.selectList(lambdaQuery);
- //根据年龄升序排列
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.orderByAsc(User::getAge);
- List<User> userList = userMapper.selectList(lambdaQuery);
- User::getAge表示按照User类中的getAge方法返回值进行排序。如果需要按照其他列进行排序,只需要将User::getAge替换为对应的列名即可
-
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.last(" desc limit 1");
- List<User> userList = userMapper.selectList(lambdaQuery);
-
groupBy :分组
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.groupBy(User::getName);
having:排序
- LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
- lambdaQuery.having(user -> user.getAge() >= 18);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。