package com.flf.plugin;
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.Properties;
import javax.xml.bind.PropertyException;
import org.apache.ibatis.builder.xml.dynamic.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;
import com.flf.entity.Page;
import com.flf.util.ReflectHelper;
import com.flf.util.Tools;
@Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})
public class PagePlugin implements Interceptor {
private static String dialect = ""; //数据库方言
private static String pageSqlId = ""; //mapper.xml中需要拦截的ID(正则匹配)
public Object intercept(Invocation ivk) throws Throwable {
// TODO Auto-generated method stub
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)){ //拦截需要分页的SQL
BoundSql boundSql = delegate.getBoundSql();
Object parameterObject = boundSql.getParameterObject();//分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
if(parameterObject==null){
throw new NullPointerException("parameterObject尚未实例化!");
}else{
Connection connection = (Connection) ivk.getArgs()[0];
String sql = boundSql.getSql();
String countSql = "select count(0) from (" + sql+ ") as tmp_count"; //记录统计
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();
//System.out.println(count);
Page page = null;
if(parameterObject instanceof Page){ //参数就是Page实体
page = (Page) parameterObject;
page.setEntityOrField(true); //见com.flf.entity.Page.entityOrField 注释
page.setTotalResult(count);
}else{ //参数为某个实体,该实体拥有Page属性
Field pageField = ReflectHelper.getFieldByFieldName(parameterObject,"page");
if(pageField!=null){
page = (Page) ReflectHelper.getValueByFieldName(parameterObject,"page");
if(page==null)
page = new Page();
page.setEntityOrField(false); //见com.flf.entity.Page.entityOrField 注释
page.setTotalResult(count);
ReflectHelper.setValueByFieldName(parameterObject,"page", page); //通过反射,对实体对象设置分页对象
}else{
throw new NoSuchFieldException(parameterObject.getClass().getName()+"不存在 page 属性!");
}
}
String pageSql = generatePageSql(sql,page);
ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); //将分页sql语句反射回BoundSql.
}
}
}
return ivk.proceed();
}
/**
* 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
* @param ps
* @param mappedStatement
* @param boundSql
* @param parameterObject
* @throws SQLException
*/
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());
}
}
}
}
/**
* 根据数据库方言,生成特定的分页sql
* @param sql
* @param page
* @return
*/
private String generatePageSql(String sql,Page page){
if(page!=null && Tools.notEmpty(dialect)){
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(") as tmp_tb where ROWNUM<=");
pageSql.append(page.getCurrentResult()+page.getShowCount());
pageSql.append(") where row_id>");
pageSql.append(page.getCurrentResult());
}
return pageSql.toString();
}else{
return sql;
}
}
public Object plugin(Object arg0) {
// TODO Auto-generated method stub
return Plugin.wrap(arg0, this);
}
public void setProperties(Properties p) {
dialect = p.getProperty("dialect");
if (Tools.isEmpty(dialect)) {
try {
throw new PropertyException("dialect property is not found!");
} catch (PropertyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
p
没有合适的资源?快使用搜索试试~ 我知道了~
Spring+MyBatis+mysql 开发网站后台权限管理系统源代码下载
共299个文件
png:70个
gif:58个
class:34个
4星 · 超过85%的资源 需积分: 9 234 下载量 197 浏览量
2015-06-07
03:42:23
上传
评论 6
收藏 8.92MB ZIP 举报
温馨提示
郑重声明:本文分享内容来自互联网,转载目的在于传递更多信息,帮助大家,并不代表本人赞同其观点和 对其真实性负责。如涉及作品内容、版权和其它问题,请在七日内与本人联系,我将在第一时间删除内容! [声明]本站文章版权归原作者所有,内容为作者个人观点,本人只提供参考并不构成任何投资及应用建议。 本人拥有对此声明的最终解释权。 项目说明: 用户名:admin 密 码:admin 运行环境: 硬件平台: CPU:酷睿II。 内存:1GB以上。 软件平台: 操作系统:Windows。 数据库:MySQL。 编程平台:MyEclipse 浏览器:IE Web服务器:tomcat 分辨率:最佳效果1024×768像素。
资源推荐
资源详情
资源评论
收起资源包目录
Spring+MyBatis+mysql 开发网站后台权限管理系统源代码下载 (299个子文件)
PagePlugin.class 9KB
UserController.class 7KB
LoginController.class 5KB
UserExcelView.class 5KB
RoleController.class 5KB
Page.class 4KB
SecCodeController.class 4KB
MenuController.class 4KB
RightsHandlerInterceptor.class 3KB
User.class 3KB
UserServiceImpl.class 3KB
MenuServiceImpl.class 2KB
Menu.class 2KB
ReflectHelper.class 2KB
LoginHandlerInterceptor.class 2KB
Tools.class 2KB
RoleServiceImpl.class 2KB
MyExceptionResolver.class 2KB
RightsHelper.class 2KB
InfoController.class 1KB
Info.class 1KB
ServiceHelper.class 1KB
WebAppContextListener.class 1KB
Role.class 976B
InfoServiceImpl.class 933B
UserMapper.class 800B
Const.class 760B
UserService.class 758B
MenuService.class 601B
MenuMapper.class 591B
RoleMapper.class 461B
RoleService.class 439B
InfoService.class 269B
InfoMapper.class 266B
.classpath 3KB
org.eclipse.wst.common.component 456B
org.eclipse.wst.jsdt.ui.superType.container 49B
zTreeStyle.css 6KB
datepicker.css 4KB
datepicker.css 4KB
areo_blue.css 3KB
areo_gray.css 3KB
default.css 3KB
index.css 3KB
chrome.css 3KB
main.css 2KB
notitle.css 2KB
WdatePicker.css 158B
login-content-bg.gif 20KB
login-wel.gif 9KB
menu_bg2.gif 4KB
lhgdg_bg.gif 3KB
titBtn.gif 3KB
titBtn.gif 3KB
titbtn.gif 2KB
lhgdg_btn.gif 2KB
menu_bgs.gif 2KB
menu_bg.gif 2KB
img.gif 2KB
img.gif 2KB
menu_bg1.gif 1KB
btn-bg2.gif 1KB
btn-bg.gif 1KB
menu_topline.gif 1KB
datePicker.gif 1KB
out.gif 859B
wait.gif 771B
wait.gif 771B
wait.gif 771B
wait.gif 771B
wait.gif 771B
nav-right-bg.gif 735B
page.gif 574B
top-right.gif 556B
left-top-right.gif 391B
buttom_right2.gif 383B
loading.gif 381B
buttom_left2.gif 353B
folder_Open.gif 341B
pic12.gif 309B
content-bg.gif 257B
folder_Close.gif 227B
buttom_bgs.gif 225B
plus_center.gif 212B
plus_bottom.gif 211B
plus_top.gif 211B
minus_center.gif 210B
menu_topimg.gif 209B
plus_root.gif 208B
minus_top.gif 208B
minus_bottom.gif 208B
lhgdg_x.gif 206B
plus_noLine.gif 205B
minus_root.gif 205B
minus_noLine.gif 203B
login-top-bg.gif 174B
mail_rightbg.gif 169B
news-title-bg.gif 166B
mail_leftbg.gif 131B
join.gif 80B
共 299 条
- 1
- 2
- 3
JAVA子弟
- 粉丝: 66
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页