首先,创建一个简单的 insert 语句:
<insert id=”insertname”>insert into names (name) values (#{value})
</insert>
然后在 java 代码中像下面这样执行批处理插入:
list < string > names = new arraylist();
names.add(“fred”);
names.add(“barney”);
names.add(“betty”);
names.add(“wilma”);
// 注意这里 executortype.batch
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();
}