如何在MyBatis中插入Arraylist?

时间:2017-05-19 03:02:51

标签: java spring-boot mybatis

你好今天我使用Mybatis插入Arraylist有问题,如下面的代码:

@Insert("<script>"
            + "   <foreach collection='USER_LIST' item='User' separator=';'> "
            + "      insert into tbl_user(username,role) values(#{User.username},#{User.role})"
            + "  </foreach>"
            + "</script>")

当我插入单项arraylist时,它工作。但如果我在arraylist错误中有多个项目:

 insert into tbl_user(username,role)             values(?,?);   ;        values(?,?);
### 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 ';        values('BEAN','BMd')' at line 1
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 ';        values('BEAN','BMd')' at line 1; bad SQL grammar []; nested exception is 

1 个答案:

答案 0 :(得分:0)

来自异常,&#34;错误的SQL语法[];嵌套异常是&#34;, 我在你的代码中发现了这一点&#34; separator =&#39 ;;&#39;&gt;&#34;,没错,myBatis不允许你写'&#39 ;;&#39 ;