0
点赞
收藏
分享

微信扫一扫

MyBatis-Plus分页插件返回自定义类型

罗子僧 2022-02-18 阅读 81

MyBatis-Plus分页插件返回自定义类型

本人也是刚开始使用MyBatis plus正好项目上使用到了,然后再使用分页的时候,突然发现返回的类型并不是自己想要的VO而是返回的PO,在一番查找后,发现了返回自己想要的类型的方法。不多说了,上代码。

实体类

public class Organization implements Serializable{

  @ApiModelProperty(value = "主键ID")
  @TableId(value = "id", type = IdType.AUTO)
  private Integer id;

  @ApiModelProperty(value = "组织名称")
  private String organizationName;

  @ApiModelProperty(value = "父组织ID")
  private Integer pid;

  @ApiModelProperty(value = "排序值")
  private Integer orderLine;

  @ApiModelProperty(value = "逻辑删除值")
  private Integer isValid;

  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  @ApiModelProperty(value = "创建时间")
  private LocalDateTime createTime;

  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  @ApiModelProperty(value = "更新时间")
  private LocalDateTime updateTime;

  @ApiModelProperty(value = "创建用户ID")
  private Integer creator;

  @ApiModelProperty(value = "更新用户ID")
  private Integer updater;
}

返回到前端的Vo

public class OrganizationVo implements Serializable{

  @ApiModelProperty(value = "主键ID")
  @TableId(value = "id", type = IdType.AUTO)
  private Integer id;

  @ApiModelProperty(value = "组织名称")
  private String organizationName;

  @ApiModelProperty(value = "父组织名称")
  private String porganizationName;

  @ApiModelProperty(value = "排序值")
  private Integer orderLine;

  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  @ApiModelProperty(value = "创建时间")
  private LocalDateTime createTime;

  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  @ApiModelProperty(value = "更新时间")
  private LocalDateTime updateTime;

  @ApiModelProperty(value = "创建用户ID")
  private Integer creator;

  @ApiModelProperty(value = "更新用户ID")
  private Integer updater;
}

首先,先定义我们的Mapper

public interface OrganizationMapper extends BaseMapper<Organization>{
    /**
     * 分页查询组织信息列表
     * @return
     */
    //原型IPage<T> queryOrganization(IPage<T> page,查询条件);
    IPage<OrganizationVo> queryOrganization(IPage<OrganizationVo> page,OrganizationDTO organizationDTO);
}

然后再去写Mapper映射文件里写sql

<select id="queryOrganization" resultType="com.szsl2011.slcsp.system.vo.OrganizationVo">
        select oa.id,
        oa.organization_name,
        ob.organization_name as porganizationName,
        oa.order_line,
        oa.create_time,
        oa.update_time,
        oa.creator,
        oa.updater
        from system_organization oa left join system_organization ob
        on oa.pid = ob.id
        <where>
            oa.is_valid != 1
        </where>
        order by order_line asc
    </select>

剩下的就是再我们的服务类接口和其实现类中就行接口的定义和接口的实现了。

public interface IUserOrganizationService extends IService<UserOrganization> {
    /**
     * 分页查询用户组织联系
     * @param userId
     * @param current
     * @param size
     * @return
     */
    IPage<UserOrganizationVO> queryUserOrganizationPage(Integer userId,Integer current,Integer size);
}
@Service
public class UserOrganizationServiceImpl extends ServiceImpl<UserOrganizationMapper,UserOrganization> implements IUserOrganizationService {
    
    @Autowired
    private UserOrganizationMapper userOrganizationMapper;
    
     /**
     * 分页查询用户组织联系
     * @param userId
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<UserOrganizationVO> queryUserOrganizationPage(Integer userId, Integer current, Integer size) {
        //获取page
        IPage<UserOrganizationVO> page = new Page<>(current,size);
        //调用mapper的方法
        IPage<UserOrganizationVO> userOrganizationVOIPage = userOrganizationMapper.queryUserOrganizationPage(page,userId);
        
        return userOrganizationVOIPage;
    }
}

最后再控制器里进行调用

public class OrganizationController {
        @Autowired
    private IOrganizationService organizationService;
    
    public ResponseResult<IPage<OrganizationVo>> listOrganization(
            @ApiParam(name = "current",value = "当前页",required = true) Integer current,
            @ApiParam(name = "size",value = "行数",required = true) Integer size){
        return ResponseResult.success(organizationService.selcteOrganization(current,size));
    }
}

最后的最后查询结果
在这里插入图片描述

举报

相关推荐

0 条评论