Mybatis之批量新增数据(7)

来源:互联网 发布:百度人工智能前景 编辑:程序博客网 时间:2024/06/05 17:22
注意下面的语法是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 blocke.printStackTrace();} finally {if(sqlSession != null) {sqlSession.close();}}}