| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package com.izouma.awesomeadmin.interceptor;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Map;
- import java.util.Properties;
- import org.apache.ibatis.executor.parameter.ParameterHandler;
- import org.apache.ibatis.executor.statement.StatementHandler;
- import org.apache.ibatis.mapping.BoundSql;
- import org.apache.ibatis.mapping.MappedStatement;
- import org.apache.ibatis.plugin.Interceptor;
- import org.apache.ibatis.plugin.Intercepts;
- import org.apache.ibatis.plugin.Invocation;
- import org.apache.ibatis.plugin.Plugin;
- import org.apache.ibatis.plugin.Signature;
- import org.apache.ibatis.reflection.DefaultReflectorFactory;
- import org.apache.ibatis.reflection.MetaObject;
- import org.apache.ibatis.reflection.SystemMetaObject;
- import com.izouma.awesomeadmin.constant.AppConstant;
- import com.izouma.awesomeadmin.dto.Page;
- /**
- * <p>分页拦截器 </p>
- *
- * @author 姓名 <br />
- * 更新履历 <br />
- * 日期 : 姓名: 更新内容<br />
- * @version 1.0
- */
- @Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
- public class PageInterceptor implements Interceptor {
- @Override
- public Object intercept(Invocation invocation) throws Throwable {
- StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
- MetaObject metaObjet = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY,
- SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
- MappedStatement mappedStatement = (MappedStatement) metaObjet.getValue("delegate.mappedStatement");
- //配置文件中SQL语句的ID
- String id = mappedStatement.getId();
- //检查以ByPage结尾的id
- if (id.matches(".+ByPage$")) {
- BoundSql bounSql = statementHandler.getBoundSql();
- //原始的SQL语句
- String sql = bounSql.getSql();
- Connection connection = (Connection) invocation.getArgs()[0];
- //默认mysql数据库
- //查询总条数的SQL语句
- String countSql = "select count(1) from (" + sql + ") aliasForPage";
- Page page = new Page();
- Map<?, ?> parameter = (Map<?, ?>) bounSql.getParameterObject();
- if (parameter != null) {
- page = (Page) parameter.get(AppConstant.PAGE);
- }
- if ("SqlServer".equals(getDataBaseType(connection))) {
- String baseSql = sql.substring(0, sql.toUpperCase().lastIndexOf("ORDER"));
- countSql = "select count(1) from (" + baseSql + ") aliasForPage";
- }
- PreparedStatement countStatement = connection.prepareStatement(countSql);
- ParameterHandler parameterHandler = (ParameterHandler) metaObjet.getValue("delegate.parameterHandler");
- parameterHandler.setParameters(countStatement);
- ResultSet rs = countStatement.executeQuery();
- if (rs.next()) {
- page.setTotalNumber(rs.getInt(1));
- }
- //改造后带分页查询的SQL语句
- String pageSql = sql + " limit " + page.getDbIndex() + "," + page.getDbNumber();
- if ("SqlServer".equals(getDataBaseType(connection))) {
- //改造后带分页查询的SQL语句
- pageSql = sql + " offset " + page.getDbIndex() + " row fetch next " + page.getDbNumber() + " row only ";
- }
- metaObjet.setValue("delegate.boundSql.sql", pageSql);
- }
- return invocation.proceed();
- }
- @Override
- public Object plugin(Object target) {
- return Plugin.wrap(target, this);
- }
- @Override
- public void setProperties(Properties properties) {
- // TODO Auto-generated method stub
- }
- /**
- * 判断数据库类型
- *
- * @return
- */
- public static String getDataBaseType(Connection connection) {
- //通过driverName是否包含关键字判断
- try {
- if (connection.getMetaData().getDriverName().toUpperCase()
- .indexOf("MYSQL") != -1) {
- return "MySql";
- } else if (connection.getMetaData().getDriverName().toUpperCase()
- .indexOf("SQL SERVER") != -1) {
- //sqljdbc与sqljdbc4不同,sqlserver中间有空格
- return "SqlServer";
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return "-1";
- }
- }
|