1、设置SqlSession为批量操作类型
sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch);
try {
namemapper mapper = sqlsession.getmapper(namemapper.class);
for (string name : names) {
mapper.insertname(name);
}
sqlsession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}
finally {
sqlsession.close();
}
然后执行insert方法即可
2、通过foreach 遍历需要操作的值
<insert id="insertByBatch" parameterType="java.util.List">
insert into user(name)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name,jdbcType=VARCHAR}
</foreach>
</insert>