在实际的Java项目的开发中,分页是必不可少的;一些简单的分页就可以直接用分页插件及mybatis-plus 就可以实现;要是碰到一些比较复杂的分页 还是手撸分页更好。小编在一次项目开发中的问题与大家分享!
首先说一下大致需求:多张表的联合查询,多个查询条件,返回数据来自不同的表。
接下来开始我们的撸代码环节》
public class logManagerDto {
String orgName;//传入参数 部门名称
String logType;//传入参数 日志类型
String timeArea;//传入参数 时间范围 带时分秒
}
public class LogSystemManageVO {
String userName;
String time;
String type;
String function;
String desc1;
String logIp;
String logPlace;
String browserVersion;
}
<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>
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);
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);
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;
}
}
public class PageVO<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Long total;
private List data;
}