原来的分页SQL
select * from 表名 limilt 155555,20
优化后的sql语句
select *
FROM 表名 WHERE 'id' in (select id from 表名 LIMIT 155555,20)
1.首先开启 PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
必然会在sql的后面追加 155555,20
而我们的优化后的SQL的limit是一个子查询语句如何解决?,此时仍然按照以前的做法会得到
SELECT * FROM 表名 WHERE 'id' in (select id from 表名 ) LIMIT 155555,20
而不是
select * FROM 表名 WHERE 'id' in (select id from 表名 LIMIT 155555,20)
2.他默认的count是查询全部内容的数量,而我们的子查询的模糊的数量如何解决
PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
//在这里算出select id from 表名 LIMIT 0,30
List<Integer> pageProcessLogList = processLogMapper.getCount(processLog);
if (pageProcessLogList.size() == 0) {
processLog.setIds(null);
} else {
processLog.setIds(pageProcessLogList);
}
//这里我们不用processlists查询的数量,而是用pageProcessLogList
PageInfo<Integer> countInfo = new PageInfo<>(pageProcessLogList);
List<ProcessLog> processlists =processLogMapper.pageProcessLogList(processLog);
PageInfo<ProcessLog> infolist = new PageInfo<>(processlists);
//countinfo含有的信息是pagehepler封装过的,我们换个对象
BeanUtils.copyProperties(countInfo,infolist);
infolist.setList(processlists);
return infolist;
Mapper文件
Mapper
``java
<select id="getCount" resultType="java.lang.Integer" parameterType="com.tvunetworks.useractivity.model.ProcessLog">
select id from
<choose>
<when test="dateSuffix.length()==6">
t_process_email_log${
dateSuffix}
</when>
<otherwise>
t_process_log${
dateSuffix}
</otherwise>
</choose>
<where>
<if test="serverName != null and serverName != ''">
`server_name` = #{
serverName}
</if>
<if test="method != null and method != ''">
and `method` = #{
method}
</if>
<if test="requestUri != null and requestUri != ''">
and `request_uri` like CONCAT('%',#{
requestUri},'%')
</if>
<if test="params != null and params != ''">
and `params` like CONCAT('%',#{
params},'%')
</if>
<if test="user != null and user != ''">
and `user` like CONCAT('%',#{
user},'%')
</if>
<if test="status != null and status != ''">
and `status` = #{
status}
</if>
<if test="result != null and result != ''">
and `result` like CONCAT('%',#{
result},'%')
</if>
<if test="requestTime != null and requestTime != ''">
and `request_time` >= #{
requestTime}
</if>
<if test="responseTime != null and responseTime != ''">
and `response_time` = #{
responseTime}
</if>
<if test="email != null and email != ''">
and `email` like CONCAT('%',#{
email},'%')
</if>
<if test="peerId != null and peerId != ''">
and `peer_id` like CONCAT('%',#{
peerId},'%')
</if>
<if test="ip != null and ip != ''">
and `ip` like CONCAT('%',#{
ip},'%')
</if>
<if test="userAgent != null and userAgent != ''">
and `user_agent` like CONCAT('%',#{
userAgent},'%')
</if>
<if test="dpi != null and dpi != ''">
and `dpi` like CONCAT('%',#{
dpi},'%')
```sql
<select id="pageProcessLogList" resultType="com.tvunetworks.useractivity.model.ProcessLog">
select * from
<choose>
<when test="dateSuffix.length()==6">
表名${
dateSuffix} a
</when>
<otherwise>
表名${
dateSuffix} a
</otherwise>
</choose>
<where>
<if test="ids!=null">
id in
<foreach collection="ids" item="s" open=" (" separator="," close=")">
#{
s}
</foreach>
</if>
<if test="serverName != null and serverName != ''">
and `server_name` = #{
serverName}
</if>
<if test="method != null and method != ''">
and `method` = #{
method}
</if>
<if test="requestUri != null and requestUri != ''">
and `request_uri` like CONCAT('%',#{
requestUri},'%')
</if>
<if test="params != null and params != ''">
and `params` like CONCAT('%',#{
params},'%')
</if>
<if test="user != null and user != ''">
and `user` like CONCAT('%',#{
user},'%')
</if>
<if test="status != null and status != ''">
and `status` = #{
status}
</if>
<if test="result != null and result != ''">
and `result` like CONCAT('%',#{
result},'%')
</if>
<if test="requestTime != null and requestTime != ''">
and `request_time` >= #{
requestTime}
</if>
<if test="responseTime != null and responseTime != ''">
and `response_time` = #{
responseTime}
</if>
<if test="email != null and email != ''">
and `email` like CONCAT('%',#{
email},'%')
</if>
<if test="peerId != null and peerId != ''">
and `peer_id` like CONCAT('%',#{
peerId},'%')
</if>
<if test="ip != null and ip != ''">
and `ip` like CONCAT('%',#{
ip},'%')
</if>
<if test="userAgent != null and userAgent != ''">
and `user_agent` like CONCAT('%',#{
userAgent},'%')
</if>
<if test="dpi != null and dpi != ''">
and `dpi` like CONCAT('%',#{
dpi},'%')
</if>
</where>
<if test="sortModel != null and sortModel != ''">
order by request_time ${
sortModel}
</if>
</select>
```</if>
</where>
</select>
在 <select id="getCount" resultType="java.lang.Integer" parameterType="com.tvunetworks.useractivity.model.ProcessLog">
select id from
<choose>
<when test="dateSuffix.length()==6">
表名 ${dateSuffix}
</when>
<otherwise>
表名 ${dateSuffix}
</otherwise>
</choose>
<where>
<if test="serverName != null and serverName != ''">
`server_name` = #{serverName}
</if>
<if test="method != null and method != ''">
and `method` = #{method}
</if>
<if test="requestUri != null and requestUri != ''">
and `request_uri` like CONCAT('%',#{requestUri},'%')
</if>
<if test="params != null and params != ''">
and `params` like CONCAT('%',#{params},'%')
</if>
<if test="user != null and user != ''">
and `user` like CONCAT('%',#{user},'%')
</if>
<if test="status != null and status != ''">
and `status` = #{status}
</if>
<if test="result != null and result != ''">
and `result` like CONCAT('%',#{result},'%')
</if>
<if test="requestTime != null and requestTime != ''">
and `request_time` >= #{requestTime}
</if>
<if test="responseTime != null and responseTime != ''">
and `response_time` = #{responseTime}
</if>
<if test="email != null and email != ''">
and `email` like CONCAT('%',#{email},'%')
</if>
<if test="peerId != null and peerId != ''">
and `peer_id` like CONCAT('%',#{peerId},'%')
</if>
<if test="ip != null and ip != ''">
and `ip` like CONCAT('%',#{ip},'%')
</if>
<if test="userAgent != null and userAgent != ''">
and `user_agent` like CONCAT('%',#{userAgent},'%')
</if>
<if test="dpi != null and dpi != ''">
and `dpi` like CONCAT('%',#{dpi},'%')
</if>
</where>
</select>```