您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關MybatisPlus如何實現批量插入,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
再數據同步或者冪等場景下,常常需要設置唯一索引來避免重復請求,select and update
效率低,且并發時還是會報錯,并不友好,那么可以用Mysql的Insert ignore語法來優化。
MybatisPlus官方并沒有針此處場景進行支持
<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>
因為只需要改造insertBatchSomeColumn
方法,那直接CV就好
insertBatchSomeColumn
方法屬于mybatis plus
官方擴展包中
sql模板
public class InsertIgnoreBatchAllColumn extends AbstractMethod { /** * mapper 對應的方法名 */ 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; // 表包含主鍵處理邏輯,如果不包含主鍵當普通字段處理 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> { /** * 全量插入,等價于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); } }
關于“MybatisPlus如何實現批量插入”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。