mybatis批量insert出错

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qtay.gls.dao.TeacherLabelDao">
    <sql id="key">
        <trim suffixOverrides=",">
            <if test="item.id!=null">
                id,
            </if>
            <if test="item.teacherId != null">
                teacher_id,
            </if>
            <if test="item.name!=null">
                name,
            </if>
        </trim>
    </sql>
    <sql id="value">
        <trim suffixOverrides=",">
            <if test="item.id!=null">
                #{item.id},
            </if>
            <if test="item.teacherId!=null">
                #{item.teacherId},
            </if>
            <if test="item.name!=null">
                #{item.name},
            </if>
        </trim>
    </sql>
    <insert id="saveTeacherLabels" keyProperty="id" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            insert into teacher_labels(<include refid="key"/>) values(<include refid="value"/>)
        </foreach>
    </insert>
</mapper>
### 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 'insert into teacher_labels( 
         id,
            
            
            ' at line 19
### The error may involve com.qtay.gls.dao.TeacherLabelDao.saveTeacherLabels-Inline
### The error occurred while setting parameters
### SQL: insert into teacher_labels(           id,                                           teacher_id,                                           name       ) values(           ?,                                           ?,                                           ?       )          ;              insert into teacher_labels(           id,                                           teacher_id,                                           name       ) 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 'insert into teacher_labels( 
         id,
            
            
            ' at line 19
; 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 'insert into teacher_labels( 
         id,
            
            
            ' at line 19, path=/teacher/save}] as "application/json;charset=utf-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@39023dbf]
2017-11-20 14:22:55.547 DEBUG [http-nio-9090-exec-4] o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-11-20 14:22:55.547 DEBUG [http-nio-9090-exec-4] o.s.web.servlet.DispatcherServlet - Successfully completed request

只单个添加一条数据是正确的,当添加2个及以上就报上面的错,请问如何解决?

阅读 7.1k
2 个回答

根据楼上的提示,发现确实如此,然后百度了一下,加上allowMultiQueries=true就OK了

spring.datasource.url=jdbc:mysql://yoururl?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true

Mybatis 每次只能执行一条语句。
批量插入

<insert id="saveTeacherLabels" keyProperty="id" parameterType="java.util.List">
    insert into teacher_labels(id, teacher_id, name) values
    <foreach collection="list" item="item" index="index" separator=",">
        (<include refid="value"/>)
    </foreach>
</insert>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题