您的位置:首页 > 其它

Mybatis之批量新增数据(7)

2017-11-29 05:43 357 查看
注意下面的语法是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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mybatis 批量新增