【feat】调整mybatis-plus配置,对sql进行增强
This commit is contained in:
parent
f712403d93
commit
4199b1b1f3
@ -6,6 +6,7 @@ import com.czsj.common.core.domain.BaseEntity;
|
||||
import com.czsj.common.core.domain.model.LoginUser;
|
||||
import com.czsj.common.utils.SecurityUtils;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
@ -18,52 +19,31 @@ import java.util.Objects;
|
||||
*
|
||||
* @author hexiaowu
|
||||
*/
|
||||
@Component
|
||||
public class DefaultDBFieldHandler implements MetaObjectHandler {
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
|
||||
BaseEntity BaseEntity = (BaseEntity) metaObject.getOriginalObject();
|
||||
// 创建时间为空,则以当前时间为插入时间
|
||||
if (Objects.isNull(BaseEntity.getCreateTime())) {
|
||||
BaseEntity.setCreateTime(new Date());
|
||||
}
|
||||
// 更新时间为空,则以当前时间为更新时间
|
||||
if (Objects.isNull(BaseEntity.getUpdateTime())) {
|
||||
BaseEntity.setUpdateTime(new Date());
|
||||
}
|
||||
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
Long userId = loginUser.getUserId();
|
||||
// 当前登录用户不为空,创建人为空,则当前登录用户为创建人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(BaseEntity.getCreateBy())) {
|
||||
BaseEntity.setCreateBy(userId.toString());
|
||||
}
|
||||
// 当前登录用户不为空,更新人为空,则当前登录用户为更新人
|
||||
if (Objects.nonNull(userId) && Objects.isNull(BaseEntity.getUpdateBy())) {
|
||||
BaseEntity.setUpdateBy(userId.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// 创建时间为空,则以当前时间为创建时间
|
||||
Object creatTime = getFieldValByName("creatTime", metaObject);
|
||||
if (Objects.isNull(creatTime)) {
|
||||
setFieldValByName("creatTime", new Date(), metaObject);
|
||||
Object createTime = getFieldValByName("createTime", metaObject);
|
||||
if (Objects.isNull(createTime)) {
|
||||
setFieldValByName("createTime", new Date(), metaObject);
|
||||
}
|
||||
// 更新时间为空,则以当前时间为更新时间
|
||||
Object modifyTime = getFieldValByName("updateTime", metaObject);
|
||||
if (Objects.isNull(modifyTime)) {
|
||||
setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
|
||||
setFieldValByName("updateTime", new Date(), metaObject);
|
||||
}
|
||||
Object modifier = getFieldValByName("updateUserId", metaObject);
|
||||
Object creater = getFieldValByName("createUserId", metaObject);
|
||||
Object modifier = getFieldValByName("updateBy", metaObject);
|
||||
Object creater = getFieldValByName("createBy", metaObject);
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
Long userId = loginUser.getUserId();
|
||||
if (Objects.nonNull(userId) && Objects.isNull(modifier)) {
|
||||
setFieldValByName("updateUserId", userId.toString(), metaObject);
|
||||
setFieldValByName("updateBy", userId.toString(), metaObject);
|
||||
}
|
||||
if (Objects.nonNull(userId) && Objects.isNull(creater)) {
|
||||
setFieldValByName("createUserId", userId.toString(), metaObject);
|
||||
setFieldValByName("createBy", userId.toString(), metaObject);
|
||||
}
|
||||
|
||||
}
|
||||
@ -75,13 +55,13 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
|
||||
if (Objects.isNull(modifyTime)) {
|
||||
setFieldValByName("updateTime",new Date(), metaObject);
|
||||
}
|
||||
|
||||
// 当前登录用户不为空,更新人为空,则当前登录用户为更新人
|
||||
Object modifier = getFieldValByName("updateUserId", metaObject);
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
Long userId = loginUser.getUserId();
|
||||
|
||||
// 当前登录用户不为空,更新人为空,则当前登录用户为更新人
|
||||
Object modifier = getFieldValByName("updateBy", metaObject);
|
||||
if (Objects.nonNull(userId) && Objects.isNull(modifier)) {
|
||||
setFieldValByName("updateUserId", userId.toString(), metaObject);
|
||||
setFieldValByName("updateBy", userId.toString(), metaObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.czsj.common.handler.DefaultDBFieldHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@ -29,7 +28,7 @@ public class MybatisPlusConfig
|
||||
// 乐观锁插件
|
||||
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
||||
// 阻断插件
|
||||
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
|
||||
// interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@ -62,8 +61,5 @@ public class MybatisPlusConfig
|
||||
return new BlockAttackInnerInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MetaObjectHandler defaultMetaObjectHandler() {
|
||||
return new DefaultDBFieldHandler(); // 自动填充参数类
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user