feat(): 增加mybatis-plus元数据填充

boot3.0
dongzeliang 2025-03-03 17:03:20 +08:00
parent 3009c07ed6
commit 0b02eeac8a
8 changed files with 109 additions and 10 deletions

View File

@ -141,10 +141,17 @@
<artifactId>apm-toolkit-logback-1.x</artifactId>
</dependency>
<!-- HuTool 工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- SpringBoot 单元测试框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,16 @@
package com.muyu.common.core.basic;
/**
* @author dongzeliang
* @version 1.0
* @description:
* @date 2025/3/1 11:57
*/
public interface SecurityBasic {
/**
*
* @return
*/
public String getUsername();
}

View File

@ -1,4 +1,4 @@
package com.muyu.common.core.config;
package com.muyu.common.core.mybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;

View File

@ -0,0 +1,46 @@
package com.muyu.common.core.mybatisplus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.muyu.common.core.basic.SecurityBasic;
import lombok.AllArgsConstructor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import java.util.Date;
/**
*
* @author dongzeliang
* @version 1.0
* @description:
* @date 2025/3/1 11:51
*/
@AllArgsConstructor
@ConditionalOnProperty(value = "basic.mybatis-plus.meta.enabled", havingValue = "true")
public class SystemMetaObjectHandler implements MetaObjectHandler {
private final SecurityBasic securityBasic;
/**
*
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createBy", String.class, securityBasic.getUsername());
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
}
/**
*
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "updateBy", String.class, securityBasic.getUsername());
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
}
}

View File

@ -1,3 +1,4 @@
com.muyu.common.core.utils.SpringUtils
com.muyu.common.core.feign.FeginConfig
com.muyu.common.core.config.MybatisPlusConfig
com.muyu.common.core.mybatisplus.MybatisPlusConfig
com.muyu.common.core.mybatisplus.SystemMetaObjectHandler

View File

@ -0,0 +1,24 @@
package com.muyu.common.security.service;
import com.muyu.common.core.basic.SecurityBasic;
import com.muyu.common.security.utils.SecurityUtils;
/**
*
* @author dongzeliang
* @version 1.0
* @description:
* @date 2025/3/1 11:59
*/
public class SecurityBasicImpl implements SecurityBasic {
/**
*
*
* @return
*/
@Override
public String getUsername() {
return SecurityUtils.getUsername();
}
}

View File

@ -3,3 +3,4 @@ com.muyu.common.security.service.TokenService
com.muyu.common.security.aspect.PreAuthorizeAspect
com.muyu.common.security.aspect.InnerAuthAspect
com.muyu.common.security.handler.GlobalExceptionHandler
com.muyu.common.security.service.SecurityBasicImpl

View File

@ -9,10 +9,14 @@ import com.muyu.gateway.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
/**
*
@ -64,13 +68,13 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object> {
private String resolveBodyFromRequest (ServerHttpRequest serverHttpRequest) {
// 获取请求体
return DataBufferUtils.join(serverHttpRequest.getBody())
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
return new String(bytes, StandardCharsets.UTF_8);
})
.block();
Flux<DataBuffer> dataBufferFlux = serverHttpRequest.getBody();
AtomicReference<String> bodyStr = new AtomicReference<>();
dataBufferFlux.cache().subscribe(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
bodyStr.set(new String(bytes, StandardCharsets.UTF_8));
});
return bodyStr.get();
}
}