mySql批量更新失败报The error occurred while setting parameters

mapper xml配置情况

  <update id="updateUserBatch" parameterType="java.util.List">
    <foreach collection="users" index="index" item="item" separator=";">
      update user
      set name = #{item.name,jdbcType=VARCHAR},
      age = #{item.age,jdbcType=INTEGER},
      address = #{item.address,jdbcType=VARCHAR},
      country = #{item.country,jdbcType=VARCHAR}
      where id = #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>

测试java

package com.skindow;

import com.skindow.mapper.UserMapper;
import com.skindow.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SkindowProviderApplicationTests {
	@Autowired
	UserMapper userMapper;
	@Test
	public void contextLoads() {
		List<User> list = new ArrayList<>(100);
		User user = null;
		for (int i = 0 ; i < 100 ; i++)
		{
			user = new User();
			user.setId(i);
			user.setAddress("武汉" + i);
			user.setAge(i);
			user.setCountry("中国");
			user.setName("skindow" + i);
			list.add(user);
			user = null;
		}
		int i = userMapper.updateUserBatch(list);
		log.info("总共批量更新了{}条用户信息",i);
	}

}

报错信息如下:

2019-08-30 09:35:03.309  INFO 16948 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-08-30 09:35:04.455  INFO 16948 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update user
      set name = 'skindow1',
      age = 1,
      address = '武汉1' at line 8
### The error may involve com.skindow.mapper.UserMapper.updateUserBatch-Inline
### The error occurred while setting parameters
### SQL: update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?      ;        update user       set name = ?,       age = ?,       address = ?,       country = ?       where id = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update user
      set name = 'skindow1',
      age = 1,
      address = '武汉1' at line 8
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update user
      set name = 'skindow1',
      age = 1,
      address = '武汉1' at line 8

	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy69.update(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy70.updateUserBatch(Unknown Source)
	at com.skindow.SkindowProviderApplicationTests.contextLoads(SkindowProviderApplicationTests.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update user
      set name = 'skindow1',
      age = 1,
      address = '武汉1' at line 8
	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:423)
	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:944)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1242)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
	... 36 more


原因是mysql配置没有配置允许批量更新的属性,mysql默认是不允许批量更新的

&allowMultiQueries=true

在这里插入图片描述
加完我们在重新测试一下
在这里插入图片描述
ok 测试成功

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值