報錯信息:
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 問題解決