您好,登錄后才能下訂單哦!
這篇文章主要講解了“mybatisplus批量更新太慢該怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mybatisplus批量更新太慢該怎么解決”吧!
最近使用mybatis-plus的 saveOrUpdateBath 和saveBath接口執行特別慢,數據量大時往往需要十幾分鐘,打開日志查看原來批量操作也是循環單條數據插入的,那有沒有批量更新的辦法呢??
mybatis-plus 提供了一個自定義方法sql注入器DefaultSqlInjector我們可以通過繼DefaultSqlInjector來加入自定義的方法達到批量插入的效果。
import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import org.springframework.stereotype.Component; import java.util.List; /** * @Description: 自定義方法SQL注入器 * @Title: CustomizedSqlInjector * @Package com.highgo.edu.common.batchOperation * @Author: * @Copyright * @CreateTime: 2022/11/3 16:21 */ @Component public class CustomizedSqlInjector extends DefaultSqlInjector { /** * 如果只需增加方法,保留mybatis plus自帶方法, * 可以先獲取super.getMethodList(),再添加add */ @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new InsertBatchMethod()); // methodList.add(new UpdateBatchMethod()); methodList.add(new MysqlInsertOrUpdateBath()); methodList.add(new PGInsertOrUpdateBath()); return methodList; } }
同時我們需要繼承BaseMapper<T> 定義
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @description:自定義接口覆蓋BaseMapper,解決mybatis-plus 批量操作慢的問題 * @author: * @date: 2022/11/3 15:14 * @param: null * @return: **/ public interface RootMapper<T> extends BaseMapper<T> { /** * @description:批量插入 * @author: * @date: 2022/11/3 15:13 * @param: [list] * @return: int **/ int insertBatch(@Param("list") List<T> list); /** * @description:批量插入更新 * @author: * @date: 2022/11/3 15:14 * @param: [list] * @return: int **/ int mysqlInsertOrUpdateBatch(@Param("list") List<T> list); int pgInsertOrUpdateBatch(@Param("list") List<T> list); }
在需要使用批量更新插入的mapper上使用自定義的RootMapper
如下圖
import com.XX.edu.common.batchOperation.RootMapper; import com.XX.edu.exam.model.TScore; import org.springframework.stereotype.Repository; /** * @Entity com.XX.edu.exam.model.TScore */ @Repository public interface TScoreMapper extends RootMapper<TScore> { }
下面我們來定義批量插入的方法:
package com.XX.edu.common.batchOperation; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.enums.SqlMethod; import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.executor.keygen.NoKeyGenerator; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @Description: 批量插入的方法 * @Title: InsertBatchMethod * @Package com.XX.edu.common.batchOperation * @Author: * @CreateTime: 2022/11/3 15:16 */ public class InsertBatchMethod extends AbstractMethod { Logger logger = LoggerFactory.getLogger(getClass()); @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { final String sql = "<script>insert into %s %s values %s</script>"; final String fieldSql = prepareFieldSql(tableInfo); final String valueSql = prepareValuesSql(tableInfo); final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql); logger.debug("sqlResult----->{}", sqlResult); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass); KeyGenerator keyGenerator = new NoKeyGenerator(); SqlMethod sqlMethod = SqlMethod.INSERT_ONE; 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(sqlMethod.getMethod(),tableInfo, builderAssistant); keyProperty = tableInfo.getKeyProperty(); keyColumn = tableInfo.getKeyColumn(); } } } // 第三個參數必須和RootMapper的自定義方法名一致 return this.addInsertMappedStatement(mapperClass, modelClass, "insertBatch", sqlSource, keyGenerator, keyProperty, keyColumn); } /** * @description: 拼接字段值 * @author: * @date: 2022/11/3 15:20 * @param: [tableInfo] * @return: java.lang.String **/ private String prepareValuesSql(TableInfo tableInfo) { final StringBuilder valueSql = new StringBuilder(); valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">"); //valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},"); tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},")); valueSql.delete(valueSql.length() - 1, valueSql.length()); valueSql.append("</foreach>"); return valueSql.toString(); } /** * @description:拼接字段 * @author: * @date: 2022/11/3 15:20 * @param: [tableInfo] * @return: java.lang.String **/ private String prepareFieldSql(TableInfo tableInfo) { StringBuilder fieldSql = new StringBuilder(); //fieldSql.append(tableInfo.getKeyColumn()).append(","); tableInfo.getFieldList().forEach(x -> { fieldSql.append(x.getColumn()).append(","); }); fieldSql.delete(fieldSql.length() - 1, fieldSql.length()); fieldSql.insert(0, "("); fieldSql.append(")"); return fieldSql.toString(); } }
繼續定義批量插入更新的抽象方法
package com.XX.edu.common.batchOperation; import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import org.apache.ibatis.executor.keygen.NoKeyGenerator; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; /** * @Description: 批量插入更新 * @Title: InsertOrUpdateBath * @Package com.XX.edu.common.batchOperation * @Author: * @Copyright * @CreateTime: 2022/11/3 15:23 */ public abstract class InsertOrUpdateBathAbstract extends AbstractMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { final SqlSource sqlSource = prepareSqlSource(tableInfo, modelClass); // 第三個參數必須和RootMapper的自定義方法名一致 return this.addInsertMappedStatement(mapperClass, modelClass, prepareInsertOrUpdateBathName(), sqlSource, new NoKeyGenerator(), null, null); } protected abstract SqlSource prepareSqlSource(TableInfo tableInfo, Class<?> modelClass); protected abstract String prepareInsertOrUpdateBathName(); }
繼承上面的抽象類----mysql版本(本版本未測試 根據自己需求修改)
package com.XX.edu.common.batchOperation; import com.baomidou.mybatisplus.core.metadata.TableInfo; import org.apache.ibatis.mapping.SqlSource; import org.springframework.util.StringUtils; /** * @Description: 批量插入更新 * @Title: InsertOrUpdateBath * @Package com.XX.edu.common.batchOperation * @Author: * @Copyright * @CreateTime: 2022/11/3 15:23 */ public class MysqlInsertOrUpdateBath extends InsertOrUpdateBathAbstract { @Override protected SqlSource prepareSqlSource(TableInfo tableInfo, Class<?> modelClass) { final String sql = "<script>insert into %s %s values %s ON DUPLICATE KEY UPDATE %s</script>"; final String tableName = tableInfo.getTableName(); final String filedSql = prepareFieldSql(tableInfo); final String modelValuesSql = prepareModelValuesSql(tableInfo); final String duplicateKeySql = prepareDuplicateKeySql(tableInfo); final String sqlResult = String.format(sql, tableName, filedSql, modelValuesSql, filedSql, duplicateKeySql); //String.format(sql, tableName, filedSql, modelValuesSql, duplicateKeySql); //System.out.println("savaorupdatesqlsql="+sqlResult); return languageDriver.createSqlSource(configuration, sqlResult, modelClass); } @Override protected String prepareInsertOrUpdateBathName() { return "mysqlInsertOrUpdateBath"; } String prepareDuplicateKeySql(TableInfo tableInfo) { final StringBuilder duplicateKeySql = new StringBuilder(); if (!StringUtils.isEmpty(tableInfo.getKeyColumn())) { duplicateKeySql.append(tableInfo.getKeyColumn()).append("=values(").append(tableInfo.getKeyColumn()).append("),"); } tableInfo.getFieldList().forEach(x -> { duplicateKeySql.append(x.getColumn()) .append("=values(") .append(x.getColumn()) .append("),"); }); duplicateKeySql.delete(duplicateKeySql.length() - 1, duplicateKeySql.length()); return duplicateKeySql.toString(); } String prepareModelValuesSql(TableInfo tableInfo) { final StringBuilder valueSql = new StringBuilder(); valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">"); if (!StringUtils.isEmpty(tableInfo.getKeyProperty())) { valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},"); } tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},")); valueSql.delete(valueSql.length() - 1, valueSql.length()); valueSql.append("</foreach>"); return valueSql.toString(); } /** * @description:準備屬性名 * @author: * @date: 2022/11/3 15:25 * @param: [tableInfo] * @return: java.lang.String **/ String prepareFieldSql(TableInfo tableInfo) { StringBuilder fieldSql = new StringBuilder(); fieldSql.append(tableInfo.getKeyColumn()).append(","); tableInfo.getFieldList().forEach(x -> { fieldSql.append(x.getColumn()).append(","); }); fieldSql.delete(fieldSql.length() - 1, fieldSql.length()); fieldSql.insert(0, "("); fieldSql.append(")"); return fieldSql.toString(); } }
繼承上面的抽象類----postgresql版本(已測試完成,其中id使用序列自增)
package com.XX.edu.common.batchOperation; import com.baomidou.mybatisplus.core.metadata.TableInfo; import org.apache.ibatis.mapping.SqlSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; /** * @Description: 批量插入更新 * @Title: InsertOrUpdateBath * @Package com.XX.edu.common.batchOperation * @Author: * @Copyright * @CreateTime: 2022/11/3 15:23 */ public class PGInsertOrUpdateBath extends InsertOrUpdateBathAbstract { Logger logger = LoggerFactory.getLogger(getClass()); @Override protected SqlSource prepareSqlSource(TableInfo tableInfo, Class<?> modelClass) { final String sql = "<script>insert into %s %s values %s on conflict (id) do update set %s </script>"; final String tableName = tableInfo.getTableName(); final String filedSql = prepareFieldSql(tableInfo); final String modelValuesSql = prepareModelValuesSql(tableInfo); final String duplicateKeySql = prepareDuplicateKeySql(tableInfo); final String sqlResult = String.format(sql, tableName, filedSql, modelValuesSql, duplicateKeySql); logger.info("sql=={}",sqlResult); return languageDriver.createSqlSource(configuration, sqlResult, modelClass); } @Override protected String prepareInsertOrUpdateBathName() { return "pgInsertOrUpdateBatch"; } private String prepareDuplicateKeySql(TableInfo tableInfo) { final StringBuilder duplicateKeySql = new StringBuilder(); if (!StringUtils.isEmpty(tableInfo.getKeyColumn())) { duplicateKeySql.append(tableInfo.getKeyColumn()).append("=excluded.").append(tableInfo.getKeyColumn()).append(","); } tableInfo.getFieldList().forEach(x -> { duplicateKeySql.append(x.getColumn()) .append("=excluded.") .append(x.getColumn()) .append(","); }); duplicateKeySql.delete(duplicateKeySql.length() - 1, duplicateKeySql.length()); return duplicateKeySql.toString(); } private String prepareModelValuesSql(TableInfo tableInfo) { final StringBuilder valueSql = new StringBuilder(); valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">"); if (!StringUtils.isEmpty(tableInfo.getKeyProperty())) { valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},"); } tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},")); valueSql.delete(valueSql.length() - 1, valueSql.length()); valueSql.append("</foreach>"); return valueSql.toString(); } /** * @description:準備屬性名 * @author: * @date: 2022/11/3 15:25 * @param: [tableInfo] * @return: java.lang.String **/ private String prepareFieldSql(TableInfo tableInfo) { StringBuilder fieldSql = new StringBuilder(); if (!StringUtils.isEmpty(tableInfo.getKeyProperty())) { fieldSql.append(tableInfo.getKeyColumn()).append(","); } tableInfo.getFieldList().forEach(x -> { fieldSql.append(x.getColumn()).append(","); }); fieldSql.delete(fieldSql.length() - 1, fieldSql.length()); fieldSql.insert(0, "("); fieldSql.append(")"); return fieldSql.toString(); } }
到此定義結束,下面開始使用
@Service public class TNewExerciseServiceImpl extends ServiceImpl<TNewExerciseMapper, TNewExercise> implements TNewExerciseService { Logger logger = LoggerFactory.getLogger(getClass()); //引入mapper @Autowired TScoreMapper scoreMapper; //這樣就可以批量新增更新操作了 public void test(List<TScore> collect){ scoreMapper.pgInsertOrUpdateBatch(collect); } }
但是如果collect數據量太大會出現異常
“Tried to send an out-of-range integer as a 2-byte value: 87923”
是因為pg對于sql語句的參數數量是有限制的,最大為32767。
看pg源碼
public void sendInteger2(int val) throws IOException { if (val >= -32768 && val <= 32767) { this.int2Buf[0] = (byte)(val >>> 8); this.int2Buf[1] = (byte)val; this.pgOutput.write(this.int2Buf); } else { throw new IOException("Tried to send an out-of-range integer as a 2-byte value: " + val); } }
從源代碼中可以看到pgsql使用2個字節的integer,故其取值范圍為[-32768, 32767]。
這意味著sql語句的參數數量,即行數*列數之積必須小于等于32767.
比如,總共有17個字段,因為最大是32767,這樣最多允許32767/ 17 大約是1 927個,所以要分批操作,或有能力的童鞋可以自己修改pg的驅動呦
分批插入代碼如下:
/** * @description: * @author: * @date: 2022/11/4 14:57 * @param: [list, fieldCount:列數] * @return: void **/ public void detachSaveOrUpdate_score(List<TScore> list, int fieldCount) { int numberBatch = 32767; //每一次插入的最大數 //每一次插入的最大行數 , 向下取整 int v = ((Double) Math.floor(numberBatch / (fieldCount * 1.0))).intValue(); double number = list.size() * 1.0 / v; int n = ((Double) Math.ceil(number)).intValue(); //向上取整 for (int i = 0; i < n; i++) { int end = v * (i + 1); if (end > list.size()) { end = list.size(); //如果end不能超過最大索引值 } scoreMapper.pgInsertOrUpdateBatch(list.subList(v * i, end)); //插入數據庫 logger.info("更新一次~~~{}-{}", v * i, end); } }
感謝各位的閱讀,以上就是“mybatisplus批量更新太慢該怎么解決”的內容了,經過本文的學習后,相信大家對mybatisplus批量更新太慢該怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。