Mybatis实现分页

本文介绍了两种在MyBatis中实现数据库分页查询的方法,一种是通过SQL语句的LIMIT/OFFSET,另一种是使用Interceptor插件,如PaginationInterceptor,通过拦截查询并动态生成总记录数的SQL。
摘要由CSDN通过智能技术生成

方式一:通过sql语句实现分页

// map接口
ArrayList<ExcelZhrjAlarmInfoDTO> selectZhrjAlarmInfosPageByType(@Param("start") int start,@Param("end") int end,@Param("type") int type);
// xml文件
<select id="selectZhrjAlarmInfosPageByType" resultType="com.zhrj.exam.dto.ExcelZhrjAlarmInfoDTO" parameterType="int">
    select ifnull(B.dept_name,A.dept_id) as deptName,A.wan_ip,A.event_time,A.event_type,A.event_status,A.event_reasons
    from zhrj_alarm_info A
    left join zhrj_dept B on A.dept_id = B.dept_id
    where A.event_type = #{type}
    limit #{end} offset #{start}
</select>

方式二:实现Interceptor接口

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;

import java.util.Properties;

@Intercepts({@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class}
)})
public class PaginationInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];

        // 判断是否需要分页
        if (mappedStatement.getId().contains("ByPage")) {
            Object parameter = args[1];
            PageParam pageParam = null;
            if (parameter instanceof PageParam) {
                pageParam = (PageParam) parameter;
            } else if (parameter instanceof Map) {
                Map<String, Object> paramMap = (Map<String, Object>) parameter;
                if (paramMap.containsKey("page") && paramMap.containsKey("pageSize")) {
                    pageParam = new PageParam((Integer) paramMap.get("page"), (Integer) paramMap.get("pageSize"));
                    paramMap.put("pageParam", pageParam);
                }
            }

            if (pageParam != null) {
                String originalSql = mappedStatement.getBoundSql(parameter).getSql();
                String countSql = "SELECT COUNT(1) FROM (" + originalSql + ") AS total";
                PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
                args[0] = MappedStatementUtils.copyFromMappedStatement(mappedStatement, (sqlSession) ->
                        sqlSession.getConfiguration().getMappedStatement(mappedStatement.getId() + "Count", false, true)
                );
                args[1] = parameter;

                Object result = invocation.proceed();
                pageParam.setTotal((Long) ((List<?>) result).get(0));

                args[0] = MappedStatementUtils.copyFromMappedStatement(mappedStatement, (sqlSession) ->
                        sqlSession.getConfiguration().getMappedStatement(mappedStatement.getId(), false, true)
                );
                args[1] = parameter;
                PageHelper.clearPage();

                return invocation.proceed();
            }
        }

        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // 配置初始化时的属性
    }
}

然后,在MyBatis的配置文件中添加该Interceptor:

<plugins>
    <plugin interceptor="your.package.name.PaginationInterceptor"/>
</plugins>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值