Commit 5efd5675 authored by 凡昌松's avatar 凡昌松

update:修改mybatis扫包

parent e522626d
package com.adc.da.main.aspect; package com.adc.da.main.aspect;
import com.adc.da.system.entity.CheckCertificateMainInfo;
import com.adc.da.util.utils.StringUtils; import com.adc.da.util.utils.StringUtils;
import com.adc.da.util.utils.UserUtil; import com.adc.da.util.utils.UserUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
...@@ -64,81 +65,86 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep ...@@ -64,81 +65,86 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep
if(null == parameter){ if(null == parameter){
return invocation.proceed(); return invocation.proceed();
} }
boolean isNotIntercept = false;
// 获取对象中所有的私有成员变量(对应表字段) // 获取对象中所有的私有成员变量(对应表字段)
Field[] declaredFields = parameter.getClass().getDeclaredFields(); Field[] declaredFields = parameter.getClass().getDeclaredFields();
if (parameter.getClass().getSuperclass() != null) { if (parameter.getClass().getSuperclass() != null) {
if (parameter.getClass().getSuperclass().getCanonicalName().equals(CheckCertificateMainInfo.class.getCanonicalName()) ) {
isNotIntercept = true;
}
Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields(); Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();
declaredFields = ArrayUtils.addAll(declaredFields, superField); declaredFields = ArrayUtils.addAll(declaredFields, superField);
} }
// mybatis plus判断 if (!isNotIntercept) {
boolean plus= parameter.getClass().getDeclaredFields().length == 1 && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID"); // mybatis plus判断
boolean plus = parameter.getClass().getDeclaredFields().length == 1 && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID");
//兼容mybatis plus //兼容mybatis plus
if (plus) { if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter; Map<String, Object> updateParam = (Map<String, Object>) parameter;
if(updateParam.containsKey("param1")&& StringUtils.isNotEmpty(updateParam.get("param1"))){ if (updateParam.containsKey("param1") && StringUtils.isNotEmpty(updateParam.get("param1"))) {
Class<?> updateParamType = updateParam.get("param1").getClass(); Class<?> updateParamType = updateParam.get("param1").getClass();
declaredFields = updateParamType.getDeclaredFields(); declaredFields = updateParamType.getDeclaredFields();
if (updateParamType.getSuperclass() != null) { if (updateParamType.getSuperclass() != null) {
Field[] superField = updateParamType.getSuperclass().getDeclaredFields(); Field[] superField = updateParamType.getSuperclass().getDeclaredFields();
declaredFields = ArrayUtils.addAll(declaredFields, superField); declaredFields = ArrayUtils.addAll(declaredFields, superField);
}
} }
} }
} String currentUserId = UserUtil.getCurrentUserId();
String currentUserId = UserUtil.getCurrentUserId();
// if(StringUtils.isEmpty(loginUserEO)){ // if(StringUtils.isEmpty(loginUserEO)){
// throw new AdcDaBaseException("登录失效,请重新登录!"); // throw new AdcDaBaseException("登录失效,请重新登录!");
// } // }
String fieldName = null; String fieldName = null;
for (Field field : declaredFields) { for (Field field : declaredFields) {
fieldName = field.getName(); fieldName = field.getName();
if (Objects.equals(CREATE_TIME, fieldName)) { if (Objects.equals(CREATE_TIME, fieldName)) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) { if (SqlCommandType.INSERT.equals(sqlCommandType)) {
try {
field.setAccessible(true);
field.set(parameter, new Timestamp(System.currentTimeMillis()));
} catch (Exception e) {
log.warn("添加创建时间报错",e);
}
}
}
if (Objects.equals(CREATE_USER_ID, fieldName)&& StringUtils.isNotEmpty(currentUserId)) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter,currentUserId);
}
}
if (Objects.equals(UPDATE_TIME, fieldName)) {
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
//兼容mybatis plus的update
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
field.set(updateParam.get("param1"), new Timestamp(System.currentTimeMillis()));
} else {
try { try {
field.setAccessible(true);
field.set(parameter, new Timestamp(System.currentTimeMillis())); field.set(parameter, new Timestamp(System.currentTimeMillis()));
} catch (Exception e) { } catch (Exception e) {
log.warn("添加修改时间报错",e); log.warn("添加创建时间报错", e);
} }
} }
} }
} if (Objects.equals(CREATE_USER_ID, fieldName) && StringUtils.isNotEmpty(currentUserId)) {
if (Objects.equals(UPDATE_USER_ID, fieldName)&& StringUtils.isNotEmpty(currentUserId) ){ if (SqlCommandType.INSERT.equals(sqlCommandType)) {
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) { field.setAccessible(true);
field.setAccessible(true);
//兼容mybatis plus的update
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
field.set(updateParam.get("param1"), currentUserId);
} else {
field.set(parameter, currentUserId); field.set(parameter, currentUserId);
} }
} }
if (Objects.equals(UPDATE_TIME, fieldName)) {
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
//兼容mybatis plus的update
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
field.set(updateParam.get("param1"), new Timestamp(System.currentTimeMillis()));
} else {
try {
field.set(parameter, new Timestamp(System.currentTimeMillis()));
} catch (Exception e) {
log.warn("添加修改时间报错", e);
}
}
}
}
if (Objects.equals(UPDATE_USER_ID, fieldName) && StringUtils.isNotEmpty(currentUserId)) {
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
//兼容mybatis plus的update
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
field.set(updateParam.get("param1"), currentUserId);
} else {
field.set(parameter, currentUserId);
}
}
}
} }
}
// //校验登录用户 // //校验登录用户
// for(Method m : ms){ // for(Method m : ms){
// if(m.getName().equals(methedName)){ // if(m.getName().equals(methedName)){
...@@ -149,6 +155,7 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep ...@@ -149,6 +155,7 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep
// } // }
// } // }
// } // }
}
return invocation.proceed(); return invocation.proceed();
} }
......
...@@ -90,12 +90,12 @@ ...@@ -90,12 +90,12 @@
<version>${mysql.driver.version}</version> <version>${mysql.driver.version}</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>com.oracle</groupId> <!-- <groupId>com.oracle</groupId>-->
<artifactId>ojdbc14</artifactId> <!-- <artifactId>ojdbc14</artifactId>-->
<version>${oracle.driver.version}</version> <!-- <version>${oracle.driver.version}</version>-->
<scope>runtime</scope> <!-- <scope>runtime</scope>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment