MyBatisPlus-查询条件小练习
用户登录(eq匹配)
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.eqgetName, "Jerry").e(User::getPassword, "jerry";
User loginUser = userDao.selectOne(lqw);
System.out.println(loginUser);
购物设定价格区间、户籍设定年龄区间(le ge匹配 或between)
// 范围查询 lt le gt ge eq between
LambdaQueryWrapper<User> lqw = newLambdaQueryWrapper<User>();
// 方案一:设定上限下限
lqw.legetAge, userQuery.getAge()).ge(User::getAge, userQuery.getAge2();
// 方案二:设定范围
lqw.betweengetAge, 10, 20;
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
查信息,搜索新闻(非全文检索:like匹配)
// 模糊匹配 like(%J%) likeLeft(%J) likeRight(J%)
LambdaQueryWrapper<User> lqw = newLambdaQueryWrapper<User>();
lqw.likeRightgetName, "J";
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
统计报表(分组查询聚合函数)
QueryWrapper<User> lqw = new QueryWrapper<User>();
// 对数据按性别分组,并统计
lqw.select("count(*) as count, gender");
lqw.groupBy("gender");
List<Map<String, Object>> userList = userDao.selectMaps(lqw);
System.out.println(userList);