feat: mybatis自动填充实现,分页拦截器实现
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
package cn.meowrain.aioj.backend.framework.core.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除枚举
|
||||||
|
*/
|
||||||
|
public enum DelStatusEnum {
|
||||||
|
STATUS_NORMAL("0"),
|
||||||
|
STATUS_DELETE("1");
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
DelStatusEnum(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
public String code() {
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,10 @@
|
|||||||
<artifactId>mybatis-plus-spring</artifactId>
|
<artifactId>mybatis-plus-spring</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
@@ -40,5 +44,17 @@
|
|||||||
<groupId>com.github.xiaoymin</groupId>
|
<groupId>com.github.xiaoymin</groupId>
|
||||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-core</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.meowrain</groupId>
|
||||||
|
<artifactId>aioj-backend-common-core</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package cn.meowrain.backend.common.mybaits;
|
||||||
|
|
||||||
|
import cn.meowrain.backend.common.mybaits.config.MybatisPlusMetaObjectHandler;
|
||||||
|
import cn.meowrain.backend.common.mybaits.plugins.PaginationInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
public class MybatisPlusAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
|
interceptor.addInnerInterceptor(new PaginationInterceptor());
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建并返回MybatisPlusMetaObjectHandler实例,用于审计字段自动填充
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
|
||||||
|
return new MybatisPlusMetaObjectHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package cn.meowrain.backend.common.mybaits.config;
|
||||||
|
|
||||||
|
import cn.meowrain.aioj.backend.framework.core.enums.DelStatusEnum;
|
||||||
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.ibatis.reflection.MetaObject;
|
||||||
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MybatisPlus 自动填充处理器,用于实体类字段的自动填充
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
|
||||||
|
@Override
|
||||||
|
public void insertFill(MetaObject metaObject) {
|
||||||
|
log.debug("mybatis plus start insert fill ....");
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
|
||||||
|
fillValIfNullByName("createTime", now, metaObject, true);
|
||||||
|
fillValIfNullByName("updateTime", now, metaObject, true);
|
||||||
|
fillValIfNullByName("createBy", getUserName(), metaObject, true);
|
||||||
|
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
|
||||||
|
|
||||||
|
// 删除标记自动填充
|
||||||
|
fillValIfNullByName("delFlag", DelStatusEnum.STATUS_NORMAL.code(), metaObject, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateFill(MetaObject metaObject) {
|
||||||
|
log.debug("mybatis plus start update fill ....");
|
||||||
|
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
|
||||||
|
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
|
||||||
|
//如果填充值为空
|
||||||
|
if (fieldVal == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 没有 set 方法
|
||||||
|
if (!metaObject.hasSetter(fieldName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// field 类型相同时设置
|
||||||
|
Class<?> getterType = metaObject.getGetterType(fieldName);
|
||||||
|
if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
|
||||||
|
metaObject.setValue(fieldName, fieldVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getUserName() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
// 匿名接口直接返回
|
||||||
|
if (authentication instanceof AnonymousAuthenticationToken) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Optional.ofNullable(authentication).isPresent()) {
|
||||||
|
return authentication.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package cn.meowrain.backend.common.mybaits.plugins;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.ParameterUtils;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.apache.ibatis.executor.Executor;
|
||||||
|
import org.apache.ibatis.mapping.BoundSql;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.session.ResultHandler;
|
||||||
|
import org.apache.ibatis.session.RowBounds;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 分页拦截器实现类,用于处理分页查询逻辑
|
||||||
|
* * <p>
|
||||||
|
* * 当分页大小小于0时自动设置为0,防止全表查询
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
public class PaginationInterceptor extends PaginationInnerInterceptor {
|
||||||
|
/**
|
||||||
|
* 数据库类型
|
||||||
|
* <p>
|
||||||
|
* 查看 {@link #findIDialect(Executor)} 逻辑
|
||||||
|
*/
|
||||||
|
private DbType dbType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 方言实现类
|
||||||
|
* <p>
|
||||||
|
* 查看 {@link #findIDialect(Executor)} 逻辑
|
||||||
|
*/
|
||||||
|
private IDialect dialect;
|
||||||
|
|
||||||
|
|
||||||
|
public PaginationInterceptor(DbType dbType) {
|
||||||
|
this.dbType = dbType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PaginationInterceptor(IDialect dialect) {
|
||||||
|
this.dialect = dialect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在执行查询前处理分页参数
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
|
||||||
|
IPage<?> page = ParameterUtils.findPage(parameter).orElse(null);
|
||||||
|
// size 小于 0 直接设置为 0 , 即不查询任何数据
|
||||||
|
if (null != page && page.getSize() < 0) {
|
||||||
|
page.setSize(0);
|
||||||
|
}
|
||||||
|
super.beforeQuery(executor, ms, page, rowBounds, resultHandler, boundSql);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1 @@
|
|||||||
com.pig4cloud.pig.common.mybatis.MybatisAutoConfiguration
|
cn.meowrain.backend.common.mybaits.MybatisPlusAutoConfiguration
|
||||||
Reference in New Issue
Block a user