package net.merise.mir.core.intercept;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.MappedStatement.Builder;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.mapping.SqlSource;
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.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
/**
* <font color="green">自动查询总数的插件,并自动实现分页</font>
*
* @ClassName PaginationInterceptor
* @author GangQiang
* @date 2015年4月21日 上午11:04:11
*
* @version 1.0
*/
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }) })
public class PaginationInterceptor implements Interceptor {
public String page;// 当前页码的key
public String pageSize;// 每页记录数的Key
public void setPage(String page) {
this.page = page;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object parameter = invocation.getArgs()[1];
MappedStatement mappedStatement = (MappedStatement) invocation
.getArgs()[0];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
if (parameter instanceof HashMap) {
@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) parameter;
if (params.get(pageSize) != null && params.get(page) != null) {
Integer offset = Integer.parseInt(params.get(page).toString());
Integer limit = Integer.parseInt(params.get(pageSize)
.toString());
Integer start = (offset - 1) * limit;
params.remove(page);
params.remove(pageSize);
Connection connection = mappedStatement.getConfiguration()
.getEnvironment().getDataSource().getConnection();
String countSql = "SELECT COUNT(1) FROM (" + boundSql.getSql()
+ ") count";
PreparedStatement countStmt = connection
.prepareStatement(countSql);
BoundSql countBS = new BoundSql(
mappedStatement.getConfiguration(), countSql,
boundSql.getParameterMappings(), parameter);
setParameters(countStmt, mappedStatement, countBS, parameter);
ResultSet rs = countStmt.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
params.put("total", count);
}
rs.close();
countStmt.close();
connection.close();
BoundSql newBoundSql = new BoundSql(
mappedStatement.getConfiguration(), boundSql.getSql()
+ " LIMIT " + start + "," + limit,
boundSql.getParameterMappings(),
boundSql.getParameterObject());
MappedStatement newMs = copyFromMappedStatement(
mappedStatement, new BoundSqlSqlSource(newBoundSql));
invocation.getArgs()[0] = newMs;
}
}
return invocation.proceed();
}
public static class BoundSqlSqlSource implements SqlSource {
BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
@SuppressWarnings("unchecked")
public void setParameters(PreparedStatement ps,
MappedStatement mappedStatement, BoundSql boundSql,
Object parameterObject) throws SQLException {
ErrorContext.instance().activity("setting parameters")
.object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql
.getParameterMappings();
if (parameterMappings != null) {
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
Configuration configuration = mappedStatement
.getConfiguration();
if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (configuration.getTypeHandlerRegistry()
.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration
.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
@SuppressWarnings("rawtypes")
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null)
jdbcType = configuration.getJdbcTypeForNull();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
}
}
}
}
private MappedStatement copyFromMappedStatement(MappedStatement ms,
SqlSource newSqlSource) {
Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.cache(ms.getCache());
MappedStatement newMs = builder.build();
return newMs;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
merise.rar (65个子文件)
merise
.project 1KB
WebContent
WEB-INF
lib
web.xml 2KB
META-INF
MANIFEST.MF 39B
src
config
applicationContext.xml 4KB
spring-mvc.xml 2KB
db.properties 197B
net
merise
mir
core
utils
MD5Util.java 1KB
ResultMap.java 3KB
annotation
valid
Validation.java 2KB
rule
Rule.java 785B
Length.java 903B
mapper
IBaseMapper.java 1KB
exception
MyExceptionResolver.java 3KB
intercept
ValidInterceptor.java 6KB
PaginationInterceptor.java 6KB
message
properties
validation_message.properties 839B
system_message.properties 1KB
java
ValidMessage.java 1KB
SystemMessage.java 1KB
Global.java 1KB
encrypt
DESUtil.java 4KB
DBConfigurer.java 2KB
system
mobile
service
impl
user
UserService.java 769B
iface
user
IUserService.java 677B
controller
impl
user
UserController.java 975B
iface
user
IUserController.java 1KB
mapper
user
UserMapper.java 579B
UserMapper.xml 1013B
intercept
SecurityInterceptor.java 1KB
message
MobileMessage.java 1KB
config
mobile-mvc.xml 1KB
mobile_message.properties 5KB
log4j.properties 643B
target
classes
config
net
merise
mir
core
utils
ResultMap.class 3KB
MD5Util.class 2KB
annotation
valid
Validation.class 707B
rule
Length.class 483B
Rule.class 460B
mapper
IBaseMapper.class 698B
Global.class 856B
exception
MyExceptionResolver.class 2KB
intercept
PaginationInterceptor$BoundSqlSqlSource.class 851B
ValidInterceptor.class 8KB
PaginationInterceptor.class 10KB
message
properties
java
SystemMessage.class 971B
ValidMessage.class 972B
encrypt
DBConfigurer.class 2KB
DESUtil.class 3KB
system
mobile
service
impl
user
UserService.class 1KB
iface
user
IUserService.class 535B
controller
impl
user
UserController.class 1KB
iface
user
IUserController.class 1KB
mapper
user
UserMapper.class 516B
intercept
SecurityInterceptor.class 2KB
message
MobileMessage.class 969B
config
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 554B
.jsdtscope 503B
pom.xml 5KB
.classpath 1KB
共 65 条
- 1
资源评论
- JordanPanther2016-02-25很好用,代码清晰运行无障碍
- nimayidayidahaoduo2015-09-16不好使啊。不过还是谢谢
刘岗强
- 粉丝: 36
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功