增加replace,replaceBatch,insertIgnore,insertIgnoreBatch

pro
wj 2 years ago
parent a953328f1f
commit 102cf093c2

@ -0,0 +1,27 @@
package com.glxp.api.config;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.util.List;
/**
* sql
*/
public class CustomerSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// 插入数据,如果中已经存在相同的记录,则忽略当前新数据
methodList.add(new InsertIgnore());
// 批量插入数据,如果中已经存在相同的记录,则忽略当前新数据
methodList.add(new InsertIgnoreBatch());
// 替换数据,如果中已经存在相同的记录,则覆盖旧数据
methodList.add(new Replace());
// 批量替换数据,如果中已经存在相同的记录,则覆盖旧数据
methodList.add(new ReplaceBatch());
return methodList;
}
}

@ -0,0 +1,28 @@
package com.glxp.api.config;
import lombok.Getter;
/**
* MybatisPlusSQL
*/
@Getter
public enum CustomerSqlMethod {
/**
*
*/
INSERT_IGNORE_ONE("insertIgnore", "插入一条数据(选择字段插入),如果中已经存在相同的记录,则忽略当前新数据", "<script>\nINSERT IGNORE INTO %s %s VALUES %s\n</script>"),
/**
*
*/
REPLACE_ONE("replace", "替换一条数据(选择字段插入),存在则替换,不存在则插入", "<script>\nREPLACE INTO %s %s VALUES %s\n</script>");
private final String method;
private final String desc;
private final String sql;
CustomerSqlMethod(String method, String desc, String sql) {
this.method = method;
this.desc = desc;
this.sql = sql;
}
}

@ -0,0 +1,49 @@
package com.glxp.api.config;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
*
* INSERT IGNORE
*/
public class InsertIgnore extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = new NoKeyGenerator();
CustomerSqlMethod sqlMethod = CustomerSqlMethod.INSERT_IGNORE_ONE;
String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(null),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String valuesScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlPropertyMaybeIf(null),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {
if (tableInfo.getIdType() == IdType.AUTO) {
/* 自增主键 */
keyGenerator = new Jdbc3KeyGenerator();
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
} else {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(sqlMethod.getMethod(), tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty, keyColumn);
}
}

@ -0,0 +1,76 @@
package com.glxp.api.config;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import java.util.List;
import java.util.function.Predicate;
/**
* , insert ignore
*/
public class InsertIgnoreBatch extends AbstractMethod {
/**
* mapper
*/
private static final String MAPPER_METHOD = "insertIgnoreBatch";
/**
*
*/
@Setter
@Accessors(chain = true)
private Predicate<TableFieldInfo> predicate;
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = new NoKeyGenerator();
CustomerSqlMethod sqlMethod = CustomerSqlMethod.INSERT_IGNORE_ONE;
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false, true) +
this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(false, ENTITY_DOT, true) +
this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (tableInfo.havePK()) {
if (tableInfo.getIdType() == IdType.AUTO) {
/* 自增主键 */
keyGenerator = new Jdbc3KeyGenerator();
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
} else {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(MAPPER_METHOD, tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource, keyGenerator, keyProperty, keyColumn);
}
@Override
public String getMethod(SqlMethod sqlMethod) {
// 自定义 mapper 方法名
return MAPPER_METHOD;
}
}

@ -56,6 +56,7 @@ public class MasterDataSourceConfig {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setConfiguration(configuration);
globalConfig.setSqlInjector(new CustomerSqlInjector());
sessionFactory.setGlobalConfig(globalConfig);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));

@ -0,0 +1,40 @@
package com.glxp.api.config;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
*
*
*/
public class Replace extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = new NoKeyGenerator();
CustomerSqlMethod sqlMethod = CustomerSqlMethod.REPLACE_ONE;
String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(null),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String valuesScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlPropertyMaybeIf(null),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(sqlMethod.getMethod(), tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty, keyColumn);
}
}

@ -0,0 +1,69 @@
package com.glxp.api.config;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import java.util.List;
import java.util.function.Predicate;
/**
*
*
*/
public class ReplaceBatch extends AbstractMethod {
/**
* mapper
*/
private static final String MAPPER_METHOD = "replaceBatch";
/**
*
*/
@Setter
@Accessors(chain = true)
private Predicate<TableFieldInfo> predicate;
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = new NoKeyGenerator();
CustomerSqlMethod sqlMethod = CustomerSqlMethod.REPLACE_ONE;
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, true) +
this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, true) +
this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(sqlMethod.getMethod(), tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource, keyGenerator, keyProperty, keyColumn);
}
@Override
public String getMethod(SqlMethod sqlMethod) {
// 自定义 mapper 方法名
return MAPPER_METHOD;
}
}

