jpa在批量添加的时候,存储慢如何解决问题。

spring.datasource.url = jdbc:mysql://xxxxxxxx:xxxx/xxxxx?useSSL=false&useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true
在这里插入图片描述
入口下加@EnableTransactionManagement
@SpringBootApplication
@EnableTransactionManagement
service实现类下加
@Service
@Transactional
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191220111053531.png引用
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTempla
在这里插入图片描述
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
可以一试。

猜你喜欢

转载自blog.csdn.net/qq_43268461/article/details/96339340
今日推荐