Java tutorial
package com.jhcz.trade.framework.plugin.mybatis; /* The MIT License (MIT) Copyright (c) 2014 abel533@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import javax.xml.bind.PropertyException; import org.apache.ibatis.executor.statement.BaseStatementHandler; 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.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.jhcz.trade.common.vo.Common; import com.jhcz.trade.common.vo.PageView; import com.jhcz.trade.framework.plugin.dialet.Dialect; /** * Mybatis?StatementHandlerprepare? * ??Page?? ??Page? * ??Page?@Param? */ @SuppressWarnings("unchecked") @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) }) public class PagePlugin implements Interceptor { /** * ? */ private static Dialect dialectObject = null; /** * mybaits?xml?ID(?) */ private static String pageSqlId = ""; public Object intercept(Invocation ivk) throws Throwable { if (ivk.getTarget() instanceof RoutingStatementHandler) { RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper .getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); // 1???.*query.* 2??page? // if (mappedStatement.getId().matches(pageSqlId)) { // ?SQL BoundSql boundSql = delegate.getBoundSql(); // SQL<select>parameterType??Mapper??,?? Object parameterObject = boundSql.getParameterObject(); if (parameterObject == null) { // throw new // NullPointerException("boundSql.getParameterObject() is null!"); return ivk.proceed(); } else { PageView pageView = null; if (parameterObject instanceof PageView) { // ?Pages pageView = (PageView) parameterObject; } else if (parameterObject instanceof Map) { for (Entry entry : (Set<Entry>) ((Map) parameterObject).entrySet()) { if (entry.getValue() instanceof PageView) { pageView = (PageView) entry.getValue(); break; } } if (pageView == null) { return ivk.proceed(); } } else { // ??Pages pageView = ReflectHelper.getValueByFieldType(parameterObject, PageView.class); if (pageView == null) { return ivk.proceed(); } } String sql = boundSql.getSql(); PreparedStatement countStmt = null; ResultSet rs = null; try { // Connection connection = (Connection) ivk.getArgs()[0]; String countSql = "select count(1) from (" + sql + ") tmp_count"; countStmt = connection.prepareStatement(countSql); ReflectHelper.setValueByFieldName(boundSql, "sql", countSql); DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); parameterHandler.setParameters(countStmt); rs = countStmt.executeQuery(); int count = 0; if (rs.next()) { count = ((Number) rs.getObject(1)).intValue(); } pageView.setRowCount(count); } finally { try { rs.close(); } catch (Exception e) { } try { countStmt.close(); } catch (Exception e) { } } String pageSql = generatePagesSql(sql, pageView); ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); // sql???BoundSql. } // } } return ivk.proceed(); } /** * ???sql * * @param sql * @param page * @return */ private String generatePagesSql(String sql, PageView page) { if (page != null && dialectObject != null) { // pageNow1?0(page.getPageNow()-1) int pageNow = page.getPageNow(); return dialectObject.getLimitString(sql, (pageNow <= 0 ? 0 : pageNow - 1) * page.getPageSize(), pageNow * page.getPageSize()); } return sql; } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties p) { // ? String dialect = ""; dialect = p.getProperty("dialect"); if (Common.isEmpty(dialect)) { try { throw new PropertyException("dialect property is not found!"); } catch (PropertyException e) { e.printStackTrace(); } } else { try { dialectObject = (Dialect) Class.forName(dialect).getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new RuntimeException(dialect + ", init fail!\n" + e); } } // ?id??? pageSqlId = p.getProperty("pageSqlId"); if (Common.isEmpty(pageSqlId)) { try { throw new PropertyException("pageSqlId property is not found!"); } catch (PropertyException e) { e.printStackTrace(); } } } }