@ -226,4 +226,41 @@ public interface BaseMapperPlus<M, T, V> extends BaseMapper<T> {
return (P) voPage;
}
/**
*
* {@link com.glxp.api.config.InsertIgnore}
*
* @param entity
* @return
*/
int insertIgnore(T entity);
/**
*
* {@link com.glxp.api.config.InsertIgnoreBatch}
*
* @param entityList
* @return
*/
int insertIgnoreBatch(List<T> entityList);
/**
*
* replace intoPrimaryKeyuniqueinsert into
* {@link com.glxp.api.config.Replace}
*
* @param entity
* @return
*/
int replace(T entity);
/**
*
* replace intoPrimaryKeyuniqueinsert into
* {@link com.glxp.api.config.ReplaceBatch}
*
* @param entityList
* @return
*/
int replaceBatch(List<T> entityList);
}

@ -1603,34 +1603,34 @@ public class HeartService {
List<String> syncFiles = JSONUtil.toList(jsonObject.getJSONArray("syncFiles"), String.class);
if (CollectionUtil.isNotEmpty(hospTypeList)) {
basicHospTypeDao.insertOrUpdateBatch(hospTypeList);
basicHospTypeDao.replaceBatch(hospTypeList);
}
if (CollectionUtil.isNotEmpty(udiRelevanceList)) {
udiRelevanceDao.insertOrUpdateBatch(udiRelevanceList);
udiRelevanceDao.replaceBatch(udiRelevanceList);
}
if (CollectionUtil.isNotEmpty(productsList)) {
basicProductsDao.insertOrUpdateBatch(productsList);
basicProductsDao.replaceBatch(productsList);
}
if (CollectionUtil.isNotEmpty(relevanceList)) {
relevanceDao.insertOrUpdateBatch(relevanceList);
relevanceDao.replaceBatch(relevanceList);
}
if (CollectionUtil.isNotEmpty(corpList)) {
corpDao.insertOrUpdateBatch(corpList);
corpDao.replaceBatch(corpList);
}
if (CollectionUtil.isNotEmpty(supCertList)) {
supCertDao.insertOrUpdateBatch(supCertList);
supCertDao.replaceBatch(supCertList);
}
if (CollectionUtil.isNotEmpty(supCertSetList)) {
supCertSetDao.insertOrUpdateBatch(supCertSetList);
supCertSetDao.replaceBatch(supCertSetList);
}
if (CollectionUtil.isNotEmpty(supCompanyList)) {
supCompanyDao.insertOrUpdateBatch(supCompanyList);
supCompanyDao.replaceBatch(supCompanyList);
}
if (CollectionUtil.isNotEmpty(supManufacturerList)) {
supManufacturerDao.insertOrUpdateBatch(supManufacturerList);
supManufacturerDao.replaceBatch(supManufacturerList);
}
if (CollectionUtil.isNotEmpty(supProductList)) {
supProductDao.insertOrUpdateBatch(supProductList);
supProductDao.replaceBatch(supProductList);
}
if (CollUtil.isNotEmpty(syncFiles)) {
fileService.download(syncFiles);
@ -1659,13 +1659,13 @@ public class HeartService {
List<IoOrderInvoiceEntity> ioOrderInvoiceEntities = JSONUtil.toList(jsonObject.getJSONArray(IoOrderInvoiceEntity.class.getSimpleName()), IoOrderInvoiceEntity.class);
if (CollectionUtil.isNotEmpty(ioCodeLostList)) {
ioCodeLostMapper.insertOrUpdateBatch(ioCodeLostList);
ioCodeLostMapper.replaceBatch(ioCodeLostList);
}
if (CollectionUtil.isNotEmpty(ioCodeRelList)) {
ioCodeRelMapper.insertOrUpdateBatch(ioCodeRelList);
ioCodeRelMapper.replaceBatch(ioCodeRelList);
}
if (CollUtil.isNotEmpty(ioOrderInvoiceEntities)) {
ioOrderInvoiceMapper.insertOrUpdateBatch(ioOrderInvoiceEntities);
ioOrderInvoiceMapper.replaceBatch(ioOrderInvoiceEntities);
}
List<String> syncFiles = JSONUtil.toList(jsonObject.getJSONArray("syncFiles"), String.class);
@ -1691,13 +1691,13 @@ public class HeartService {
List<BasicBusTypeChangeEntity> busTypeChangeEntities = JSONUtil.toList(jsonObject.getJSONArray(BasicBusTypeChangeEntity.class.getSimpleName()), BasicBusTypeChangeEntity.class);
List<ThrBusTypeOriginEntity> typeOriginEntities = JSONUtil.toList(jsonObject.getJSONArray(ThrBusTypeOriginEntity.class.getSimpleName()), ThrBusTypeOriginEntity.class);
if (CollectionUtil.isNotEmpty(bussinessTypeEntities)) {
bussinessTypeDao.insertOrUpdateBatch(bussinessTypeEntities);
bussinessTypeDao.replaceBatch(bussinessTypeEntities);
}
if (CollectionUtil.isNotEmpty(busTypeChangeEntities)) {
busTypeChangeDao.insertOrUpdateBatch(busTypeChangeEntities);
busTypeChangeDao.replaceBatch(busTypeChangeEntities);
}
if (CollectionUtil.isNotEmpty(typeOriginEntities)) {
thrBusTypeOriginDao.insertOrUpdateBatch(typeOriginEntities);
thrBusTypeOriginDao.replaceBatch(typeOriginEntities);
}
}

Loading…
Cancel
Save