1、mybatis中使用sql批量插入数据
<insert id="insertDept" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="deptId">
INSERT INTO sys_dept (parent_id, dept_name, levels, create_by, ancestors, types, create_time, status)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.parentId},
#{item.deptName},
#{item.levels},
#{item.createBy},
#{item.ancestors},
5,
sysdate(),
#{item.status}
)
</foreach>
</insert>
注意这两项的设置、deptId为实体类中主键的id
useGeneratedKeys=“true” keyProperty="deptId"
2、在service的插入对象中,能够批量获取到ID