您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)MybatisPlus如何實(shí)現(xiàn)批量插入,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
再數(shù)據(jù)同步或者冪等場景下,常常需要設(shè)置唯一索引來避免重復(fù)請(qǐng)求,select and update
效率低,且并發(fā)時(shí)還是會(huì)報(bào)錯(cuò),并不友好,那么可以用Mysql的Insert ignore語法來優(yōu)化。
MybatisPlus官方并沒有針此處場景進(jìn)行支持
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </exclusion> </exclusions> </dependency>
因?yàn)橹恍枰脑?code>insertBatchSomeColumn方法,那直接CV就好
insertBatchSomeColumn
方法屬于mybatis plus
官方擴(kuò)展包中
sql模板
public class InsertIgnoreBatchAllColumn extends AbstractMethod { /** * mapper 對(duì)應(yīng)的方法名 */ private static final String MAPPER_METHOD = "insertIgnoreBatchAllColumn"; /** * 字段篩選條件 */ @Setter @Accessors(chain = true) private Predicate<TableFieldInfo> predicate; @SuppressWarnings("Duplicates") @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { KeyGenerator keyGenerator = new NoKeyGenerator(); SqlMethod sqlMethod = SqlMethod.INSERT_ONE; String sqlTemplate = "<script>\nINSERT IGNORE INTO %s %s VALUES %s\n</script>"; List<TableFieldInfo> fieldList = tableInfo.getFieldList(); String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) + this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY); String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET; String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) + 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; // 表包含主鍵處理邏輯,如果不包含主鍵當(dāng)普通字段處理 if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) { if (tableInfo.getIdType() == IdType.AUTO) { /* 自增主鍵 */ keyGenerator = new Jdbc3KeyGenerator(); keyProperty = tableInfo.getKeyProperty(); keyColumn = tableInfo.getKeyColumn(); } else { if (null != tableInfo.getKeySequence()) { keyGenerator = TableInfoHelper.genKeyGenerator(tableInfo, builderAssistant, sqlMethod.getMethod(), languageDriver); keyProperty = tableInfo.getKeyProperty(); keyColumn = tableInfo.getKeyColumn(); } } } String sql = String.format(sqlTemplate, tableInfo.getTableName(), columnScript, valuesScript); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addInsertMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource, keyGenerator, keyProperty, keyColumn); } }
注入sql
public class CustomerSqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new InsertIgnoreBatchAllColumn()); return methodList; } }
通用mapper
public interface CommonMapper<T> extends BaseMapper<T> { /** * 全量插入,等價(jià)于insert,忽略唯一索引沖突的行 * {@link InsertIgnoreBatchAllColumn} * * @param entityList * @return */ int insertIgnoreBatchAllColumn(List<T> entityList); }
通用Service
public class CommonServiceImpl<M extends CommonMapper<T>, T> extends ServiceImpl<M, T> { @Transactional(rollbackFor = Exception.class) public boolean fastSaveIgnoreBatch(List<T> list, int batchSize) { if(CollectionUtils.isEmpty(list)) { return true; } batchSize = batchSize < 1 ? BATCH_SIZE : batchSize; if(list.size() <= batchSize) { return retBool(baseMapper.insertIgnoreBatchAllColumn(list)); } for (int fromIdx = 0 , endIdx = batchSize ; ; fromIdx += batchSize, endIdx += batchSize) { if(endIdx > list.size()) { endIdx = list.size(); } baseMapper.insertIgnoreBatchAllColumn(list.subList(fromIdx, endIdx)); if(endIdx == list.size()) { return true; } } } @Transactional(rollbackFor = Exception.class) public boolean fastSaveIgnoreBatch(List<T> list) { return fastSaveIgnoreBatch(list, BATCH_SIZE); } }
關(guān)于“MybatisPlus如何實(shí)現(xiàn)批量插入”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。