mapper中方法
void insertList(@Param("relaList") List<user> relaList);
xml中的语句
<insert id="insertList" parameterType="com.Entity.user" useGeneratedKeys="false">
INSERT INTO user
(id,pkg_id,`age`)
VALUES
<foreach collection="relaList" item="item" index="index" separator=",">
(
#{item.id},
#{item.pkgId},
#{item.age}
)
</foreach>
</insert>
注意的地方:
1、在xml中的parameterType="" 是mapper方法中的list中的类型。不是java.util.List类型,否则会报错。我的就是因为这个报错的。
2、插入列中的字段不能有数据库关键字,否则也会报错,如果有关键字用 `` (Esc)见下面的那个键包起来也行。
3、如果mapper方法中加了@param注解,xml collection中就用注解里面的参数名collection=“relaList”,如果没有@param,就直接用list代替。collection=“list”