35 changed files with 409 additions and 144 deletions
@ -1,20 +0,0 @@ |
|||
//package com.ccsens.ht.bean;
|
|||
//
|
|||
//import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
|||
//import org.springframework.context.annotation.Bean;
|
|||
//import org.springframework.context.annotation.Configuration;
|
|||
//import org.springframework.web.client.RestTemplate;
|
|||
//
|
|||
///**
|
|||
// * @description:
|
|||
// * @author: wuHuiJuan
|
|||
// * @create: 2019/11/26 15:45
|
|||
// */
|
|||
//@Configuration
|
|||
//public class BeanConfig {
|
|||
// @Bean
|
|||
// @LoadBalanced
|
|||
// public RestTemplate restTemplate() {
|
|||
// return new RestTemplate();
|
|||
// }
|
|||
//}
|
@ -0,0 +1,22 @@ |
|||
package com.ccsens.ht.config; |
|||
|
|||
import com.ccsens.ht.intercept.MybatisInterceptor; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/11 10:59 |
|||
*/ |
|||
@Configuration |
|||
public class BeanConfig { |
|||
/** |
|||
* 注册拦截器 |
|||
*/ |
|||
@Bean |
|||
public MybatisInterceptor mybatisInterceptor() { |
|||
MybatisInterceptor interceptor = new MybatisInterceptor(); |
|||
return interceptor; |
|||
} |
|||
} |
@ -0,0 +1,220 @@ |
|||
package com.ccsens.ht.intercept; |
|||
|
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import com.ccsens.ht.bean.po.HtDoctorExample; |
|||
import com.ccsens.ht.uitl.Constant; |
|||
import org.apache.ibatis.binding.MapperMethod; |
|||
import org.apache.ibatis.cache.CacheKey; |
|||
import org.apache.ibatis.executor.CachingExecutor; |
|||
import org.apache.ibatis.executor.Executor; |
|||
import org.apache.ibatis.executor.parameter.ParameterHandler; |
|||
import org.apache.ibatis.executor.statement.PreparedStatementHandler; |
|||
import org.apache.ibatis.executor.statement.StatementHandler; |
|||
import org.apache.ibatis.mapping.*; |
|||
import org.apache.ibatis.plugin.*; |
|||
import org.apache.ibatis.reflection.DefaultReflectorFactory; |
|||
import org.apache.ibatis.reflection.MetaObject; |
|||
import org.apache.ibatis.reflection.factory.DefaultObjectFactory; |
|||
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; |
|||
import org.apache.ibatis.session.Configuration; |
|||
import org.apache.ibatis.session.ResultHandler; |
|||
import org.apache.ibatis.session.RowBounds; |
|||
import org.springframework.beans.BeanUtils; |
|||
import org.springframework.util.ClassUtils; |
|||
|
|||
import java.beans.PropertyDescriptor; |
|||
import java.lang.reflect.Field; |
|||
import java.lang.reflect.InvocationTargetException; |
|||
import java.lang.reflect.Method; |
|||
import java.sql.PreparedStatement; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Properties; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/11 10:58 |
|||
*/ |
|||
@Intercepts({ |
|||
@Signature( |
|||
type = Executor.class, |
|||
method = "query", |
|||
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} |
|||
)/*, |
|||
@Signature( |
|||
type = Executor.class, |
|||
method = "query", |
|||
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class} |
|||
)*/ |
|||
/*@Signature( |
|||
type = ParameterHandler.class, |
|||
method = "setParameters", |
|||
args = {PreparedStatement.class} |
|||
)*/ |
|||
}) |
|||
public class MybatisInterceptor implements Interceptor { |
|||
private static final String PARAM_KEY = "is_del"; |
|||
@Override |
|||
public Object intercept(Invocation invocation) throws Throwable { |
|||
|
|||
// Object[] args = invocation.getArgs();
|
|||
// MappedStatement statement = (MappedStatement) args[0];
|
|||
// BoundSql boundSql = statement.getBoundSql(args.length > 1 ? args[1] : null);
|
|||
// String sql = boundSql.getSql();
|
|||
// String whereDel = "is_del";
|
|||
// int whereIndex = sql.indexOf("where") > 0 ? sql.indexOf("where") : sql.indexOf("WHERE");
|
|||
// boolean hasParam = true;
|
|||
// if (whereIndex < 0) {
|
|||
// hasParam = false;
|
|||
// sql += " where ";
|
|||
// whereIndex = sql.indexOf("where");
|
|||
// }
|
|||
// //sql中不包含is_del,则添加
|
|||
// if (!sql.substring(whereIndex).contains(whereDel)) {
|
|||
// StringBuilder sqlBuilder = new StringBuilder();
|
|||
// sqlBuilder.append(sql.substring(0, whereIndex + "where".length()))
|
|||
// .append(hasParam ? " is_del = 0 and " : " is_del = 0 ")
|
|||
// .append(sql.substring(whereIndex + "where".length()));
|
|||
// //通过反射修改sql语句
|
|||
//// Field field = boundSql.getClass().getDeclaredField("sql");
|
|||
//// field.setAccessible(true);
|
|||
//// field.set(boundSql, builder.toString());
|
|||
//
|
|||
// System.out.println("------------------");
|
|||
// List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
|||
// Configuration configuration = statement.getConfiguration();
|
|||
// BoundSql newBoundSql = new BoundSql(configuration, sqlBuilder.toString(), parameterMappings, boundSql.getParameterObject());
|
|||
// for (ParameterMapping parameterMapping : parameterMappings) {
|
|||
// String prop = parameterMapping.getProperty();
|
|||
// if (boundSql.hasAdditionalParameter(prop)) {
|
|||
// Object param = boundSql.getAdditionalParameter(prop);
|
|||
// newBoundSql.setAdditionalParameter(prop, param);
|
|||
// }
|
|||
// }
|
|||
// BoundSqlSource newSqlSource = new BoundSqlSource(newBoundSql);
|
|||
// MappedStatement newMappedStatement = copyFromMappedStatement(
|
|||
// statement, newSqlSource);
|
|||
// args[MAPPED_STATEMENT_INDEX] = newMappedStatement;
|
|||
// }
|
|||
|
|||
String selectByExample = "selectByExample"; |
|||
String selectByPrimaryKey = "selectByPrimaryKey"; |
|||
|
|||
Object[] args = invocation.getArgs(); |
|||
MappedStatement statement = (MappedStatement) args[0]; |
|||
if (statement.getId().endsWith(selectByExample)) { |
|||
//XXXExample
|
|||
Object example = args[1]; |
|||
Method method = example.getClass().getMethod("getOredCriteria", null); |
|||
//获取到条件数组,第一个是Criteria
|
|||
List list = (List)method.invoke(example); |
|||
if (CollectionUtil.isEmpty(list)) { |
|||
Class clazz = ((ResultMap)statement.getResultMaps().get(0)).getType(); |
|||
String exampleName = clazz.getName() + "Example"; |
|||
Object paramExample = Class.forName(exampleName).newInstance(); |
|||
Method createCriteria = paramExample.getClass().getMethod("createCriteria"); |
|||
Object criteria = createCriteria.invoke(paramExample); |
|||
Method andIsDelEqualTo = criteria.getClass().getMethod("andIsDelEqualTo", Byte.class); |
|||
andIsDelEqualTo.invoke(criteria, Constant.Ht.NUMBER_DEFAULT); |
|||
list.add(criteria); |
|||
} else { |
|||
Object criteria = list.get(0); |
|||
Method getCriteria = criteria.getClass().getMethod("getCriteria"); |
|||
List params = (List)getCriteria.invoke(criteria); |
|||
boolean hasDel = false; |
|||
for(Object param: params) { |
|||
Method getCondition = param.getClass().getMethod("getCondition"); |
|||
Object condition = getCondition.invoke(param); |
|||
if ("iis_del =".equals(condition)) { |
|||
hasDel = true; |
|||
} |
|||
} |
|||
if (!hasDel) { |
|||
Method andIsDelEqualTo = criteria.getClass().getMethod("andIsDelEqualTo", Byte.class); |
|||
andIsDelEqualTo.invoke(criteria, Constant.Ht.NUMBER_DEFAULT); |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
} else if (statement.getId().endsWith(selectByPrimaryKey)) { |
|||
BoundSql boundSql = statement.getBoundSql(args[1]); |
|||
String sql = boundSql.getSql() + " and is_del = 0"; |
|||
MappedStatement newStatement = newMappedStatement(statement, new BoundSqlSqlSource(boundSql)); |
|||
MetaObject msObject = MetaObject.forObject(newStatement, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),new DefaultReflectorFactory()); |
|||
msObject.setValue("sqlSource.boundSql.sql", sql); |
|||
args[0] = newStatement; |
|||
} |
|||
|
|||
return invocation.proceed(); |
|||
} |
|||
|
|||
@Override |
|||
public Object plugin(Object target) { |
|||
return Plugin.wrap(target, this); |
|||
} |
|||
|
|||
@Override |
|||
public void setProperties(Properties properties) { |
|||
|
|||
} |
|||
|
|||
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) { |
|||
MappedStatement.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()); |
|||
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { |
|||
StringBuilder keyProperties = new StringBuilder(); |
|||
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()); |
|||
builder.resultMaps(ms.getResultMaps()); |
|||
builder.resultSetType(ms.getResultSetType()); |
|||
builder.cache(ms.getCache()); |
|||
builder.flushCacheRequired(ms.isFlushCacheRequired()); |
|||
builder.useCache(ms.isUseCache()); |
|||
|
|||
return builder.build(); |
|||
} |
|||
|
|||
private String getOperateType(Invocation invocation) { |
|||
final Object[] args = invocation.getArgs(); |
|||
MappedStatement ms = (MappedStatement) args[0]; |
|||
SqlCommandType commondType = ms.getSqlCommandType(); |
|||
if (commondType.compareTo(SqlCommandType.SELECT) == 0) { |
|||
return "select"; |
|||
} |
|||
if (commondType.compareTo(SqlCommandType.INSERT) == 0) { |
|||
return "insert"; |
|||
} |
|||
if (commondType.compareTo(SqlCommandType.UPDATE) == 0) { |
|||
return "update"; |
|||
} |
|||
if (commondType.compareTo(SqlCommandType.DELETE) == 0) { |
|||
return "delete"; |
|||
} |
|||
return null; |
|||
} |
|||
// 定义一个内部辅助类,作用是包装sq
|
|||
class BoundSqlSqlSource implements SqlSource { |
|||
private BoundSql boundSql; |
|||
public BoundSqlSqlSource(BoundSql boundSql) { |
|||
this.boundSql = boundSql; |
|||
} |
|||
@Override |
|||
public BoundSql getBoundSql(Object parameterObject) { |
|||
return boundSql; |
|||
} |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue