注册全局数据填充监听器

This commit is contained in:
dataprince 2023-09-10 19:07:00 +08:00
parent 01f30b9e89
commit de31954c6f
7 changed files with 85 additions and 78 deletions

View File

@ -1,8 +1,18 @@
package com.ruoyi.common.orm.config; package com.ruoyi.common.orm.config;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.datasource.DataSourceDecipher; import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import com.ruoyi.common.orm.core.domain.BaseEntity;
import com.ruoyi.common.orm.decipher.Decipher; import com.ruoyi.common.orm.decipher.Decipher;
import com.ruoyi.common.orm.listener.EntityInsertListener;
import com.ruoyi.common.orm.listener.EntityUpdateListener;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -15,26 +25,25 @@ import org.springframework.context.annotation.Configuration;
* *
* @author dataprince数据小王子 * @author dataprince数据小王子
*/ */
//@EnableTransactionManagement(proxyTargetClass = true)
@Configuration @Configuration
@MapperScan("${mybatis-flex.mapperPackage}") @MapperScan("${mybatis-flex.mapperPackage}")
public class MyBatisFlexConfig { public class MyBatisFlexConfig implements ConfigurationCustomizer, MyBatisFlexCustomizer {
private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql"); private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql");
/** // /**
* SQL 打印 // * SQL 打印
*/ // */
public MyBatisFlexConfig() { // public MyBatisFlexConfig() {
//开启审计功能 // //开启审计功能
AuditManager.setAuditEnable(true); // AuditManager.setAuditEnable(true);
//
//设置 SQL 审计收集器 // //设置 SQL 审计收集器
AuditManager.setMessageCollector(auditMessage -> // AuditManager.setMessageCollector(auditMessage ->
logger.info("{},耗费{}ms", auditMessage.getFullSql() // logger.info("{},耗费{}ms", auditMessage.getFullSql()
, auditMessage.getElapsedTime()) // , auditMessage.getElapsedTime())
); // );
} // }
/** /**
* 数据源解密 * 数据源解密
@ -45,6 +54,31 @@ public class MyBatisFlexConfig {
return decipher; return decipher;
} }
@Override
public void customize(FlexConfiguration configuration) {
//mybatis实现的打印sql到控制台便于调试
configuration.setLogImpl(StdOutImpl.class);
}
/**
* Mybatis-Flex自定义初始化配置
* @param globalConfig 全局配置
*/
@Override
public void customize(FlexGlobalConfig globalConfig) {
//统一设置数据库表主键为雪花算法
FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
keyConfig.setKeyType(KeyType.Generator);
keyConfig.setValue(KeyGenerators.snowFlakeId);
keyConfig.setBefore(true);
globalConfig.setKeyConfig(keyConfig);
// 注册全局数据填充监听器
globalConfig.registerInsertListener(new EntityInsertListener(), BaseEntity.class);
globalConfig.registerUpdateListener(new EntityUpdateListener(), BaseEntity.class);
}
//TODO:动态表名 //TODO:动态表名
//TODO:多租户配置 //TODO:多租户配置

View File

@ -1,28 +0,0 @@
package com.ruoyi.common.orm.config;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.springframework.context.annotation.Configuration;
/**
* Mybatis-Flex自定义初始化配置
*
* @author dataprince数据小王子
*/
@Configuration
public class MyBatisFlexInitConfig implements MyBatisFlexCustomizer {
@Override
public void customize(FlexGlobalConfig globalConfig) {
//我们可以在这里进行一些初始化配置
//统一设置数据库表主键为雪花算法
FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
keyConfig.setKeyType(KeyType.Generator);
keyConfig.setValue(KeyGenerators.snowFlakeId);
keyConfig.setBefore(true);
globalConfig.setKeyConfig(keyConfig);
}
}

View File

@ -1,20 +0,0 @@
package com.ruoyi.common.orm.config;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.context.annotation.Configuration;
/**
* 指定输出日志
*
* @author dataprince数据小王子
*/
@Configuration
public class MyConfigurationCustomizer implements ConfigurationCustomizer {
@Override
public void customize(FlexConfiguration configuration) {
//mybatis实现的打印sql到控制台便于调试
configuration.setLogImpl(StdOutImpl.class);
}
}

View File

@ -1,13 +1,16 @@
package com.ruoyi.common.orm.listener; package com.ruoyi.common.orm.listener;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.mybatisflex.annotation.InsertListener; import com.mybatisflex.annotation.InsertListener;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import java.util.Date; import java.util.Date;
/** /**
* Entity实体类新增数据监听器 * Entity实体类全局插入数据监听器
* *
* @author dataprince数据小王子 * @author dataprince数据小王子
*/ */
@ -15,12 +18,22 @@ public class EntityInsertListener implements InsertListener {
@Override @Override
public void onInsert(Object entity) { public void onInsert(Object entity) {
try {
if (ObjectUtil.isNotNull(entity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
Long loginUserId = LoginHelper.getUserId(); Long loginUserId = LoginHelper.getUserId();
Date createTime = new Date();
Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : new Date();
baseEntity.setCreateBy(loginUserId); baseEntity.setCreateBy(loginUserId);
baseEntity.setCreateTime(createTime); baseEntity.setCreateTime(createTime);
baseEntity.setUpdateBy(loginUserId); baseEntity.setUpdateBy(loginUserId);
baseEntity.setUpdateTime(createTime); baseEntity.setUpdateTime(createTime);
} }
} catch (Exception e) {
throw new ServiceException("全局插入数据监听器注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
} }

View File

@ -1,21 +1,31 @@
package com.ruoyi.common.orm.listener; package com.ruoyi.common.orm.listener;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.mybatisflex.annotation.UpdateListener; import com.mybatisflex.annotation.UpdateListener;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import java.util.Date; import java.util.Date;
/** /**
* Entity实体类更新数据监听器 * Entity实体类全局更新数据监听器
* *
* @author dataprince数据小王子 * @author dataprince数据小王子
*/ */
public class EntityUpdateListener implements UpdateListener { public class EntityUpdateListener implements UpdateListener {
@Override @Override
public void onUpdate(Object entity) { public void onUpdate(Object entity) {
try {
if (ObjectUtil.isNotNull(entity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
baseEntity.setUpdateBy(LoginHelper.getUserId()); baseEntity.setUpdateBy(LoginHelper.getUserId());
baseEntity.setUpdateTime(new Date()); baseEntity.setUpdateTime(new Date());
} }
} catch (Exception e) {
throw new ServiceException("全局更新数据监听器注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
} }

View File

@ -1,4 +1,2 @@
com.ruoyi.common.orm.config.PagehelperConfig com.ruoyi.common.orm.config.PagehelperConfig
com.ruoyi.common.orm.config.MyBatisFlexInitConfig
com.ruoyi.common.orm.config.MyConfigurationCustomizer
com.ruoyi.common.orm.config.MyBatisFlexConfig com.ruoyi.common.orm.config.MyBatisFlexConfig

View File

@ -18,7 +18,7 @@ import java.io.Serial;
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Table(value = "sys_client",onInsert = EntityInsertListener.class,onUpdate = EntityUpdateListener.class) @Table(value = "sys_client")
public class SysClient extends BaseEntity { public class SysClient extends BaseEntity {
@Serial @Serial