基础信息提交
This commit is contained in:
77
njcn-db/mybatis-plus/pom.xml
Normal file
77
njcn-db/mybatis-plus/pom.xml
Normal file
@@ -0,0 +1,77 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>njcn-db</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>mybatis-plus</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<mybatis.version>2.1.3</mybatis.version>
|
||||
<mybatis-plus.version>3.4.2</mybatis-plus.version>
|
||||
<jeffrey-mybatis-plus.version>1.5.1-RELEASE</jeffrey-mybatis-plus.version>
|
||||
<druid.version>1.2.5</druid.version>
|
||||
<mysql.version>8.0.19</mysql.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--公共依赖-->
|
||||
<dependency>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>njcn-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis基础工具-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<!-- 排除默认的 HikariCP 数据源 -->
|
||||
<exclusion>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${mybatis.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis增强工具-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-plus 额外补充功能-->
|
||||
<dependency>
|
||||
<groupId>com.github.jeffreyning</groupId>
|
||||
<artifactId>mybatisplus-plus</artifactId>
|
||||
<version>${jeffrey-mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--druid连接池-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--mysql驱动-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.njcn.db.mybatisplus.bo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年12月13日 10:18
|
||||
*/
|
||||
@Data
|
||||
public class BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.njcn.db.mybatisplus.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.njcn.db.mybatisplus.handler.AutoFillValueHandler;
|
||||
import com.njcn.db.mybatisplus.handler.BatchInjector;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年12月14日 16:43
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisConfig {
|
||||
|
||||
/**
|
||||
* 分页插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 自定义公共字段自动填充
|
||||
*/
|
||||
@Bean
|
||||
public AutoFillValueHandler autoFillValueHandler() {
|
||||
return new AutoFillValueHandler();
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 自定义注册器,处理批量插入
|
||||
* @author hongawen
|
||||
* @return BatchInjector
|
||||
*/
|
||||
@Bean
|
||||
public BatchInjector BatchInjector() {
|
||||
return new BatchInjector();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.njcn.db.mybatisplus.constant;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年12月20日 16:08
|
||||
*/
|
||||
public interface DbConstant {
|
||||
|
||||
/**
|
||||
* 正序标识
|
||||
*/
|
||||
String ASC = "asc";
|
||||
|
||||
/**
|
||||
* 倒序标识
|
||||
*/
|
||||
String DESC = "desc";
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.njcn.db.mybatisplus.constant;
|
||||
|
||||
public interface UserConstant {
|
||||
|
||||
String USER_ID = "userId";
|
||||
|
||||
String UNKNOWN_USER_ID = "未知用户";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.njcn.db.mybatisplus.handler;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.njcn.common.bean.CustomCacheUtil;
|
||||
import com.njcn.db.mybatisplus.constant.UserConstant;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 数据自动填充处理器
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2021年12月14日 16:36
|
||||
*/
|
||||
|
||||
public class AutoFillValueHandler implements MetaObjectHandler {
|
||||
|
||||
private static final String CREATE_USER = "createBy";
|
||||
|
||||
private static final String CREATE_TIME = "createTime";
|
||||
|
||||
private static final String UPDATE_USER = "updateBy";
|
||||
|
||||
private static final String UPDATE_TIME = "updateTime";
|
||||
|
||||
|
||||
/**
|
||||
* 执行元数据需要插入的值添加
|
||||
*/
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Supplier<String> supplierUserId = getUserIdSupplier();
|
||||
this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime::now, LocalDateTime.class);
|
||||
this.strictInsertFill(metaObject, CREATE_USER, supplierUserId, String.class);
|
||||
this.strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
|
||||
this.strictInsertFill(metaObject, UPDATE_USER, supplierUserId, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行元数据需要更新的数据更新
|
||||
*/
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
Supplier<String> supplierUserId = getUserIdSupplier();
|
||||
this.strictInsertFill(metaObject, UPDATE_USER, supplierUserId, String.class);
|
||||
this.strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
|
||||
}
|
||||
|
||||
|
||||
public Supplier<String> getUserIdSupplier(){
|
||||
CustomCacheUtil customCacheUtil = SpringUtil.getBean(CustomCacheUtil.CACHE_NAME);
|
||||
String userId = customCacheUtil.get(UserConstant.USER_ID, false);
|
||||
final String actualUserId = StrUtil.isBlank(userId) ? UserConstant.UNKNOWN_USER_ID : userId;
|
||||
return () -> actualUserId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.njcn.db.mybatisplus.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
|
||||
import com.github.jeffreyning.mybatisplus.base.MppSqlInjector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2023年05月30日 14:51
|
||||
*/
|
||||
public class BatchInjector extends MppSqlInjector {
|
||||
|
||||
@Override
|
||||
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
|
||||
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
|
||||
//更新时自动填充的字段,不用插入值
|
||||
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
|
||||
return methodList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.njcn.db.mybatisplus.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2023年05月30日 14:54
|
||||
*/
|
||||
public interface BatchBaseMapper<T> extends BaseMapper<T> {
|
||||
|
||||
/**
|
||||
* 真正的批量插入
|
||||
*/
|
||||
int insertBatchSomeColumn(List<T> entityList);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.njcn.db.mybatisplus.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2023年06月06日 18:14
|
||||
*/
|
||||
public interface IReplenishMybatisService<T> extends IService<T> {
|
||||
|
||||
/***
|
||||
* 当批量插入数据量过大时,可以指定尺寸交给mybatis,每次插入多少条记录
|
||||
* @author hongawen
|
||||
* @date 2023/6/6 9:33
|
||||
* @param data 数据集合
|
||||
* @param size 分片的尺寸
|
||||
*/
|
||||
void insertBatchBySlice(List<T> data, int size);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.njcn.db.mybatisplus.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.db.mybatisplus.mapper.BatchBaseMapper;
|
||||
import com.njcn.db.mybatisplus.service.IReplenishMybatisService;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2023年06月06日 18:16
|
||||
*/
|
||||
public class ReplenishMybatisServiceImpl<M extends BatchBaseMapper<T>, T> extends ServiceImpl<BatchBaseMapper<T>, T> implements IReplenishMybatisService<T> {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void insertBatchBySlice(List<T> data, int size) {
|
||||
try {
|
||||
int totalCount = data.size();
|
||||
int idxLimit = Math.min(size, totalCount);
|
||||
List<T> dataTemp = new ArrayList<>(data);
|
||||
//保存单批提交的数据集合
|
||||
if (idxLimit == size) {
|
||||
int times = totalCount / idxLimit + 1;
|
||||
for (int i = 1; i <= times; i++) {
|
||||
if (totalCount >= idxLimit) {
|
||||
List<T> temp = dataTemp.subList(0, idxLimit);
|
||||
this.baseMapper.insertBatchSomeColumn(temp);
|
||||
temp.clear();
|
||||
totalCount = totalCount - idxLimit;
|
||||
} else {
|
||||
if (CollectionUtil.isNotEmpty(dataTemp)) {
|
||||
this.baseMapper.insertBatchSomeColumn(dataTemp);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.baseMapper.insertBatchSomeColumn(dataTemp);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("分片批量插入数据异常,异常为:" + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.njcn.db.mybatisplus.config.MybatisConfig
|
||||
|
||||
|
||||
Reference in New Issue
Block a user