注册全局数据填充监听器
This commit is contained in:
parent
01f30b9e89
commit
de31954c6f
@ -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:多租户配置
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user