分页拦截器怎么把记录总条数传出?
public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = (StatementHandler)invocation.getTarget(); MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY); MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement"); // 配置文件中SQL语句的ID String id = mappedStatement.getId(); if(id.matches(".+ByPage$")) { BoundSql boundSql = statementHandler.getBoundSql(); // 原始的SQL语句 String sql = boundSql.getSql(); // 查询总条数的SQL语句 String countSql = "select count(*) from (" + sql + ")a"; Connection connection = (Connection)invocation.getArgs()[0]; PreparedStatement countStatement = connection.prepareStatement(countSql); ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler"); parameterHandler.setParameters(countStatement); ResultSet rs = countStatement.executeQuery(); Map<?,?> parameter = (Map<?,?>)boundSql.getParameterObject(); Page page = (Page)parameter.get("page"); if(rs.next()) { page.setTotalNumber(rs.getInt(1)); } // 改造后带分页查询的SQL语句 String pageSql = sql + " limit " + page.getDbIndex() + "," + page.getDbNumber(); metaObject.setValue("delegate.boundSql.sql", pageSql); } return invocation.proceed(); }
拦截器中 中途查询了记录总条数,若要把该总条数传给外部控制如Service层要怎么做?
想把rs.getInt(1)的值传给拦截器外部类使用