QueryWrapper<Source> queryWrapper = new QueryWrapper();
if (StrUtil.isNotEmpty(filter)) {
queryWrapper.or(i->i.like("name", filter).or().like("description", filter));
}
if (StrUtil.isNotEmpty(catalog)) {
queryWrapper.eq("catalog_id", catalog);
}
//根据id查询
User user = userMapper.selectById(1);
System.out.println("根据id查询" + user);
//普通查询
System.out.println("----- 普通查询 ------");
List<User> plainUsers = userMapper.selectList(new QueryWrapper<User>().eq("role_id", 2L).orderByAsc("age"));
List<User> lambdaUsers = userMapper.selectList(new QueryWrapper<User>().lambda().eq(User::getRoleId, 2L));
System.out.println("查询默认未删除 deleted=0" + plainUsers);
//子查询
System.out.println("----- 子查询 ------");
List<User> plainUsers2 = userMapper.selectList(new QueryWrapper<User>()
.inSql("role_id", "select id from role where id = 2"));
System.out.println("子查询"+plainUsers2);
List<User> lambdaUsers2 = userMapper.selectList(new QueryWrapper<User>().lambda()
.inSql(User::getRoleId, "select id from role where id = 2"));
System.out.println("子查询"+lambdaUsers2);
//带嵌套查询
System.out.println("----- 带嵌套查询 ------");
List<User> plainUsers3 = userMapper.selectList(new QueryWrapper<User>()
.nested(i -> i.eq("role_id", 2L).or().eq("role_id", 3L))
.and(i -> i.ge("age", 20)
));
List<User> lambdaUsers3 = userMapper.selectList(new QueryWrapper<User>().lambda()
.nested(i -> i.eq(User::getRoleId, 2L).or().eq(User::getRoleId, 3L))
.and(i -> i.ge(User::getAge, 20)));
System.out.println("带嵌套查询"+plainUsers3);
System.out.println("带嵌套查询"+lambdaUsers3);
//自定义(sql注入)
System.out.println("----- 自定义(sql注入) ------");
List<User> plainUsers4 = userMapper.selectList(new QueryWrapper<User>()
.apply("role_id = 2"));