使用MyBatis + MySql进行批量修改操作时的错误,及改正方法
报错信息:
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。。。
1.错误背景:
增删改查以及批量添加方法测试全部正常,只有批量修改方法是出现报错
2.下面是具体代码:
在Mapper.xml文件中编写批量修改方法
<!--批量修改--> <!--参数:list--> <!--无返回值--> <update id="updateBatch" parameterType="java.util.List" > <foreach collection="list" item="item" separator=";"> update shop.manage <set> <if test="item.userName != null"> userName = #{item.userName}, </if> <if test="item.passWord != null"> passWord = #{item.passWord}, </if> <if test="item.realName != null"> realName = #{item.realName}, </if> </set> <where> <if test="item.id != null"> and id = #{item.id} </if> </where> </foreach> </update>
测试代码
/*测试批量修改*/ @Test public void test10(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); ManageMapper managerMapper = context.getBean("manageMapper", ManageMapper.class); ArrayList<Manage> manages = new ArrayList<Manage>(); for (int i = 10; i <= 19; i++) { manages.add(new Manage(i,"admin_" + i + i,"00000" + i + i,"realName_" + i + i)); } managerMapper.updateBatch(manages); for (Manage manage : manages) { System.out.println(manage); } }
经过检查,所有相关代码均没有错误。
报错信息如下:(这里的信息太长了,大概意思就是sql语法有问题,但是我把日志中生成的sql语句放到sqlyog中运行,未发现异常)
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 shop.manage SET userName = 'admin_1111', ' at line 11 ### The error may exist in file [D:\workspace-ssmproject\shuiguoshop\target\classes\com\jiabowen\mapper\ManageMapper.xml] ### The error may involve com.jiabowen.mapper.ManageMapper.updateBatch-Inline ### The error occurred while setting parameters ### SQL: update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? WHERE id = ? ; update shop.manage SET userName = ?, passWord = ?, realName = ? 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 shop.manage SET userName = 'admin_1111', ' at line 11 ; 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 shop.manage SET userName = 'admin_1111', ' at line 11
后来经过搜索发现,在url中添加
&allowMultiQueries=true 问题解决