XML自定义sql分页,返回记录总数

席言
2023-12-01

在实际的Java项目的开发中,分页是必不可少的;一些简单的分页就可以直接用分页插件及mybatis-plus 就可以实现;要是碰到一些比较复杂的分页 还是手撸分页更好。小编在一次项目开发中的问题与大家分享!

首先说一下大致需求:多张表的联合查询,多个查询条件,返回数据来自不同的表。
接下来开始我们的撸代码环节》

  1. 先定义传入参数的接收类:
public class logManagerDto {
    String orgName;//传入参数 部门名称
    String logType;//传入参数 日志类型
    String timeArea;//传入参数 时间范围 带时分秒
}

  1. 定义查询结果返回接收参数类
public class LogSystemManageVO {
    String userName;
    String time;
    String type;
    String function;
    String desc1;
    String logIp;
    String logPlace;
    String browserVersion;

}
  1. 在xml 里面写自定义的sql
    查询返回记录:
    LIMIT :一页的大小 OFFSET :起始页的偏移量
    xml的时间比较的格式转义: <![CDATA[tb_log_system_manage.f_time >= #{starttime}]]>
 <select id="selectLogManage" resultType="tfgic.tfipm.pojo.vo.LogSystemManageVO">
        SELECT
        tb_user_info.f_user_name AS userName ,
        tb_log_system_manage.f_time AS time,
        tb_log_system_manage.f_operation AS type,
        tb_log_system_manage.f_function AS function,
        tb_log_system_manage.f_desc AS desc1,
        tb_log_system_manage.f_log_ip AS logIp,
        tb_log_system_manage.f_log_place AS logPlace,
        tb_log_system_manage.f_browser_version AS browserVersion
        FROM
        tb_org_info
        JOIN tb_user_info ON tb_org_info.f_org_id = tb_user_info.f_org_id
        JOIN tb_log_system_manage ON tb_user_info.f_user_id = tb_log_system_manage.f_user_id
        <where>
                <if test="starttime != null">
                    <![CDATA[tb_log_system_manage.f_time >= #{starttime}]]>
                </if>
                <if test="endtime != null">
                    and  <![CDATA[tb_log_system_manage.f_time <= #{endtime}]]>
                </if>
                <if test="orgName != null and orgName != ''">
                    and tb_org_info.f_iu_fullname = #{orgName,jdbcType=VARCHAR}
                </if>
         </where>
        ORDER BY tb_log_system_manage.f_time 	desc
        LIMIT  #{pageParam.size} OFFSET  #{pageParam.current}
  </select>

同上面的筛选条件查询该条件下的记录总数

 <select id="selectLogManageAll" resultType="java.lang.Long">
        SELECT
        COUNT(*)
        FROM
        tb_org_info
        JOIN tb_user_info ON tb_org_info.f_org_id = tb_user_info.f_org_id
        JOIN tb_log_system_manage ON tb_user_info.f_user_id = tb_log_system_manage.f_user_id
        <where>
            <if test="starttime != null">
                <![CDATA[tb_log_system_manage.f_time >= #{starttime}]]>
            </if>
            <if test="endtime != null">
                and  <![CDATA[tb_log_system_manage.f_time <= #{endtime}]]>
            </if>
            <if test="orgName != null and orgName != ''">
                and tb_org_info.f_iu_fullname = #{orgName,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
  1. Mapper代码
 
    List<LogSystemManageVO> selectLogManage(@Param("pageParam")PageParam pageParam, @Param("starttime")Date starttime, @Param("endtime")Date endtime, @Param("orgName")String orgName);

    /**
     *  总数
     */
    long selectLogManageAll(@Param("starttime")Date starttime, @Param("endtime")Date endtime, @Param("orgName")String orgName);
  1. service 、 serviceImpl 此处省略
  2. controller层
PageParam pageParam = new PageParam();
int psize = (int)page.getSize();//传入参数 一页大小
int pcurrent = ((int) page.getCurrent()-1)*psize;//页数偏移量

pageParam.setSize(psize);
pageParam.setCurrent(pcurrent);

 List<LogSystemManageVO> logSystemManageVOList = new ArrayList<>();
logSystemManageVOList =logSystemManageVOMapper.selectLogManage(pageParam,starttime,endtime,project.getOrgName());//查询记录
long total = logSystemManageVOMapper.selectLogManageAll(starttime,endtime,project.getOrgName());//查询总记录数
         

PageVO pageVO = new PageVO();
pageVO.setData(logSystemManageVOList);
pageVO.setTotal(total);
return  ResponseResult.success(pageVO);
  1. PageParam 类:
public class PageParam {

    /**
     * 页偏移
     */
    private Integer current;

    /**
     * 每页显示数量
     *
     */
    private Integer size;

    public PageParam() {
    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }
}
  1. PageVo 类:
public class PageVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long total;
    private List data;
}

  1. 完成以上的操作 就可以的到:
    {
    “code”: 200000,
    “data”: {
    “total”: 53,//返回记录总数
    “data”: [
    …这里是返回数据
    ]
    },
    “message”: “请求成功”,
    “time”: “”
    }
 类似资料: