Java tutorial
/* 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. */ package com.hotpot.commons.pagination; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.text.StrBuilder; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.*; import org.apache.ibatis.plugin.*; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.factory.DefaultObjectFactory; import org.apache.ibatis.reflection.factory.ObjectFactory; import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import java.util.ArrayList; import java.util.List; import java.util.Properties; // TODO: Auto-generated Javadoc /** * Mybatis - * * ,??. * * @author pengz * @version 3.2.2 */ @SuppressWarnings("rawtypes") @Intercepts(@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class })) public class PageHelper implements Interceptor { /** The Constant DEFAULT_OBJECT_FACTORY. */ public static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory(); /** The Constant DEFAULT_OBJECT_WRAPPER_FACTORY. */ public static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory(); /** The Constant NULL_META_OBJECT. */ public static final MetaObject NULL_META_OBJECT = MetaObject.forObject(NullObject.class, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); /** * The Class NullObject. */ private static class NullObject { } /** * ??Mybatis?. * * @param object the object * @return the meta object */ public static MetaObject forObject(Object object) { return MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); } /** The Constant BOUND_SQL. */ private static final String BOUND_SQL = "sqlSource.boundSql.sql"; /** The Constant LOCAL_PAGE. */ private static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>(); /** The Constant EMPTY_RESULTMAPPING. */ private static final List<ResultMapping> EMPTY_RESULTMAPPING = new ArrayList<ResultMapping>(0); //? /** The dialect. */ private static String dialect = ""; //RowBounds?offsetPageNum - ? /** The offset as page num. */ private static boolean offsetAsPageNum = false; //RowBounds?count - ? /** The row bounds with count. */ private static boolean rowBoundsWithCount = false; /** * . * * @param pageNum the page num * @param pageSize the page size */ public static void startPage(int pageNum, int pageSize) { startPage(pageNum, pageSize, true); } /** * . * * @param pageNum the page num * @param pageSize the page size * @param count the count */ public static void startPage(int pageNum, int pageSize, boolean count) { LOCAL_PAGE.set(new Page(pageNum, pageSize, count)); } /** * . * * @param pageNum the page num * @param pageSize the page size * @param count the count * @param sidxs ? * @param sorts ?/?? */ public static void startPage(int pageNum, int pageSize, boolean count, String[] sidxs, String[] sorts) { LOCAL_PAGE.set(new Page(pageNum, pageSize, count, sidxs, sorts)); } /** * ??. * * @param rowBounds the row bounds * @return the page */ private Page getPage(RowBounds rowBounds) { Page page = LOCAL_PAGE.get(); //?? LOCAL_PAGE.remove(); if (page == null) { if (offsetAsPageNum) { page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount); } else { page = new Page(rowBounds, rowBoundsWithCount); } } return page; } /** * Mybatis. * * @param invocation the invocation * @return the object * @throws Throwable the throwable */ @SuppressWarnings("unchecked") @Override public Object intercept(Invocation invocation) throws Throwable { final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } else { //RowBounds-?Mybatis args[2] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[0]; Object parameterObject = args[1]; BoundSql boundSql = ms.getBoundSql(parameterObject); //? Page page = getPage(rowBounds); //MappedStatement MappedStatement qs = newMappedStatement(ms, new BoundSqlSqlSource(boundSql)); //?MappedStatement?qs? args[0] = qs; MetaObject msObject = forObject(qs); String sql = (String) msObject.getValue(BOUND_SQL); //?total??count if (page.isCount()) { //count - ?sql msObject.setValue(BOUND_SQL, getCountSql(sql)); // Object result = invocation.proceed(); // page.setTotal((Integer) ((List) result).get(0)); } //?sql - ?sql sql = getSortSql(sql, page); //sql - ?sql msObject.setValue(BOUND_SQL, getPageSql(sql, page)); //?? msObject.setValue("resultMaps", ms.getResultMaps()); // Object result = invocation.proceed(); //? page.addAll((List) result); // return page; // return new PageInfo(page); } } /** * Clear. */ public static void clear() { LOCAL_PAGE.remove(); } /** * ?SQL. * * @param sql the sql * @param page the page * @return the sort sql */ private String getSortSql(String sql, Page page) { if (ArrayUtils.isEmpty(page.getSidxs())) { return sql; } StrBuilder pageSql = new StrBuilder(200); if ("mysql".equals(dialect)) { pageSql.append("select _s.* from ("); pageSql.append(sql); pageSql.append(") _s order by "); for (int idx = 0, max = page.getSidxs().length; idx < max; idx++) { pageSql.append(page.getSidxs()[idx]).append(" "); if (ArrayUtils.getLength(page.getSorts()) > idx) { pageSql.append(page.getSorts()[idx]).append(" ,"); } else { pageSql.append("desc ").append(","); } } pageSql.deleteCharAt(pageSql.length() - 1); } return pageSql.toString(); } /** * ?sql - ????. * * @param sql the sql * @return the count sql */ private String getCountSql(String sql) { return "select count(0) from (" + sql + ") tmp_count"; } /** * ?sql - ????. * * @param sql the sql * @param page the page * @return the page sql */ private String getPageSql(String sql, Page page) { StrBuilder pageSql = new StrBuilder(200); if ("mysql".equals(dialect)) { pageSql.append(sql); pageSql.append(" limit " + page.getStartRow() + "," + page.getPageSize()); } else if ("hsqldb".equals(dialect)) { pageSql.append(sql); pageSql.append(" LIMIT " + page.getPageSize() + " OFFSET " + page.getStartRow()); } else if ("oracle".equals(dialect)) { pageSql.append("select * from ( select temp.*, rownum row_id from ( "); pageSql.append(sql); pageSql.append(" ) temp where rownum <= ").append(page.getEndRow()); pageSql.append(") where row_id > ").append(page.getStartRow()); } return pageSql.toString(); } /** * The Class BoundSqlSqlSource. */ private class BoundSqlSqlSource implements SqlSource { /** The bound sql. */ BoundSql boundSql; /** * Instantiates a new bound sql sql source. * * @param boundSql the bound sql */ public BoundSqlSqlSource(BoundSql boundSql) { this.boundSql = boundSql; } /* (non-Javadoc) * @see org.apache.ibatis.mapping.SqlSource#getBoundSql(java.lang.Object) */ public BoundSql getBoundSql(Object parameterObject) { return boundSql; } } /** * MappedStatement?????. * * @param ms the ms * @param newSqlSource the new sql source * @return the mapped statement */ private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId() + "_PageHelper", newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { StrBuilder keyProperties = new StrBuilder(); for (String keyProperty : ms.getKeyProperties()) { keyProperties.append(keyProperty).append(","); } keyProperties.delete(keyProperties.length() - 1, keyProperties.length()); builder.keyProperty(keyProperties.toString()); } builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); //resultMaps?int??resultMap - ? List<ResultMap> resultMaps = new ArrayList<ResultMap>(); ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), int.class, EMPTY_RESULTMAPPING).build(); resultMaps.add(resultMap); builder.resultMaps(resultMaps); builder.resultSetType(ms.getResultSetType()); builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); } /** * ?Executor. * * @param target the target * @return the object */ @Override public Object plugin(Object target) { if (target instanceof Executor) { return Plugin.wrap(target, this); } else { return target; } } /** * . * * @param p the new properties */ public void setProperties(Properties p) { dialect = p.getProperty("dialect"); if (dialect == null || "".equals(dialect)) { throw new RuntimeException("Mybatis?PageHelper?dialect?!"); } //offsetPageNum String offset = p.getProperty("offsetAsPageNum"); if (offset != null && "TRUE".equalsIgnoreCase(offset)) { offsetAsPageNum = true; } //RowBounds???count String withcount = p.getProperty("rowBoundsWithCount"); if (withcount != null && "TRUE".equalsIgnoreCase(withcount)) { rowBoundsWithCount = true; } } }