注意下面的语法是MySQL的批量插入语句,不同的数据库语法可能不一样。
1、定义插入的SQL语句
<insert id="insertBatch" parameterType="java.util.List">
insert into COMMAND_CONTENT(CONTENT,COMMAND_ID) values
<foreach collection="list" item="item" separator=",">
(#{item.content},#{item.commandId})
</foreach>
</insert>
2、定义Mapper接口的方法
/**
* 批量新增
*/
public void insertBatch(List<CommandContent> content);
3、义具体的插入语句的方法
/**
* 批量新增
*/
public void insertBatch(List<CommandContent> contentList) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
// 通过sqlSession执行SQL语句
ICommandContent commandContent = sqlSession.getMapper(ICommandContent.class);
commandContent.insertBatch(contentList);
sqlSession.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(sqlSession != null) {
sqlSession.close();
}
}
}