Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘330’ for key ‘PRIMARY’
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
解答:综上所示,其实批量的数据是不重复的,而插入操作却报了某个值重复插入
数据库连接字符串 必须配置 allowMultiQueries=true
如 jdbc:mysql://10.136.34.211:3306/scyw?allowMultiQueries=true&autoReconnect=true&useSSL=false&characterEncoding=utf-8
解决方案:
1 整句foreach
自己使用的解决办法:
<insert id="batchSaveFinale" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
<foreach item="item" collection="list" separator="," >
insert ignore into tpromotion_finalist
(
user_id,
transac_id
)
values
(
#{item.userId},
#{item.transacId}
)
</foreach>
</insert>
2 插入sql语句中 加入ignore关键字
网上查到的解决办法:
<insert id="batchSaveFinale" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert ignore into tpromotion_finalist
(
user_id,
transac_id
)
values
<foreach item="item" collection="list" separator="," >
(
#{item.userId},
#{item.transacId}
)
</foreach>
</insert>
原文:https://blog.csdn.net/dingxingmei/article/details/89406012