From de31954c6feca850c6a4c3fb44996d41f3ac1edf Mon Sep 17 00:00:00 2001 From: dataprince Date: Sun, 10 Sep 2023 19:07:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E5=85=A8=E5=B1=80=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=A1=AB=E5=85=85=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/orm/config/MyBatisFlexConfig.java | 64 ++++++++++++++----- .../orm/config/MyBatisFlexInitConfig.java | 28 -------- .../orm/config/MyConfigurationCustomizer.java | 20 ------ .../orm/listener/EntityInsertListener.java | 29 ++++++--- .../orm/listener/EntityUpdateListener.java | 18 ++++-- ...ot.autoconfigure.AutoConfiguration.imports | 2 - .../com/ruoyi/system/domain/SysClient.java | 2 +- 7 files changed, 85 insertions(+), 78 deletions(-) delete mode 100644 ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexInitConfig.java delete mode 100644 ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyConfigurationCustomizer.java diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexConfig.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexConfig.java index a71cfad..304b140 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexConfig.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexConfig.java @@ -1,8 +1,18 @@ 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.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.listener.EntityInsertListener; +import com.ruoyi.common.orm.listener.EntityUpdateListener; +import org.apache.ibatis.logging.stdout.StdOutImpl; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,26 +25,25 @@ import org.springframework.context.annotation.Configuration; * * @author dataprince数据小王子 */ -//@EnableTransactionManagement(proxyTargetClass = true) @Configuration @MapperScan("${mybatis-flex.mapperPackage}") -public class MyBatisFlexConfig { +public class MyBatisFlexConfig implements ConfigurationCustomizer, MyBatisFlexCustomizer { private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql"); - /** - * SQL 打印 - */ - public MyBatisFlexConfig() { - //开启审计功能 - AuditManager.setAuditEnable(true); - - //设置 SQL 审计收集器 - AuditManager.setMessageCollector(auditMessage -> - logger.info("{},耗费{}ms", auditMessage.getFullSql() - , auditMessage.getElapsedTime()) - ); - } +// /** +// * SQL 打印 +// */ +// public MyBatisFlexConfig() { +// //开启审计功能 +// AuditManager.setAuditEnable(true); +// +// //设置 SQL 审计收集器 +// AuditManager.setMessageCollector(auditMessage -> +// logger.info("{},耗费{}ms", auditMessage.getFullSql() +// , auditMessage.getElapsedTime()) +// ); +// } /** * 数据源解密 @@ -45,6 +54,31 @@ public class MyBatisFlexConfig { 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:多租户配置 diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexInitConfig.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexInitConfig.java deleted file mode 100644 index 67db827..0000000 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyBatisFlexInitConfig.java +++ /dev/null @@ -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); - } -} diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyConfigurationCustomizer.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyConfigurationCustomizer.java deleted file mode 100644 index 16bc522..0000000 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/config/MyConfigurationCustomizer.java +++ /dev/null @@ -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); - } -} diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java index 3bee812..9ab2263 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java @@ -1,13 +1,16 @@ package com.ruoyi.common.orm.listener; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpStatus; import com.mybatisflex.annotation.InsertListener; +import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.security.utils.LoginHelper; import java.util.Date; /** - * Entity实体类新增数据监听器 + * Entity实体类全局插入数据监听器 * * @author dataprince数据小王子 */ @@ -15,12 +18,22 @@ public class EntityInsertListener implements InsertListener { @Override public void onInsert(Object entity) { - BaseEntity baseEntity = (BaseEntity) entity; - Long loginUserId = LoginHelper.getUserId(); - Date createTime = new Date(); - baseEntity.setCreateBy(loginUserId); - baseEntity.setCreateTime(createTime); - baseEntity.setUpdateBy(loginUserId); - baseEntity.setUpdateTime(createTime); + try { + if (ObjectUtil.isNotNull(entity)) { + BaseEntity baseEntity = (BaseEntity) entity; + + Long loginUserId = LoginHelper.getUserId(); + + Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime()) + ? baseEntity.getCreateTime() : new Date(); + + baseEntity.setCreateBy(loginUserId); + baseEntity.setCreateTime(createTime); + baseEntity.setUpdateBy(loginUserId); + baseEntity.setUpdateTime(createTime); + } + } catch (Exception e) { + throw new ServiceException("全局插入数据监听器注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); + } } } diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java index aeb465f..4626ef7 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java @@ -1,21 +1,31 @@ package com.ruoyi.common.orm.listener; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpStatus; import com.mybatisflex.annotation.UpdateListener; +import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.security.utils.LoginHelper; import java.util.Date; /** - * Entity实体类更新数据监听器 + * Entity实体类全局更新数据监听器 * * @author dataprince数据小王子 */ public class EntityUpdateListener implements UpdateListener { @Override public void onUpdate(Object entity) { - BaseEntity baseEntity = (BaseEntity) entity; - baseEntity.setUpdateBy(LoginHelper.getUserId()); - baseEntity.setUpdateTime(new Date()); + try { + if (ObjectUtil.isNotNull(entity)) { + BaseEntity baseEntity = (BaseEntity) entity; + baseEntity.setUpdateBy(LoginHelper.getUserId()); + baseEntity.setUpdateTime(new Date()); + } + } catch (Exception e) { + throw new ServiceException("全局更新数据监听器注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); + } + } } diff --git a/ruoyi-common/ruoyi-common-orm/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-orm/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 82153bd..d8e4ec1 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/ruoyi-common/ruoyi-common-orm/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,2 @@ com.ruoyi.common.orm.config.PagehelperConfig -com.ruoyi.common.orm.config.MyBatisFlexInitConfig -com.ruoyi.common.orm.config.MyConfigurationCustomizer com.ruoyi.common.orm.config.MyBatisFlexConfig diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysClient.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysClient.java index 2294e56..896c084 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysClient.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysClient.java @@ -18,7 +18,7 @@ import java.io.Serial; */ @Data @EqualsAndHashCode(callSuper = true) -@Table(value = "sys_client",onInsert = EntityInsertListener.class,onUpdate = EntityUpdateListener.class) +@Table(value = "sys_client") public class SysClient extends BaseEntity { @Serial