MyBatis--动态插入多条数据

MySQL支持的一种插入多行数据的INSERT语句写法是

INSERT INTO 表名 (字段名1,字段名2,字段名3) VALUES (值1,值2,值3,...),(值1,值2,值3,...)...

对应的接口方法

//给News插入多条类别
    public int addMoreNewsTypeByNewsId(@Param("newsId")int newsId, @Param("newsTypes")List<NewsType> newsTypeList);

xml文件配置方法:

<insert id="addMoreNewsTypeByNewsId">
            insert into news_newstype (newsid,newstypeid)
            values
            <foreach collection="newsTypes" item="type" separator=",">
                (#{newsId},#{type})
            </foreach>
    </insert>

猜你喜欢

转载自www.cnblogs.com/charlottepl/p/12636222.html