一、一对一分页案例
入参bean
@Data
public class RolePageQuery implements Serializable {
/**
* 当前页码
*/
@ApiModelProperty(name = "pageNum",value = "当前页码")
private Integer pageNum;
/**
* 分页大小
*/
@ApiModelProperty(name = "pageSize",value = "分页大小")
private Integer pageSize;
...
}
出参bean
@Data
public class RolePageListDto implements Serializable {
/**
* ID
*/
@ApiModelProperty(name = "id",value = "ID")
private Long id;
/**
* 角色名称
*/
@ApiModelProperty(name = "roleName",value = "角色名称")
private String roleName;
/**
* 备注
*/
@ApiModelProperty(name = "remark",value = "角色描述")
private String remark;
/**
* 状态,0表示禁用,1表示启用
*/
@ApiModelProperty(name = "state",value = "是否激活,false否,true是")
private Boolean state;
/**
* 更新时间
*/
@ApiModelProperty(name = "modifyTimeStamp",value = "更新时间")
private Date modifyTimeStamp;
/**
* 更新人(一对一)
*/
@ApiModelProperty(name = "modifyUser",value = "更新人")
private SimpleUserDto modifyUser;
/**
* 角色类型(一对一)
*/
@ApiModelProperty(name = "roleType",value = "角色类型")
private SimpleRoleTypeDto roleType;
}
业务案例代码
@Override
public IPage<RolePageListDto> companyRolePage(RolePageQuery rolePageQuery) {
SecurityUser currentUser = SpringSecurityUtils.getCurrentSecurityUser();
Assert.notNull(currentUser, "获取当前操作人信息失败!");
Page<RolePageListDto> page = new Page<>(
rolePageQuery.getPageNum() == null ? 1 : rolePageQuery.getPageNum(), rolePageQuery.getPageSize() == null ? 10 : rolePageQuery.getPageSize());
QueryWrapper<RolePageListDto> wrapper = new QueryWrapper();
wrapper.eq(StringUtils.isNotBlank(rolePageQuery.getRoleName()), "r.role_name", rolePageQuery.getRoleName());
wrapper.eq(rolePageQuery.getRoleTypeId()!=null, "r.role_type_id", rolePageQuery.getRoleTypeId());
wrapper.eq(rolePageQuery.getState() != null, "r.state", rolePageQuery.getState());
wrapper.eq( "r.company_id", currentUser.getCompanyId());
wrapper.eq("r.is_deleted",0);
return this.baseMapper.companyRolePage(page, wrapper);
}
public interface SysRoleMapper extends BaseMapper<SysRoleEntity> {
/**
* 分页查询角色信息
* @param page 分页信息
* @param wrapper 查询条件
* @return IPage<RolePageListDto>
*/
IPage<RolePageListDto> companyRolePage(Page<RolePageListDto> page, @Param(Constants.WRAPPER)Wrapper<RolePageListDto> wrapper);
}
XML相关配置,其中一对一使用association
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.SysRoleMapper">
<resultMap id="rolePageListDto" type="com.pojo.dto.RolePageListDto">
<id property="id" column="id"/>
<result property="roleName" column="role_name"/>
<result property="remark" column="remark"/>
<result property="state" column="state"/>
<result property="modifyTimeStamp" column="modify_time_stamp"/>
<association property="modifyUser" javaType="com.pojo.dto.SimpleUserDto" columnPrefix="u_">
<id property="id" column="id"/>
<result property="userName" column="user_name"/>
</association>
<association property="roleType" javaType="com.pojo.dto.SimpleRoleTypeDto" columnPrefix="t_">
<id property="id" column="id"/>
<result property="roleTypeCode" column="role_type_code"/>
<result property="roleTypeName" column="role_type_name"/>
</association>
</resultMap>
<select id="companyRolePage" resultMap="rolePageListDto">
SELECT r.id,r.role_name,r.remark,r.state,r.modify_time_stamp,
u.id u_id,u.user_name u_user_name,t.id t_id,t.role_type_code t_role_type_code,t.role_type_name t_role_type_name
FROM sys_role r
LEFT JOIN sys_user u ON r.modify_user_id=u.id
LEFT JOIN sys_role_type t ON r.role_type_id=t.id
${ew.customSqlSegment}
</select>
</mapper>
一、一对一和一对多混合案例
核心代码
@Override
public List<DeptJobDto> findByDeptId(Long deptId) {
return this.baseMapper.findByDeptId(deptId);
}
其中一对多使用collection
<resultMap id="deptJobDto" type="com.pojo.dto.DeptJobDto">
<id property="id" column="id"/>
<result property="jobName" column="job_name"/>
<result property="remark" column="remark"/>
<result property="state" column="state"/>
<result property="modifyTimeStamp" column="modify_time_stamp"/>
<association property="parentJob" javaType="com.pojo.dto.SimpleJob" columnPrefix="p_">
<id property="id" column="id"/>
<result property="jobName" column="job_name"/>
</association>
<collection property="roleList" ofType="com.pojo.dto.SimpleRoleDto" columnPrefix="r_">
<id property="id" column="id"/>
<result property="roleName" column="role_name"/>
</collection>
</resultMap>
<select id="findByDeptId" parameterType="long" resultMap="deptJobDto">
SELECT dj.id,jc.job_name,jc.remark,jc.state,jc.modify_time_stamp,
jc.parent_id p_id,jp.job_name p_job_name,
r.id r_id,r.role_name r_role_name
FROM sys_dept_job dj
LEFT JOIN sys_job jc ON dj.job_id=jc.id
LEFT JOIN sys_job jp ON jc.parent_id=jp.id
LEFT JOIN sys_job_role jr ON jc.id=jr.job_id
LEFT JOIN sys_role r ON jr.role_id=r.id
WHERE dj.dept_id=#{deptId} AND jr.is_deleted=0
</select>