|
|
@ -12,8 +12,10 @@ import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; |
|
|
|
import org.apache.ibatis.session.ResultHandler; |
|
|
|
import org.apache.ibatis.session.RowBounds; |
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
/** |
|
|
@ -34,13 +36,41 @@ public class MybatisInterceptor implements Interceptor { |
|
|
|
|
|
|
|
|
|
|
|
String selectByExample = "selectByExample"; |
|
|
|
String countByExample = "countByExample"; |
|
|
|
String countByExample2 = "selectByExample_COUNT"; |
|
|
|
String selectByPrimaryKey = "selectByPrimaryKey"; |
|
|
|
|
|
|
|
Object[] args = invocation.getArgs(); |
|
|
|
MappedStatement statement = (MappedStatement) args[0]; |
|
|
|
if (statement.getId().endsWith(selectByExample)) { |
|
|
|
if (statement.getId().endsWith(selectByExample) |
|
|
|
|| statement.getId().endsWith(countByExample) |
|
|
|
|| statement.getId().endsWith(countByExample2)) { |
|
|
|
//XXXExample
|
|
|
|
Object example = args[1]; |
|
|
|
|
|
|
|
addCondition(statement, example); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (statement.getId().endsWith(selectByPrimaryKey)) { |
|
|
|
BoundSql boundSql = statement.getBoundSql(args[1]); |
|
|
|
String sql = boundSql.getSql() + " and rec_status = 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(); |
|
|
|
} |
|
|
|
|
|
|
|
private void addCondition(MappedStatement statement, Object example) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException { |
|
|
|
if (example instanceof Map) { |
|
|
|
example = ((Map) example).get("_ORIGINAL_PARAMETER_OBJECT"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Method method = example.getClass().getMethod("getOredCriteria", null); |
|
|
|
//获取到条件数组,第一个是Criteria
|
|
|
|
List list = (List) method.invoke(example); |
|
|
@ -50,8 +80,8 @@ public class MybatisInterceptor implements Interceptor { |
|
|
|
Object paramExample = Class.forName(exampleName).newInstance(); |
|
|
|
Method createCriteria = paramExample.getClass().getMethod("createCriteria"); |
|
|
|
Object criteria = createCriteria.invoke(paramExample); |
|
|
|
Method andIsDelEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class); |
|
|
|
andIsDelEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value); |
|
|
|
Method andRecStatusEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class); |
|
|
|
andRecStatusEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value); |
|
|
|
list.add(criteria); |
|
|
|
} else { |
|
|
|
Object criteria = list.get(0); |
|
|
@ -61,28 +91,17 @@ public class MybatisInterceptor implements Interceptor { |
|
|
|
for (Object param : params) { |
|
|
|
Method getCondition = param.getClass().getMethod("getCondition"); |
|
|
|
Object condition = getCondition.invoke(param); |
|
|
|
if ("iis_del =".equals(condition)) { |
|
|
|
if ("rec_status =".equals(condition)) { |
|
|
|
hasDel = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!hasDel) { |
|
|
|
Method andIsDelEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class); |
|
|
|
andIsDelEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value); |
|
|
|
} |
|
|
|
|
|
|
|
Method andRecStatusEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class); |
|
|
|
andRecStatusEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else if (statement.getId().endsWith(selectByPrimaryKey)) { |
|
|
|
BoundSql boundSql = statement.getBoundSql(args[1]); |
|
|
|
String sql = boundSql.getSql() + " and rec_status = 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 |
|
|
@ -121,24 +140,7 @@ public class MybatisInterceptor implements Interceptor { |
|
|
|
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; |
|
|
|