這篇文章將為大家詳細講解有關MybatisPlus如何實現(xiàn)批量插入,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為井陘企業(yè)提供專業(yè)的成都網(wǎng)站建設、網(wǎng)站設計,井陘網(wǎng)站改版等技術服務。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
再數(shù)據(jù)同步或者冪等場景下,常常需要設置唯一索引來避免重復請求,select and update
效率低,且并發(fā)時還是會報錯,并不友好,那么可以用MySQL的Insert ignore語法來優(yōu)化。
MybatisPlus官方并沒有針此處場景進行支持
com.baomidou mybatis-plus-boot-starter 3.2.0 com.baomidou mybatis-plus-generator
因為只需要改造insertBatchSomeColumn
方法,那直接CV就好
insertBatchSomeColumn
方法屬于mybatis plus
官方擴展包中
sql模板
public class InsertIgnoreBatchAllColumn extends AbstractMethod { /** * mapper 對應的方法名 */ private static final String MAPPER_METHOD = "insertIgnoreBatchAllColumn"; /** * 字段篩選條件 */ @Setter @Accessors(chain = true) private Predicatepredicate; @SuppressWarnings("Duplicates") @Override public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) { KeyGenerator keyGenerator = new NoKeyGenerator(); SqlMethod sqlMethod = SqlMethod.INSERT_ONE; String sqlTemplate = ""; List 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 ListgetMethodList(Class> mapperClass) { List methodList = super.getMethodList(mapperClass); methodList.add(new InsertIgnoreBatchAllColumn()); return methodList; } }
通用mapper
public interface CommonMapperextends BaseMapper { /** * 全量插入,等價于insert,忽略唯一索引沖突的行 * {@link InsertIgnoreBatchAllColumn} * * @param entityList * @return */ int insertIgnoreBatchAllColumn(List entityList); }
通用Service
public class CommonServiceImpl, T> extends ServiceImpl { @Transactional(rollbackFor = Exception.class) public boolean fastSaveIgnoreBatch(List 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 list) { return fastSaveIgnoreBatch(list, BATCH_SIZE); } }
關于“MybatisPlus如何實現(xiàn)批量插入”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。