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; package com.ruoyi.framework.config;
import cn.hutool.core.net.NetUtil; 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.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator; import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.framework.handler.CreateAndUpdateMetaObjectHandler; import com.ruoyi.framework.handler.CreateAndUpdateMetaObjectHandler;
import com.ruoyi.framework.interceptor.PlusDataPermissionInterceptor; import com.ruoyi.framework.interceptor.PlusDataPermissionInterceptor;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
@ -24,79 +27,108 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@MapperScan("${mybatis-plus.mapperPackage}") @MapperScan("${mybatis-plus.mapperPackage}")
public class MybatisPlusConfig { public class MybatisPlusConfig {
@Bean @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() { public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据权限处理 // 数据权限处理
interceptor.addInnerInterceptor(dataPermissionInterceptor()); interceptor.addInnerInterceptor(dataPermissionInterceptor());
// 分页插件 // 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor()); interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件 // 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
return interceptor; return interceptor;
} }
/** /**
* *
*/ */
public PlusDataPermissionInterceptor dataPermissionInterceptor() { public PlusDataPermissionInterceptor dataPermissionInterceptor() {
return new PlusDataPermissionInterceptor(); return new PlusDataPermissionInterceptor();
} }
/** /**
* *
*/ */
public PaginationInnerInterceptor paginationInnerInterceptor() { public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置最大单页限制数量,默认 500 条,-1 不受限制 // 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L); paginationInnerInterceptor.setMaxLimit(-1L);
// 分页合理化 // 分页合理化
paginationInnerInterceptor.setOverflow(true); paginationInnerInterceptor.setOverflow(true);
return paginationInnerInterceptor; return paginationInnerInterceptor;
} }
/** /**
* *
*/ */
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor(); return new OptimisticLockerInnerInterceptor();
} }
/** /**
* *
*/ */
@Bean @Bean
public MetaObjectHandler metaObjectHandler() { public MetaObjectHandler metaObjectHandler() {
return new CreateAndUpdateMetaObjectHandler(); return new CreateAndUpdateMetaObjectHandler();
} }
/** /**
* 使 * 使
* ID * ID
*/ */
@Bean // @Bean
public IdentifierGenerator idGenerator() { // public IdentifierGenerator idGenerator() {
return new DefaultIdentifierGenerator(NetUtil.getLocalhost()); // return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
} // }
/** /**
* PaginationInnerInterceptor * 使id
* https://baomidou.com/pages/97710a/ * @return
* OptimisticLockerInnerInterceptor */
* https://baomidou.com/pages/0d93c0/ @Bean
* MetaObjectHandler public IdentifierGenerator idGenerator() {
* https://baomidou.com/pages/4c6bcf/ return new IdentifierGenerator() {
* ISqlInjector sql
* https://baomidou.com/pages/42ea4a/ @Override
* BlockAttackInnerInterceptor public boolean assignId(Object idValue) {
* https://baomidou.com/pages/f9a237/ if (idValue instanceof CharSequence) {
* IllegalSQLInnerInterceptor sql(SQL) return StrUtil.isBlank((CharSequence) idValue);
* IdentifierGenerator }
* https://baomidou.com/pages/568eb2/ return ObjUtil.isNull(idValue);
* TenantLineInnerInterceptor }
* https://baomidou.com/pages/aef2f2/
* DynamicTableNameInnerInterceptor @Override
* https://baomidou.com/pages/2a45ff/ 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; 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.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus; import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
@ -11,6 +13,7 @@ import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.MetaObject;
import java.beans.PropertyDescriptor;
import java.util.Date; import java.util.Date;
/** /**
@ -22,58 +25,83 @@ import java.util.Date;
@Slf4j @Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler { public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
@Override @Override
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
try { try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) { if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject(); BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : new Date(); ? baseEntity.getCreateTime() : new Date();
baseEntity.setCreateTime(current); baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current); baseEntity.setUpdateTime(current);
String username = StringUtils.isNotBlank(baseEntity.getCreateBy()) String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : getLoginUsername(); ? baseEntity.getCreateBy() : getLoginUsername();
// 当前已登录 且 创建人为空 则填充 // 当前已登录 且 创建人为空 则填充
baseEntity.setCreateBy(username); baseEntity.setCreateBy(username);
// 当前已登录 且 更新人为空 则填充 // 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username); baseEntity.setUpdateBy(username);
} }
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); 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) { @Override
try { public void updateFill(MetaObject metaObject) {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) { try {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject(); if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
Date current = new Date(); BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
// 更新时间填充(不管为不为空) Date current = new Date();
baseEntity.setUpdateTime(current); // 更新时间填充(不管为不为空)
String username = getLoginUsername(); baseEntity.setUpdateTime(current);
// 当前已登录 更新人填充(不管为不为空) String username = getLoginUsername();
if (StringUtils.isNotBlank(username)) { // 当前已登录 更新人填充(不管为不为空)
baseEntity.setUpdateBy(username); if (StringUtils.isNotBlank(username)) {
} baseEntity.setUpdateBy(username);
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
} }
}
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() { private String getLoginUsername() {
LoginUser loginUser; LoginUser loginUser;
try { try {
loginUser = LoginHelper.getLoginUser(); loginUser = LoginHelper.getLoginUser();
} catch (Exception e) { } catch (Exception e) {
log.warn("自动注入警告 => 用户未登录"); log.warn("自动注入警告 => 用户未登录");
return null; return null;
}
return loginUser.getUsername();
} }
return loginUser.getUsername();
}
} }

Loading…
Cancel
Save