Mybatis下collections使用pageHelper进行分页

Posted SimonHu1993

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis下collections使用pageHelper进行分页相关的知识,希望对你有一定的参考价值。

pageHelper在对mybatis一对多分页时造成查询总页数结果不对的情况。

 

可以做出如下修改:

service层:


public CommonResult worksList(String userId, int page, int pageSize) throws Exception  {
PageHelper.startPage(page, pageSize);
List<DesignDTO1> dtos = designMapper.worksList(userId);
for (DesignDTO1 dto : dtos) {
checkDesignerEdit(dto);
}
PageInfo p = new PageInfo(dtos);
Map map = new HashMap();
map.put("rows", p.getList());
map.put("totalCount", p.getTotal());

return CommonResult.success(map);
}

xml配置文件

<resultMap id="ResultMapDesignDTO1" type="com.zhx.web.design.model.DesignDTO1">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="works_id" property="worksId" jdbcType="VARCHAR"/>
        <result column="works_name" property="worksName" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="work_status" property="workStatus" jdbcType="INTEGER"/>
        <result column="status_msg" property="statusMsg" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="works_img_cover" property="workCoverImg" jdbcType="VARCHAR"/>
        <association property="userInfo" javaType="com.zhx.web.design.model.UserDTO">
            <result column="user_id" property="userId" jdbcType="VARCHAR" />
            <result column="real_name" property="realName" jdbcType="VARCHAR" />
            <result column="user_email" property="userEmail" jdbcType="VARCHAR" />
            <result column="unit_name" property="unitName" jdbcType="VARCHAR" />
            <result column="user_phone" property="userPhone" jdbcType="VARCHAR" />
        </association>
        <collection property="worksLog" ofType="com.zhx.web.design.model.WorksLog"
                    column="works_id"//这个参数会作为子查询的参数,需要和实体的属性参数一致
                    select="com.zhx.web.design.mapper.DesignMapper.getWorksLogs">
        </collection>
    </resultMap>

 <select id="getWorksLogs" resultMap="WorksLogs" parameterType="java.lang.String">
        SELECT oper_time,memo
        FROM works_log where  works_id =#{worksId}//这里用works_id好像也没问题……测试了几次,用错误参数也能传递过来,
就是个占位符,实际参数会从
collection中的column中传递过来
</select> <!--查询用户上传作品列表--> <select id="worksList" resultMap="ResultMapDesignDTO1" parameterType="java.lang.String"> SELECT ui.user_id,ui.real_name,ui.user_email,ui.unit_name,ui.user_phone,uw.id,uw.works_id ,uw.works_name, uw.create_time ,uw.status_show as status,uw.status as work_status,uw.works_img_cover, <include refid="showStatusMsg"></include> from user_works uw ,user_info ui where uw.user_id = ui.user_id and uw.user_id = #{userId} </select>

实体类

package com.zhx.web.design.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.Date;
import java.util.List;
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
public class DesignDTO1 {
    /**
     * 作品联系人信息
     */
    private UserDTO userInfo;

    private Integer id;
    /**
     * 作品编号
     */
    private String worksId;
    /**
     * 作品名称
     */
    private String worksName;

    /**
     * 上传时间
     */
    @JsonFormat(pattern="yyyy年MM月dd日")
    private Date createTime;

    /**
     * 状态status_show
     */
    private Integer status;
    /**
     * 状态描述
     */
    private String statusMsg;
    /**
     * 作品封面图(图片名称)
     */
    private String workCoverImg;

    /**
     * 是否可修改
     */
    private Integer edit;
    /**
     * 可修改剩余时间
     */
    private  Long edtiTime;

    /**
     * 业务状态status
     */
    private Integer workStatus;
    /**
     * 作品审核信息
     */
    private List<WorksLog> worksLog;

    public UserDTO getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserDTO userInfo) {
        this.userInfo = userInfo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getWorksId() {
        return worksId;
    }

    public void setWorksId(String worksId) {
        this.worksId = worksId;
    }

    public String getWorksName() {
        return worksName;
    }

    public void setWorksName(String worksName) {
        this.worksName = worksName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public String getWorkCoverImg() {
        return workCoverImg;
    }

    public void setWorkCoverImg(String workCoverImg) {
        this.workCoverImg = workCoverImg;
    }

    public Integer getEdit() {
        return edit;
    }

    public void setEdit(Integer edit) {
        this.edit = edit;
    }

    public Long getEdtiTime() {
        return edtiTime;
    }

    public void setEdtiTime(Long edtiTime) {
        this.edtiTime = edtiTime;
    }

    public Integer getWorkStatus() {
        return workStatus;
    }

    public void setWorkStatus(Integer workStatus) {
        this.workStatus = workStatus;
    }

    public List<WorksLog> getWorksLog() {
        return worksLog;
    }

    public void setWorksLog(List<WorksLog> worksLog) {
        this.worksLog = worksLog;
    }
}

 

以上是关于Mybatis下collections使用pageHelper进行分页的主要内容,如果未能解决你的问题,请参考以下文章

mybatis使用collection查询集合属性规则

Mybatis_IllegalArgumentException: Mapped Statements collection does not contain value for

Mybatis之foreach用法----ListArrayMap三种类型遍历

mybatis数组和集合的长度判断及插入

mybatis映射文件select_resultMap_关联查询_collection定义关联集合

Mybatis中collection和association的使用区别