重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关MybatisPlus如何实现批量插入,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为井陉企业提供专业的成都网站建设、网站设计,井陉网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
再数据同步或者幂等场景下,常常需要设置唯一索引来避免重复请求,select and update
效率低,且并发时还是会报错,并不友好,那么可以用MySQL的Insert ignore语法来优化。
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如何实现批量插入”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。