update 修改MybatisPlus的配置:

1. 使用自定义主键生成策略
  2. 新增自动填充字段,createTime,updateTime,createUserId,updateUserId,createUsername,updateUsername,loginIp
master
管理员 1 year ago
parent 7835a5d6de
commit 6cb2f3c153

@ -1,12 +1,15 @@
package com.ruoyi.framework.config;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.framework.handler.CreateAndUpdateMetaObjectHandler;
import com.ruoyi.framework.interceptor.PlusDataPermissionInterceptor;
import org.mybatis.spring.annotation.MapperScan;
@ -24,79 +27,108 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@MapperScan("${mybatis-plus.mapperPackage}")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据权限处理
interceptor.addInnerInterceptor(dataPermissionInterceptor());
// 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
return interceptor;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据权限处理
interceptor.addInnerInterceptor(dataPermissionInterceptor());
// 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
return interceptor;
}
/**
*
*/
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
return new PlusDataPermissionInterceptor();
}
/**
*
*/
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
return new PlusDataPermissionInterceptor();
}
/**
*
*/
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
// 分页合理化
paginationInnerInterceptor.setOverflow(true);
return paginationInnerInterceptor;
}
/**
*
*/
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
// 分页合理化
paginationInnerInterceptor.setOverflow(true);
return paginationInnerInterceptor;
}
/**
*
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
}
/**
*
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
}
/**
*
*/
@Bean
public MetaObjectHandler metaObjectHandler() {
return new CreateAndUpdateMetaObjectHandler();
}
/**
*
*/
@Bean
public MetaObjectHandler metaObjectHandler() {
return new CreateAndUpdateMetaObjectHandler();
}
/**
* 使
* ID
*/
@Bean
public IdentifierGenerator idGenerator() {
return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
}
/**
* 使
* ID
*/
// @Bean
// public IdentifierGenerator idGenerator() {
// return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
// }
/**
* PaginationInnerInterceptor
* https://baomidou.com/pages/97710a/
* OptimisticLockerInnerInterceptor
* https://baomidou.com/pages/0d93c0/
* MetaObjectHandler
* https://baomidou.com/pages/4c6bcf/
* ISqlInjector sql
* https://baomidou.com/pages/42ea4a/
* BlockAttackInnerInterceptor
* https://baomidou.com/pages/f9a237/
* IllegalSQLInnerInterceptor sql(SQL)
* IdentifierGenerator
* https://baomidou.com/pages/568eb2/
* TenantLineInnerInterceptor
* https://baomidou.com/pages/aef2f2/
* DynamicTableNameInnerInterceptor
* https://baomidou.com/pages/2a45ff/
*/
/**
* 使id
* @return
*/
@Bean
public IdentifierGenerator idGenerator() {
return new IdentifierGenerator() {
@Override
public boolean assignId(Object idValue) {
if (idValue instanceof CharSequence) {
return StrUtil.isBlank((CharSequence) idValue);
}
return ObjUtil.isNull(idValue);
}
@Override
public Number nextId(Object entity) {
return IdUtils.nextDateIdLong(entity.getClass().getSimpleName(), 17);
}
@Override
public String nextUUID(Object entity) {
return IdUtils.nextDateId(entity.getClass().getSimpleName(), 17);
}
};
}
/**
* PaginationInnerInterceptor
* https://baomidou.com/pages/97710a/
* OptimisticLockerInnerInterceptor
* https://baomidou.com/pages/0d93c0/
* MetaObjectHandler
* https://baomidou.com/pages/4c6bcf/
* ISqlInjector sql
* https://baomidou.com/pages/42ea4a/
* BlockAttackInnerInterceptor
* https://baomidou.com/pages/f9a237/
* IllegalSQLInnerInterceptor sql(SQL)
* IdentifierGenerator
* https://baomidou.com/pages/568eb2/
* TenantLineInnerInterceptor
* https://baomidou.com/pages/aef2f2/
* DynamicTableNameInnerInterceptor
* https://baomidou.com/pages/2a45ff/
*/
}

@ -1,5 +1,7 @@
package com.ruoyi.framework.handler;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
@ -11,6 +13,7 @@ import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import java.beans.PropertyDescriptor;
import java.util.Date;
/**
@ -22,58 +25,83 @@ import java.util.Date;
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : new Date();
baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current);
String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : getLoginUsername();
// 当前已登录 且 创建人为空 则填充
baseEntity.setCreateBy(username);
// 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username);
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
@Override
public void insertFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : new Date();
baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current);
String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : getLoginUsername();
// 当前已登录 且 创建人为空 则填充
baseEntity.setCreateBy(username);
// 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username);
}
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
try {
LoginUser loginUser = LoginHelper.getLoginUser();
this.strictInsertFill(metaObject, "createUserId", Long.class, loginUser.getUserId());
this.strictInsertFill(metaObject, "createUsername", String.class, loginUser.getUsername());
this.strictInsertFill(metaObject, "loginIp", String.class, loginUser.getIpaddr());
} catch (Exception e) {
log.debug("自动注入警告 => 用户未登录");
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
@Override
public void updateFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = new Date();
// 更新时间填充(不管为不为空)
baseEntity.setUpdateTime(current);
String username = getLoginUsername();
// 当前已登录 更新人填充(不管为不为空)
if (StringUtils.isNotBlank(username)) {
baseEntity.setUpdateBy(username);
}
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
@Override
public void updateFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = new Date();
// 更新时间填充(不管为不为空)
baseEntity.setUpdateTime(current);
String username = getLoginUsername();
// 当前已登录 更新人填充(不管为不为空)
if (StringUtils.isNotBlank(username)) {
baseEntity.setUpdateBy(username);
}
}
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
try {
LoginUser loginUser = LoginHelper.getLoginUser();
this.strictUpdateFill(metaObject, "updateUserId", Long.class, loginUser.getUserId());
this.strictUpdateFill(metaObject, "updateUsername", String.class, loginUser.getUsername());
} catch (Exception e) {
log.debug("自动注入警告 => 用户未登录");
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
/**
*
*/
private String getLoginUsername() {
LoginUser loginUser;
try {
loginUser = LoginHelper.getLoginUser();
} catch (Exception e) {
log.warn("自动注入警告 => 用户未登录");
return null;
}
return loginUser.getUsername();
/**
*
*/
private String getLoginUsername() {
LoginUser loginUser;
try {
loginUser = LoginHelper.getLoginUser();
} catch (Exception e) {
log.warn("自动注入警告 => 用户未登录");
return null;
}
return loginUser.getUsername();
}
}

Loading…
Cancel
Save