MybatisPlus-条件构造器
Warpper 条件构造器定级接口
我们在使用过程,就是去new QueryWarpper<T>()
与new UpdateWarpper<T>()
,完成相对复杂的sql拼接。
使用方式
1. 使用Mapper
Warpper
接口的作用是提供一定逻辑去拼接sql
语句。通过Dao层去执行sql语句。
代码实现:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
private void queryAllUser() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("userId");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
}
2. 使用serviceImpl 实现类
MybatisPlus
提供了对应的ServiceImpl
辅助工具类,对业务逻辑进行部分封账,完成调用。
代码实现:
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Resource
private UserStrMapper userStrMapper;
private void queryAllUser() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("userId");
// list 方法是serviceImpl 方法
List<User> list = this.list(queryWrapper);
list.forEach(System.out::println);
}
}
QueryWrapper使用例
函数 | 说明 | queryWrapper使用例子 | 对应sql |
---|---|---|---|
eq | 等于(=) | queryWrapper.eq(“user_name”, “张三”); | user_name=“张三” |
ne | 不等于(<>) | queryWrapper.ne(“user_name”, “张三”); | user_name<>“张三” |
gt | 大于(>) | queryWrapper.gt(“level”, 1); | level>1 |
ge | 大于等于(>=) | queryWrapper.ge(“level”, 1); | level>=1 |
lt | 小于(<) | queryWrapper.lt(“level”, 1); | level<1 |
le | 小于等于(<=) | queryWrapper.le(“level”, 1); | level<=1 |
between | 值1和值2之间 | queryWrapper.between(“level”, 1,10); | level>=1 and level <=10 |
notBetween | 不在值1和值2之间 | queryWrapper.notBetween(“level”, 1,10); | level<1 or level >10 |
like | 模糊匹配 like %值% | queryWrapper.like(“user_name”, “张三”); | user_name like “%张三%” |
not like | 不模糊匹配 not like %值% | queryWrapper.notLike(“user_name”, “张三”); | user_name not like “%张三%” |
likeLeft | 左模糊匹配 like %值 | queryWrapper.likeLeft(“user_name”, “张三”); | user_name like “%张三” |
likeRight | 右模糊匹配 like 值% | queryWrapper.likeRight(“user_name”, “张三”); | user_name like “张三%” |
isNull | 字段 为空 | queryWrapper.isNull(“user_name”); | user_name is null |
isNotNull | 字段 不为空 | queryWrapper.isNotNull(“user_name”); | user_name is not null |
in | 字段 in (v0,v1,…) | queryWrapper.in(“user_name”, {“张三”,“李四”,“王五”}); | user_name in (“张三”,“李四”,“王五”) |
notIn | 字段 not in (v0,vl,…) | queryWrapper.notIn(“user_name”, {“张三”,“李四”,“王五”}); | user_name not in (“张三”,“李四”,“王五”) |
inSql | 字段 in ( sql语句) | queryWrapper.in(“user_name”, (select name from student where age< 23)); | user_name in (select name from student where age< 23) |
notInSql | 字段 not in ( sql语句) | queryWrapper.notIn(“user_name”, (select name from student where age< 23)); | user_name not in (select name from student where age< 23) |
groupBy | 分组:GROUP BY 字段 | queryWrapper.groupBy(“user_name”); | group by user_name |
orderByAsc | 排序:ORDER BY 字段 ASC | queryWrapper.orderByAsc(“createTime”); | order by createTime asc |
orderByDesc | 排序:ORDER BY 字段 DESC | queryWrapper.orderByDesc(“createTime”); | order by createTime desc |
orderBy | 排序:ORDER BY 字段 | queryWrapper.orderBy(true, true, “createTime”); | order by createTime asc |
or | 拼接 OR。不调用or则默认为使用and连接 | queryWrapper.eq(“id”, “1”).or().eq(“user_name”, “张三”); | id = 1 or name =‘张三’ |
and | 拼接 AND | queryWrapper.eq(“id”, “1”).and(i->i.eq(“user_name”, “张三”)); | id = 1 and name =‘张三’ |
apply | 拼接 sql。说明: 该方法可用于数据库函数动态入参的params | queryWrapper.eq(“id”, user.getId()).apply(“date_format(‘createTime’,‘%Y-%m-%d’) = {0}”,“2023-07-25”); | date_format(‘createTime’,'%Y-%m-%d) =‘2023-07-25’") |
last | 无视优化规则直接拼接到 sql 的最后。 说明:只能调用一次,多次调用以最后一次为准 有sq1注入的风险,请谨慎使用 |
queryWrapper.last("" limit 1""); | limit 1 |
exists | 拼接 EXISTS(sql语句) | queryWrapper.exists(“SELECT id FROM Student WHERE User.id = Student.userId”); | exists (“SELECT id FROM Student WHERE User.id = Student.userId”) |
notExists | 拼接 NOT EXISTS (sg1语句) | queryWrapper.notExists(“SELECT id FROM Student WHERE User.id = Student.userId”); | not exists (“SELECT id FROM Student WHERE User.id = Student.userId”) |
nested | 正常嵌套不带 AND或者 OR | queryWrapper.nested(i->i.eq(“id”,“1”).eq(“user_name”,“张三”)); | id = 1 and user_name=“张三” |
having | having ( sql语句) | queryWrapper.having(“sum(age) > {0}”,10); | having sum(age) > 10; |
查询
查询用户,name不为空,name模糊查询,age大于值
@Override
public List<UserDto> queryByNameAndAge(String name, Integer age) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", name)
.gt("age", age)
.isNull("name");
List<User> users = this.list(queryWrapper); // mapper.selectList(queryWrapper)
return userStrMapper.toDto(users);
}
排序
查询用户,按name正序,按照age倒序
@Override
public List<UserDto> query() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("name").orderByDesc("age");
List<User> users = this.list(queryWrapper); // mapper.selectList(queryWrapper)
return userStrMapper.toDto(users);
}
删除
删除 年龄小于18 的用户
@Override
public void deleteByAge() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lt("age", "18");
boolean i = this.remove(queryWrapper); // mapper.delete(queryWrapper)
}
组合查询
查询 (name等于张三,并且年龄为18岁)or(name不为空)
@Override
public List<UserDto> queryByNameAndAge() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.and(item -> {
item.like("name", "张三").eq("age", "18");
}).or(item -> {
item.isNull("name");
});
List<User> users = this.list(queryWrapper); // mapper.selectList(queryWrapper)
return userStrMapper.toDto(users);
}
字段查询
查询 name等于张三,并返回 姓名、年龄。并返回为 map
@Override
public List<Map<String,Object>> queryByNam() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "张三").select("name","age");
// 将数据与map以map方式返回
List<Map<String,Object>> listMaps = this.listMaps(queryWrapper); // mapper.selectMap(queryWrapper);
return userStrMapper.toDto(listMaps);
}
子查询
@Override
public List<UserDto> childenQuery(String name) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("name", "select name from sys_user where name is not null");
List<User> users = this.list(queryWrapper); // mapper.selectList(queryWrapper)
return userStrMapper.toDto(users);
}
更新
更新 name等于张三的用户,其年龄改为25
@Override
public void updateUser() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
// set方法相当于 set,eq相当于 where
updateWrapper.set("age", "25")
.eq("name", "张三");
this.update(updateWrapper); // mapper.update(queryWrapper)
}
动态查询语句
@Override
public List<UserDto> query(String name,Integer age) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 如果 true,则执行后面
queryWrapper.like(StringUtils.isNotBlank(name),"name", name)
// 如果年龄不为null且大于0,则执行 age>25
.gt(age != null && age >0, "age", 25);
List<User> users = this.list(queryWrapper);// mapper.selectList(queryWrapper)
return userStrMapper.toDto(users);
}
分页查询
@Override
public List<UserDto> query() {
// 设置每页10条,查询第2页数据
Page<User> pageConfig = new Page<>(2,10);
Page<User> page = this.page(pageConfig); // mapper.selectPage(pageConfig,Wrappers.emptyWrapper())
// 第2页的数据
List<User> records = page.getRecords();
// 总共多少条
long total = page.getTotal();
// 每页多少条
long size = page.getSize();
// 总共几页
long pages = page.getPages();
return userStrMapper.toDto(records);
}