久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

解決spring data jpa 批量保存更新的問題

瀏覽:68日期:2023-07-11 14:59:20
spring data jpa 批量保存更新問題

使用jpa批量保存時,看日志發現是一條一條打印的,然后去看了下源碼,果然是循環調用的單個保存(巨坑啊)

解決spring data jpa 批量保存更新的問題

經查詢jpa是可以實現批量保存更新的,具體設置如下:

spring.jpa.properties.hibernate.jdbc.batch_size=500spring.jpa.properties.hibernate.jdbc.batch_versioned_data=truespring.jpa.properties.hibernate.order_inserts=truespring.jpa.properties.hibernate.order_updates =true其中:batch_size根據自己的數據庫情況來設置

配置好后,感覺終于可以批量保存了,立馬試了一把,結果,一首涼涼。。。(并沒什么用)

繼續查資料,終于發現了還有一個坑,那就是jpa中主鍵策略會影響批量功能!!

如果主鍵策略使用了IDENTITY 也就是@GeneratedValue(strategy = GenerationType.IDENTITY),那么批量功能不支持的,

如果要開啟批量,那么就要使用sequence策略,也就是@GeneratedValue(strategy = GenerationType.SEQUENCE),

然后立馬去開啟,結果發現再次入坑,原來mysql數據庫是無法使用sequence策略的,已淚奔。。。。。

批量功能只能自己去單獨找尋方法實現了。

jpa在批量添加的時候,存儲慢如何解決問題

spring.datasource.url = jdbc:mysql://xxxxxxxx:xxxx/xxxxx?useSSL=false&useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true

解決spring data jpa 批量保存更新的問題

入口下加

@EnableTransactionManagement@SpringBootApplication@EnableTransactionManagement

service實現類下加

@Service@Transactional![在這里插入圖片描述](https://img-blog.csdnimg.cn/20191220111053531.png引用@Autowiredprivate NamedParameterJdbcTemplate namedParameterJdbcTempla

解決spring data jpa 批量保存更新的問題

sql語句這樣寫:根據自己的字段多少來寫格式不能變下面是一個demo

String sql = “INSERT INTO xxxx(id,name,age) VALUES (:id,:name,:age)”;List uparChnMulMinList = (List) map.get(“uparChnMulMinList”);SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(uparChnMulMinList.toArray());namedParameterJdbcTemplate.batchUpdate(sql, batch);

自定義的list:List

SqlParameterSourceUtils導入這個直接用,調用這個方法createBatch();

開啟數據空批量配置:

rewriteBatchedStatements=true

開啟事務:

@EnableTransactionManagement ,@Transactional

可以一試。以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 四子王旗| 湖北省| 临安市| 衡南县| 河曲县| 达孜县| 乌兰县| 巴南区| 绥宁县| 龙州县| 洛隆县| 漳州市| 久治县| 孝义市| 罗平县| 牙克石市| 桂阳县| 苍溪县| 蕉岭县| 中方县| 维西| 丰顺县| 金川县| 赤城县| 纳雍县| 广宁县| 浙江省| 宁蒗| 铜陵市| 昭苏县| 城步| 邳州市| 民县| 珠海市| 临桂县| 桂平市| 龙海市| 湖北省| 萨迦县| 五寨县| 绿春县|