mybaties-plus 自定义分页查询,使用条件构造器

场景

分页查询某个角色所关联的用户信息。

map层

UserMapper
IPage getByRoleId(Long roleId, IPage page, @Param(Constants.WRAPPER) QueryWrapper queryWrapper);
UserMapper.xml
  

测试

1.不带wrapper参数查询

==>  Preparing: select * from user u inner join user_role ur on u.id = ur.user_id and ur.role_id = ? LIMIT ?
==> Parameters: 2(Long), 10(Long)

2.带wrapper参数查询

==>  Preparing: select * from user u inner join user_role ur on u.id = ur.user_id and ur.role_id = ? and (status = ?) LIMIT ?
==> Parameters: 2(Long), 1(Integer), 10(Long)

其他补充

  • Constants.WRAPPER是mybaties定义的常量"ew"
  • ${ew.customSqlSegment} 会在外层套where标签,而 ${ew.sqlSegment}不会,所以使用ew.sqlSegment
  • 因为wrapper可能是空的 所以要if test="ew.sqlSegment!=null and ew.sqlSegment!=''"判断一下。否则会导致sql错误,末尾多个and

你可能感兴趣的:(后端,mybaties,java)