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

update:修改mybatis扫包

parent e522626d
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.UserUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
......@@ -64,81 +65,86 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep
if(null == parameter){
return invocation.proceed();
}
boolean isNotIntercept = false;
// 获取对象中所有的私有成员变量(对应表字段)
Field[] declaredFields = parameter.getClass().getDeclaredFields();
if (parameter.getClass().getSuperclass() != null) {
if (parameter.getClass().getSuperclass().getCanonicalName().equals(CheckCertificateMainInfo.class.getCanonicalName()) ) {
isNotIntercept = true;
}
Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();
declaredFields = ArrayUtils.addAll(declaredFields, superField);
}
// mybatis plus判断
boolean plus= parameter.getClass().getDeclaredFields().length == 1 && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID");
if (!isNotIntercept) {
// mybatis plus判断
boolean plus = parameter.getClass().getDeclaredFields().length == 1 && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID");
//兼容mybatis plus
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
if(updateParam.containsKey("param1")&& StringUtils.isNotEmpty(updateParam.get("param1"))){
Class<?> updateParamType = updateParam.get("param1").getClass();
declaredFields = updateParamType.getDeclaredFields();
if (updateParamType.getSuperclass() != null) {
Field[] superField = updateParamType.getSuperclass().getDeclaredFields();
declaredFields = ArrayUtils.addAll(declaredFields, superField);
//兼容mybatis plus
if (plus) {
Map<String, Object> updateParam = (Map<String, Object>) parameter;
if (updateParam.containsKey("param1") && StringUtils.isNotEmpty(updateParam.get("param1"))) {
Class<?> updateParamType = updateParam.get("param1").getClass();
declaredFields = updateParamType.getDeclaredFields();
if (updateParamType.getSuperclass() != null) {
Field[] superField = updateParamType.getSuperclass().getDeclaredFields();
declaredFields = ArrayUtils.addAll(declaredFields, superField);
}
}
}
}
String currentUserId = UserUtil.getCurrentUserId();
String currentUserId = UserUtil.getCurrentUserId();
// if(StringUtils.isEmpty(loginUserEO)){
// throw new AdcDaBaseException("登录失效,请重新登录!");
// }
String fieldName = null;
for (Field field : declaredFields) {
fieldName = field.getName();
if (Objects.equals(CREATE_TIME, fieldName)) {
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 {
String fieldName = null;
for (Field field : declaredFields) {
fieldName = field.getName();
if (Objects.equals(CREATE_TIME, fieldName)) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
try {
field.setAccessible(true);
field.set(parameter, new Timestamp(System.currentTimeMillis()));
} catch (Exception e) {
log.warn("添加修改时间报错",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 {
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 {
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){
// if(m.getName().equals(methedName)){
......@@ -149,6 +155,7 @@ public class SqlInterceptor extends AbstractSqlParserHandler implements Intercep
// }
// }
// }
}
return invocation.proceed();
}
......
......@@ -90,12 +90,12 @@
<version>${mysql.driver.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>${oracle.driver.version}</version>
<scope>runtime</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.oracle</groupId>-->
<!-- <artifactId>ojdbc14</artifactId>-->
<!-- <version>${oracle.driver.version}</version>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<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