Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement batch insertion in MybatisPlus

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve batch insertion of MybatisPlus. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. background

In data synchronization or idempotent scenarios, you often need to set a unique index to avoid duplicate requests. Select and update is inefficient, and errors are still reported during concurrency, which is not friendly, so you can use Mysql's Insert ignore syntax to optimize.

MybatisPlus officially does not support this scenario.

Second, environment com.baomidou mybatis-plus-boot-starter 3.2.0 com.baomidou mybatis-plus-generator third, injection custom batch insert sql

Because you only need to modify the insertBatchSomeColumn method, then just CV it.

The insertBatchSomeColumn method belongs to the official extension package of mybatis plus

Sql template

Method name for public class InsertIgnoreBatchAllColumn extends AbstractMethod {/ * mapper * / private static final String MAPPER_METHOD = "insertIgnoreBatchAllColumn"; / * Field filter condition * / @ Setter @ Accessors (chain = true) private Predicate predicate; @ SuppressWarnings ("Duplicates") @ Override public MappedStatement injectMappedStatement (Class mapperClass, Class modelClass, TableInfo tableInfo) {KeyGenerator keyGenerator = new NoKeyGenerator () SqlMethod sqlMethod = SqlMethod.INSERT_ONE; String sqlTemplate = "\ nINSERT IGNORE INTO% s% s VALUES% s\ n"; 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 / / the table contains primary key processing logic, if it does not contain primary keys when ordinary fields handle if (StringUtils.isNotEmpty (tableInfo.getKeyProperty () {if (tableInfo.getIdType () = = IdType.AUTO) {/ * self-incrementing primary key * / 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);}}

Inject sql

Public class CustomerSqlInjector extends DefaultSqlInjector {@ Override public List getMethodList (Class mapperClass) {List methodList = super.getMethodList (mapperClass); methodList.add (new InsertIgnoreBatchAllColumn ()); return methodList;}}

General mapper

Public interface CommonMapper extends BaseMapper {/ * full insert, equivalent to insert, ignoring rows with unique index conflicts * {@ link InsertIgnoreBatchAllColumn} * * @ param entityList * @ return * / int insertIgnoreBatchAllColumn (List entityList);}

General Service

Public class CommonServiceImpl 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 () 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) }} this is the end of the article on "how to insert MybatisPlus in bulk". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report