com.jeesuite.mybatis.plugin.pagination.PaginationHandler Maven / Gradle / Ivy
package com.jeesuite.mybatis.plugin.pagination;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeesuite.mybatis.MybatisConfigs;
import com.jeesuite.mybatis.core.InterceptorHandler;
import com.jeesuite.mybatis.exception.MybatisHanlerInitException;
import com.jeesuite.mybatis.parser.EntityInfo;
import com.jeesuite.mybatis.parser.MybatisMapperParser;
import com.jeesuite.mybatis.plugin.JeesuiteMybatisInterceptor;
import com.jeesuite.mybatis.plugin.pagination.PageSqlUtils.DbType;
import com.jeesuite.mybatis.plugin.pagination.annotation.Pageable;
public class PaginationHandler implements InterceptorHandler {
private static Logger logger = LoggerFactory.getLogger(PaginationHandler.class);
public static final String NAME = "page";
private static final String PAGE_COUNT_SUFFIX = "_PageCount";
private Map pageMappedStatements = new HashMap<>();
private DbType dbType = DbType.MYSQL;
public void setDbType(String dbType){
if(StringUtils.isBlank(dbType))return;
DbType[] dbTypes = DbType.values();
for (DbType dt : dbTypes) {
if(dt.name().equalsIgnoreCase(dbType)){
this.dbType = dt;
break;
}
}
}
@Override
public void start(JeesuiteMybatisInterceptor context) {
setDbType(MybatisConfigs.getDbType(context.getGroupName()));
logger.info("dbType:{}",dbType.name());
List entityInfos = MybatisMapperParser.getEntityInfos(context.getGroupName());
for (EntityInfo ei : entityInfos) {
Class> mapperClass = ei.getMapperClass();
Method[] methods = mapperClass.getDeclaredMethods();
for (Method method : methods) {
if(method.getReturnType() == Page.class){
String msId = ei.getMapperClass().getName() + "." + method.getName();
boolean withPageParams = false;
Class>[] parameterTypes = method.getParameterTypes();
self:for (Class> clazz : parameterTypes) {
if(withPageParams = (clazz == PageParams.class || clazz.getSuperclass() == PageParams.class)){
break self;
}
}
if(!withPageParams){
throw new MybatisHanlerInitException(String.format("method[%s] returnType is:Page,but not found Parameter[PageParams] in Parameters list", method.getName()));
}
pageMappedStatements.put(msId,true);
}else if(method.isAnnotationPresent(Pageable.class)){
String msId = ei.getMapperClass().getName() + "." + method.getName();
pageMappedStatements.put(msId,false);
}
}
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object onInterceptor(Invocation invocation) throws Throwable {
try {
final Executor executor = (Executor) invocation.getTarget();
final Object[] args = invocation.getArgs();
final MappedStatement orignMappedStatement = (MappedStatement)args[0];
if(!orignMappedStatement.getSqlCommandType().equals(SqlCommandType.SELECT))return null;
PageParams pageParams = PageExecutor.getPageParams();
if(pageParams == null && !pageMappedStatements.keySet().contains(orignMappedStatement.getId()))return null;
final RowBounds rowBounds = (RowBounds) args[2];
final ResultHandler resultHandler = (ResultHandler) args[3];
final Object parameter = args[1];
BoundSql boundSql;
if(args.length == 4){
boundSql = orignMappedStatement.getBoundSql(parameter);
} else {
boundSql = (BoundSql) args[5];
}
if(pageParams == null && pageMappedStatements.get(orignMappedStatement.getId())){
if(parameter instanceof Map){
Collection parameterValues = ((Map)parameter).values();
for (Object val : parameterValues) {
if(val instanceof PageParams){
pageParams = (PageParams) val;
break;
}
}
}else{
pageParams = (PageParams) parameter;
}
}
if(pageParams == null)return null;
//查询总数
MappedStatement countMappedStatement = getCountMappedStatement(orignMappedStatement);
Long total = executeQueryCount(executor, countMappedStatement, parameter, boundSql, rowBounds, resultHandler);
//查询分页数据
List> datas = executeQuery(executor, orignMappedStatement, parameter, boundSql, rowBounds, resultHandler, pageParams);
Page
© 2015 - 2025 Weber Informatics LLC | Privacy Policy