参考网上各位牛人的帖子,终于实现了Mybatis的物理分页。
贴出来备用
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true"> <bean class="com.nechx.util.SpringContextUtil" lazy-init="false" /> <context:property-placeholder location="classpath*:conf/core.properties" /> <context:component-scan base-package="com.nechx.*" /> <!-- 数据源配置,使用dbcp数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="30" /> <property name="maxWait" value="10000" /> </bean> <!-- 创建 SqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> <property name="mapperLocations" value="classpath:com/spm/mapper/xml/*.xml" /> </bean> <!-- 创建 SqlSessionTemplate--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 创建 MapperScannerConfigurer MapperScannerConfigurer会查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean。 basePackage 属性是让你为映射器接口文件设置基本的包路径。 可以使用分号或逗号 作为分隔符设置多于一个的包路径。 每个映射器将会在指定的包路径中递归地被搜索到。 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.spm.mapper" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用annotation定义事务 transaction-manager="transactionManager" proxy-target-class="true" --> <!-- <tx:annotation-driven proxy-target-class="true"/> --> <bean id="appConfig" class="com.spm.common.ApplicationConfig"> <property name="filePath" value="${app.fileBasePath}" /> <property name="fileServer" value="${app.fileServer}" /> </bean> <!-- <import resource="classpath*:spring/log4j.xml"/> --> </beans>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <plugins> <plugin interceptor="com.spm.interceptor.PaginationInterceptor"> <property name="dialect" value="oracle" /> <property name="pageSqlId" value=".*selectPage.*"/> </plugin> </plugins> </configuration>
PaginationInterceptor.java
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) }) public class PaginationInterceptor implements Interceptor { public static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory(); public static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory(); private static Dialect dialect = null; // SQL方言 private static String pageSqlId = ""; // 要拦截的SQL语句名正则 private final static Log log = LogFactory.getLog(PaginationInterceptor.class); public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = null; RoutingStatementHandler rsh = (RoutingStatementHandler) invocation.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(rsh, "delegate"); MappedStatement mappedStatement = (MappedStatement)ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); // 对SQL方法名做匹配,若包含指定的字符串,则拦截,进行分页操作 if (mappedStatement.getId().matches(pageSqlId)) { statementHandler = (StatementHandler) invocation.getTarget(); BoundSql boundSql = statementHandler.getBoundSql(); MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds"); if (rowBounds == null || rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql"); metaStatementHandler.setValue("delegate.boundSql.sql", dialect.getLimitString(originalSql, rowBounds.getOffset(),rowBounds.getLimit())); metaStatementHandler.setValue("delegate.rowBounds.offset",RowBounds.NO_ROW_OFFSET); metaStatementHandler.setValue("delegate.rowBounds.limit",RowBounds.NO_ROW_LIMIT); if (log.isDebugEnabled()) { log.debug("生成分页SQL : " + boundSql.getSql()); } } return invocation.proceed(); } public Object plugin(Object target) { return Plugin.wrap(target, this); } // 通过读取properties,对类中的两个属性进行赋值 public void setProperties(Properties properties) { String databaseTypeStr = properties.getProperty("dialect"); if (Tools.isEmpty(databaseTypeStr)) { try { throw new PropertyException("dialect property is not found!"); } catch (PropertyException e) { e.printStackTrace(); } } else { Dialect.Type databaseType = Dialect.Type.valueOf(databaseTypeStr.toUpperCase()); switch (databaseType) { case MYSQL: dialect = new MySql5Dialect(); break; case ORACLE: dialect = new OracleDialect(); break; } } pageSqlId = properties.getProperty("pageSqlId"); if (Tools.isEmpty(pageSqlId)) { try { throw new PropertyException("pageSqlId property is not found!"); } catch (PropertyException e) { e.printStackTrace(); } } } }
ReflectHelper.java
/** * @author Administrator * 反射工具 */ public class ReflectHelper { /** * 获取obj对象fieldName的Field * @param obj * @param fieldName * @return */ public static Field getFieldByFieldName(Object obj, String fieldName) { for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { return superClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { } } return null; } /** * 获取obj对象fieldName的属性值 * @param obj * @param fieldName * @return * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Object getValueByFieldName(Object obj, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field field = getFieldByFieldName(obj, fieldName); Object value = null; if(field!=null){ if (field.isAccessible()) { value = field.get(obj); } else { field.setAccessible(true); value = field.get(obj); field.setAccessible(false); } } return value; } /** * 设置obj对象fieldName的属性值 * @param obj * @param fieldName * @param value * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void setValueByFieldName(Object obj, String fieldName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field field = obj.getClass().getField(fieldName); //.getDeclaredField(fieldName); if (field.isAccessible()) { field.set(obj, value); } else { field.setAccessible(true); field.set(obj, value); field.setAccessible(false); } } public static void setValueByPrivateFieldName(Object obj, String fieldName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field field = obj.getClass().getDeclaredField(fieldName); if (field.isAccessible()) { field.set(obj, value); } else { field.setAccessible(true); field.set(obj, value); field.setAccessible(false); } } }