转载自:http://blog.csdn.net/hfmbook/article/details/41985853
比较适用于在分页时候进行拦截。对分页的SQL语句通过封装处理,处理成不同的分页sql。
实用性比较强。
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.Properties;
-
- import org.apache.ibatis.executor.parameter.ParameterHandler;
- import org.apache.ibatis.executor.statement.RoutingStatementHandler;
- import org.apache.ibatis.executor.statement.StatementHandler;
- import org.apache.ibatis.mapping.BoundSql;
- import org.apache.ibatis.mapping.MappedStatement;
- import org.apache.ibatis.mapping.ParameterMapping;
- 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.scripting.defaults.DefaultParameterHandler;
-
- import com.yidao.utils.Page;
- import com.yidao.utils.ReflectHelper;
-
- @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})
- public class PageInterceptor implements Interceptor {
- private String dialect = "";
- private String pageSqlId = "";
-
- public Object intercept(Invocation invocation) throws Throwable {
-
-
-
-
-
-
-
-
- if(invocation.getTarget() instanceof RoutingStatementHandler){
- RoutingStatementHandler statementHandler = (RoutingStatementHandler)invocation.getTarget();
- StatementHandler delegate = (StatementHandler) ReflectHelper.getFieldValue(statementHandler, "delegate");
- BoundSql boundSql = delegate.getBoundSql();
- Object obj = boundSql.getParameterObject();
- if (obj instanceof Page<?>) {
- Page<?> page = (Page<?>) obj;
-
- MappedStatement mappedStatement = (MappedStatement)ReflectHelper.getFieldValue(delegate, "mappedStatement");
-
- Connection connection = (Connection)invocation.getArgs()[0];
-
- String sql = boundSql.getSql();
-
- this.setTotalRecord(page,
- mappedStatement, connection);
-
- String pageSql = this.getPageSql(page, sql);
-
- ReflectHelper.setFieldValue(boundSql, "sql", pageSql);
- }
- }
- return invocation.proceed();
- }
-
-
- private void setTotalRecord(Page<?> page,
- MappedStatement mappedStatement, Connection connection) {
-
-
- BoundSql boundSql = mappedStatement.getBoundSql(page);
-
- String sql = boundSql.getSql();
-
- String countSql = this.getCountSql(sql);
-
- List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
-
- BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings, page);
-
- ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, page, countBoundSql);
-
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = connection.prepareStatement(countSql);
-
- parameterHandler.setParameters(pstmt);
-
- rs = pstmt.executeQuery();
- if (rs.next()) {
- int totalRecord = rs.getInt(1);
-
- page.setTotalRecord(totalRecord);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (pstmt != null)
- pstmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-
-
- private String getCountSql(String sql) {
- int index = sql.indexOf("from");
- return "select count(*) " + sql.substring(index);
- }
-
-
- private String getPageSql(Page<?> page, String sql) {
- StringBuffer sqlBuffer = new StringBuffer(sql);
- if ("mysql".equalsIgnoreCase(dialect)) {
- return getMysqlPageSql(page, sqlBuffer);
- } else if ("oracle".equalsIgnoreCase(dialect)) {
- return getOraclePageSql(page, sqlBuffer);
- }
- return sqlBuffer.toString();
- }
-
-
- private String getMysqlPageSql(Page<?> page, StringBuffer sqlBuffer) {
-
- int offset = (page.getPage() - 1) * page.getRows();
- sqlBuffer.append(" limit ").append(offset).append(",").append(page.getRows());
- return sqlBuffer.toString();
- }
-
-
- private String getOraclePageSql(Page<?> page, StringBuffer sqlBuffer) {
-
- int offset = (page.getPage() - 1) * page.getRows() + 1;
- sqlBuffer.insert(0, "select u.*, rownum r from (").append(") u where rownum < ").append(offset + page.getRows());
- sqlBuffer.insert(0, "select * from (").append(") where r >= ").append(offset);
-
-
- return sqlBuffer.toString();
- }
-
-
-
- public Object plugin(Object arg0) {
-
- if (arg0 instanceof StatementHandler) {
- return Plugin.wrap(arg0, this);
- } else {
- return arg0;
- }
- }
-
-
- public void setProperties(Properties p) {
-
- }
-
- public String getDialect() {
- return dialect;
- }
-
- public void setDialect(String dialect) {
- this.dialect = dialect;
- }
-
- public String getPageSqlId() {
- return pageSqlId;
- }
-
- public void setPageSqlId(String pageSqlId) {
- this.pageSqlId = pageSqlId;
- }
-
- }
xml配置:
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
- <property name="basePackage" value="com.yidao.mybatis.dao" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
-
-
- <bean id="paginationInterceptor" class="com.mybatis.interceptor.PageInterceptor">
- <property name="dialect" value="mysql"/>
-
- <property name="pageSqlId" value=".*query$"/>
- </bean>
在mybatis执行SQL语句之前进行拦击处理
原文:http://www.cnblogs.com/wenlj/p/4700115.html