feat : 部门重构完成
This commit is contained in:
@@ -6,29 +6,36 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2023-05-25
|
||||
*/
|
||||
@MapperScan("${mybatis.mapperPackage}")
|
||||
public class MybatisPlusAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 分页插件
|
||||
interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
||||
// 配置分页拦截器
|
||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
||||
paginationInnerInterceptor.setOptimizeJoin(false);
|
||||
paginationInnerInterceptor.setMaxLimit(500L);
|
||||
paginationInnerInterceptor.setOverflow(false);
|
||||
interceptor.addInnerInterceptor(paginationInnerInterceptor);
|
||||
// 乐观锁插件
|
||||
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
||||
// 添加防止全表更新与删除插件
|
||||
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分页插件,自动识别数据库类型
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.fateverse.common.mybatisplus.enums;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IEnum;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public interface BaseEnum<T extends Enum<T>> extends IEnum<Integer> {
|
||||
|
||||
/**
|
||||
* 获取编码
|
||||
*
|
||||
* @return {@link Integer}
|
||||
*/
|
||||
Integer getCode();
|
||||
|
||||
/**
|
||||
* 获取名称
|
||||
*
|
||||
* @return {@link String}
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* 获取value值
|
||||
*
|
||||
* @return {@link Integer}
|
||||
*/
|
||||
@Override
|
||||
default Integer getValue() {
|
||||
return getCode();
|
||||
}
|
||||
/**
|
||||
* 解析通过编码
|
||||
*
|
||||
* @param enumClass 枚举班
|
||||
* @param code 编码
|
||||
* @return {@link E}
|
||||
*/
|
||||
static <E extends Enum<E> & BaseEnum<E>> E parseByCode(Class<E> enumClass, Integer code) {
|
||||
return Arrays.stream(enumClass.getEnumConstants())
|
||||
.filter(e -> e.getCode().equals(code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按名称解析
|
||||
*
|
||||
* @param enumClass 枚举班
|
||||
* @param name 名称
|
||||
* @return {@link E}
|
||||
*/
|
||||
static <E extends Enum<E> & BaseEnum<E>> E parseByName(Class<E> enumClass, String name) {
|
||||
return Arrays.stream(enumClass.getEnumConstants())
|
||||
.filter(e -> e.getName().equals(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -27,6 +28,9 @@ public class AutoSetMetaObjectHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
autoSetValue(metaObject, MethodEnum.UPDATE);
|
||||
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
||||
this.strictUpdateFill(metaObject, "updateBy", Long.class, SecurityUtils.getUserId());
|
||||
this.strictUpdateFill(metaObject, "updateUserName", String.class, SecurityUtils.getUsername());
|
||||
}
|
||||
|
||||
private void autoSetValue(MetaObject metaObject, MethodEnum method) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package cn.fateverse.common.mybatisplus.utils;
|
||||
|
||||
import cn.fateverse.common.core.entity.PageInfo;
|
||||
import cn.fateverse.common.core.result.page.TableDataInfo;
|
||||
import cn.fateverse.common.core.utils.TableSupport;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2023-05-25
|
||||
*/
|
||||
public class PageConditionUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 转换为TableDataInfo对象
|
||||
*
|
||||
* @param page 源对象
|
||||
* @param map 转换方法
|
||||
* @param <T> 转换后的对象类型
|
||||
* @param <R> 需要转换的对象类型
|
||||
* @return 转换后的对象
|
||||
*/
|
||||
public static <T, R> TableDataInfo<T> convertDataTable(Page<R> page, Function<R, T> map) {
|
||||
List<T> convertList = page.getRecords().stream().map(map).collect(Collectors.toList());
|
||||
return convertDataTable(convertList, page.getTotal());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换为TableDataInfo对象
|
||||
*
|
||||
* @param list
|
||||
* @param count
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> TableDataInfo<T> convertDataTable(List<T> list, Long count) {
|
||||
if (null == list) {
|
||||
return new TableDataInfo<>(new ArrayList<>(), 0);
|
||||
}
|
||||
TableDataInfo<T> tableDataInfo = new TableDataInfo<>();
|
||||
tableDataInfo.setRows(list);
|
||||
tableDataInfo.setTotal(count);
|
||||
return tableDataInfo;
|
||||
}
|
||||
|
||||
public static <T> Page<T> getPage(){
|
||||
PageInfo pageInfo = TableSupport.getPageInfo();
|
||||
return new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user