2025年mysql 分页查询拦截器_【Mybatis】Mybatis 分页-拦截器方式

mysql 分页查询拦截器_【Mybatis】Mybatis 分页-拦截器方式Desc Mybatis 分页拦截器 Author 山野痞夫 Date 2015 年 8 月 25 日 Time 下午 4 14 14 Intercepts Signature method prepare type StatementHan class args Connection

大家好,我是讯享网,很高兴认识大家。

Desc : Mybatis分页拦截器 Author: 山野痞夫 Date : 2015年8月25日 Time: : 下午4:14:14 */ @Intercepts({ @Signature(method = "prepare", type = StatementHandler.class, args = { Connection.class }) }) public class SpringMybatisPageInterceptor implements Interceptor {

/ 拦截器 */ public Object intercept(Invocation invocation) throws Throwable { //拦截执行的Handler final RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget(); //获取分页拦截器必要的参数 final StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate"); //获取执行的SQL final BoundSql boundSql = delegate.getBoundSql(); //获取执行的参数 final Object obj = boundSql.getParameterObject(); //获取StateMent final MappedStatement mappedStatement=(MappedStatement)ReflectUtil.getFieldValue(delegate, "mappedStatement"); //判断参数是否有Page参数,如果有则按照 if (obj instanceof Page) { Page resultPage = new Page(); final Page page = (Page) obj; final String sql = boundSql.getSql(); //计算总的条目数 final String countSQL = getMysqlCountSql(new StringBuffer(sql)); int totalCount = getCounts(mappedStatement, countSQL ,boundSql,page); //获取分页后的结果 final String pageSql = this.getMysqlPageSql(page, new StringBuffer(sql)); ReflectUtil.setFieldValue(boundSql, "sql", pageSql); page.setTotal(totalCount); } return invocation.proceed();

}

/ 定义使用的拦截器 */ public Object plugin(Object obj) { return Plugin.wrap(obj, this); }

/ 获取属性信息 */ public void setProperties(Properties properties) {

}

/

组装分页SQL

@param page 分页javaBean

@param sqlBuffer 组装好的分页SQL


讯享网

@return */ private String getMysqlPageSql(Page page, StringBuffer sqlBuffer) { sqlBuffer.append(" limit ").append(page.getLimitStart()).append(",").append(page.getLimitend()); return sqlBuffer.toString(); }

/

组装查询数量的SQL

@param sqlBuffer 组装好的分页SQL

@return / private String getMysqlCountSql(StringBuffer sqlBuffer) { StringBuffer buffer = new StringBuffer(); buffer.append("select count() from ("); buffer.append(sqlBuffer); buffer.append(") as total"); return buffer.toString(); }

/

获取总页数

@param mappedStatement

@param countSQL

@return */ private int getCounts(MappedStatement mappedStatement, String countSQL ,BoundSql boundSql , Page page) { final ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, page, boundSql); Connection connection = null; ResultSet rs = null; PreparedStatement countStmt = null; int totpage = 0; try { //获取连接 connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); //预处理SQL countStmt = connection.prepareStatement(countSQL); //给预处理SQL 赋值 parameterHandler.setParameters(countStmt); //查询预处理的SQL rs = countStmt.executeQuery(); if (rs.next()) { totpage = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } finally { //关闭释放资源 try { if(rs!=null)rs.close(); if(countStmt!=null)countStmt.close(); if(connection!=null)connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return totpage; } }

小讯
上一篇 2025-02-16 10:55
下一篇 2025-03-13 22:14

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/83056.html