当我们需要关联表格分页查询时,MyBatis_plus封装的单表方法已经满足不了我们的需求了,那么我们需要进行联表分页查询
假设我们需要的 SQL 语句如下:
SELECT
su.id,
su.username,
su.sex,
su.user_identity,
su.user_company,
su.status,
su.third_type,
su.telephone,
su.avatar,
su.email,
su.realname,
su.post,
su.del_flag,
su.create_time,
sr.role_name
FROM
sys_user AS su
LEFT JOIN sys_user_role AS sur ON su.id = sur.user_id
LEFT JOIN sys_role AS sr ON sur.role_id = sr.id
order by su.create_time desc
那么我们需要进行如下操作:
UserInfoVo实际上是一个页面数据对象,由于页面上需要显示用户表的数据还需要根据用户去查询另一张表中的角色名称,所以UserInfoVO类似构造了一个MyBatis中的result,在MP中我们可以使用IPage
import lombok.Data;
@Data
public class UserInfoVO extends UserInfo {
/**
* id
*/
@TableId(type = IdType.ASSIGN_ID)
private String id;
/**
* 登录账号
*/
@Excel(name = "登录账号", width = 15)
private String username;
/**
* 真实姓名
*/
@Excel(name = "真实姓名", width = 15)
private String realname;
/**
* 密码
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
/**
* md5密码盐
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String salt;
/**
* 头像
*/
@Excel(name = "头像", width = 15, type = 2)
private String avatar;
/**
* 性别(1:男 2:女)
*/
@Excel(name = "性别", width = 15, dicCode = "sex")
@Dict(dicCode = "sex")
/**
* 电子邮件
*/
@Excel(name = "电子邮件", width = 15)
private String email;
/**
* 电话
*/
@Excel(name = "电话", width = 15)
private String phone;
/**
* 删除状态(0,正常,1已删除)
*/
@TableLogic
@Excel(name = "删除状态", width = 15, dicCode = "del_flag")
private Integer delFlag;
//其他表的数据
@TableField(exist = false)
private String roleName;
}
123456789
Constants.WRAPPER:在MP官网的解释是:根据entity条件查询记录,所以
IPage getUserList(@Param("username") String username, @Param("realname") String realname, @Param("status") Integer status, @Param("page") Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
}
${ew.customSqlSegment}:表示将自定义的SQL代码包裹,使用QueryWrapper(LambdaQueryWrapper)进行输出。
<select id="getUserList" resultType="org.jeecg.modules.system.entity.SysUser">
SELECT
su.id,
su.username,
su.sex,
su.user_company,
su.status,
su.telephone,
su.avatar,
su.email,
su.realname,s
su.del_flag,
su.create_time,
sr.role_name
FROM
sys_user AS su
LEFT JOIN sys_user_role AS sur ON su.id = sur.user_id
LEFT JOIN sys_role AS sr ON sur.role_id = sr.id
<where>
su.open_id is null and del_flag = 0
<if test="username !=null and username!=''">
and su.username like concat('%',#{username},'%')
</if>
<if test="realname !=null and realname!=''">
and su.realname like concat('%',#{realname},'%')
</if>
<if test="status != null and status!=''">
and su.status like concat('%',#{status}'%')
</if>
</where>
order by su.create_time desc
${ew.customSqlSegment}
</select>
@Override
public IPage<UserInfoVO> getUserList(String username, String realname, Integer status, Page<UserInfoVO> page, QueryWrapper<UserInfoVO> queryWrapper) {
return userMapper.getUserList(username, realname, status, page, queryWrapper);
}
由此可见,serviceImpl返回的是一个IPage
QueryWrapper<UserInfoVO> queryWrapper = new QueryWrapper<>();
Page<UserInfoVO> page = new Page<UserInfoVO>(pageNo, pageSize);
IPage<UserInfoVO> userPageList = sysUserService.getUserList(sysUser.getUsername(), sysUser.getRealname(), sysUser.getStatus(), page, queryWrapper);
return Result.OK(userPageList);
以上就是分页查询(联表)时的操作,使用拼写SQL的方式实现多表联合分页查询,是效率最高的一种。