package manage.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.bind.PropertyException;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.ExecutorException;
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.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
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.reflection.property.PropertyTokenizer;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PagePlugin implements Interceptor {
private static String dialect = "";
private static String pageSqlId = "";
@SuppressWarnings("unchecked")
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");
if (mappedStatement.getId().matches(pageSqlId)) {
BoundSql boundSql = delegate.getBoundSql();
Object parameterObject = boundSql.getParameterObject();
if (parameterObject == null) {
throw new NullPointerException("parameterObject error");
} else {
Connection connection = (Connection) ivk.getArgs()[0];
String sql = boundSql.getSql();
String countSql = "select count(0) from (" + sql + ") myCount";
System.out.println("总数sql 语句:" + countSql);
PreparedStatement countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject);
setParameters(countStmt, mappedStatement, countBS, parameterObject);
ResultSet rs = countStmt.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
rs.close();
countStmt.close();
PageInfo page = null;
if (parameterObject instanceof PageInfo) {
page = (PageInfo) parameterObject;
page.setTotalResult(count);
} else if (parameterObject instanceof Map) {
Map<String, Object> map = (Map<String, Object>) parameterObject;
page = (PageInfo) map.get("page");
if (page == null)
page = new PageInfo();
page.setTotalResult(count);
} else {
Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "page");
if (pageField != null) {
page = (PageInfo) ReflectHelper.getValueByFieldName(parameterObject, "page");
if (page == null)
page = new PageInfo();
page.setTotalResult(count);
ReflectHelper.setValueByFieldName(parameterObject, "page", page);
} else {
throw new NoSuchFieldException(parameterObject.getClass().getName());
}
}
String pageSql = generatePageSql(sql, page);
System.out.println("page sql:" + pageSql);
ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql);
}
}
}
return ivk.proceed();
}
private 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) {
Configuration configuration = mappedStatement.getConfiguration();
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
PropertyTokenizer prop = new PropertyTokenizer(propertyName);
if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) {
value = boundSql.getAdditionalParameter(prop.getName());
if (value != null) {
value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
}
} else {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
}
}
}
}
private String generatePageSql(String sql, PageInfo page) {
if (page != null && (dialect != null || !dialect.equals(""))) {
StringBuffer pageSql = new StringBuffer();
if ("mysql".equals(dialect)) {
pageSql.append(sql);
pageSql.append(" limit " + page.getCurrentResult() + "," + page.getShowCount());
} else if ("oracle".equals(dialect)) {
pageSql.append("select * from (select tmp_tb.*,ROWNUM row_id from (");
pageSql.append(sql);
pageSql.append(") tmp_tb where ROWNUM<=");
pageS
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于SSM+MySQL的人力资源管理系统源码+数据库.zip 基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip基于SSM+MySQL的人力资源管理系统源码+数据库.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于SSM+MySQL的人力资源管理系统源码+数据库.zip (205个子文件)
.classpath 1KB
org.eclipse.wst.common.component 646B
org.eclipse.wst.jsdt.ui.superType.container 49B
bootstrap.css 134KB
bootstrap.min.css 111KB
bootstrap-theme.css 21KB
bootstrap-theme.min.css 19KB
bootstrap-table.css 7KB
bootstrap-select.css 7KB
common.css 883B
treeview.css 204B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
glyphicons-halflings-regular.eot 20KB
.gitignore 9B
.gitingore 62B
index.html 246B
Bishe.iml 6KB
Human-Resource.iml 336B
PagePlugin.java 9KB
UserController.java 7KB
BaseController.java 4KB
DepartmentsController.java 3KB
UserServiceImpl.java 3KB
Userinfo.java 3KB
EmployeeController.java 3KB
TrainController.java 3KB
UserinfoController.java 3KB
WageController.java 3KB
PayController.java 3KB
DepartmentsServiceImpl.java 2KB
ReflectHelper.java 2KB
BreadCrumbController.java 2KB
UserDetailsService.java 2KB
EmployeeServiceImpl.java 2KB
PayServiceImpl.java 1KB
TrainServiceImpl.java 1KB
WageServiceImpl.java 1KB
PageInfo.java 1KB
Train.java 1KB
LoginController.java 1KB
Employee.java 1KB
Pay.java 1KB
User.java 1KB
BaseServiceImpl.java 1KB
Wage.java 1KB
UserEnum.java 1KB
UserinfoServiceImpl.java 1002B
Departments.java 839B
UserMapper.java 798B
PageResult.java 692B
BaseMapper.java 589B
DateConverter.java 586B
UserService.java 578B
UserinfoMapper.java 564B
BaseService.java 562B
TrainMapper.java 556B
EmployeeMapper.java 551B
WageMapper.java 524B
PayMapper.java 485B
DepartmentsService.java 448B
DepartmentsMapper.java 413B
UserPo.java 404B
BiSheUtil.java 336B
UserinfoService.java 249B
EmployeeService.java 234B
TrainService.java 222B
WageService.java 218B
PayService.java 214B
login.jpg 336KB
logow.jpg 3KB
bootstrapValidator.js 318KB
jquery-1.8.2.min.js 91KB
jquery-min.js 81KB
bootstrap-select.js 69KB
bootstrap.js 64KB
bootstrap.min.js 34KB
treeview.js 16KB
lTable.js 5KB
index.js 5KB
pagination.js 4KB
common.js 2KB
npm.js 484B
.jsdtscope 626B
index.jsp 16KB
user3.jsp 13KB
addUserInfo.jsp 11KB
user.jsp 10KB
addUser.jsp 8KB
addTrain.jsp 8KB
departments.jsp 8KB
addEmploy.jsp 8KB
userInfo.jsp 8KB
addPay.jsp 7KB
addDepartments.jsp 7KB
addWage.jsp 7KB
pay.jsp 7KB
wage.jsp 7KB
train.jsp 7KB
共 205 条
- 1
- 2
- 3
资源评论
程序员张小妍
- 粉丝: 1w+
- 资源: 3252
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于 Ant 的 Java 项目示例.zip
- 各种字符串相似度和距离算法的实现Levenshtein、Jaro-winkler、n-Gram、Q-Gram、Jaccard index、最长公共子序列编辑距离、余弦相似度…….zip
- 运用python生成的跳跃的爱心
- 包括用 Java 编写的程序 欢迎您在此做出贡献!.zip
- (源码)基于QT框架的学生管理系统.zip
- 功能齐全的 Java Socket.IO 客户端库,兼容 Socket.IO v1.0 及更高版本 .zip
- 功能性 javascript 研讨会 无需任何库(即无需下划线),只需 ES5 .zip
- 分享Java相关的东西 - Java安全漫谈笔记相关内容.zip
- 具有适合 Java 应用程序的顺序定义的 Cloud Native Buildpack.zip
- 网络建设运维资料库职业
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功