mybatis使用foreach进行批量保存

mysql下批量保存  mysql支持语法 inset into table values(),(),().... 可以使用foreach进行遍历保存第一种方法<insert id="addEmps" parameterType="queryVo" resultType="user">

insert into employee (last_name,email,gender)

  values

<!-- foreach标签,进行遍历 -->

<!-- collection:遍历的集合,这里是QueryVo的ids属性 -->

<!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->

<!-- open:在前面添加的sql片段 -->

<!-- close:在结尾处添加的sql片段 -->

<!-- separator:指定遍历的元素之间使用的分隔符 -->

<!--    示例sql语句  insert into employee (last_name,email,gender) values(?,?,?),(?,?,?)  -->

<foreach collection="emps" item="emp"   separator=",">

( #{emp.last_name} , #{email} , #{gender} )
</foreach>
</insert>

第二种方法发送多条sql  也是使用foreach,每次遍历一个员工就生成一个完整的sql语句需要配置连接属性运行多个sql语句之间通过分号 ; 进行连接配置allowMultiQueries=true  默认为false
<foreach collection="emps" item="emp"   separator=";">
insert into employee (last_name,email,gender)
  values
( #{emp.last_name} , #{email} , #{gender} ) </foreach>

接口

int addEmps(@(Param("emps") List<Employee> list)

使用Oracle数据库进行批量保存

  Oracle不支持values(),(),().....

Oraclez批量保存方式

其中一种

  begin

    insert into table values();

    insert into table values();

  end;

 

<insert id="addEmps" adtabaseId="oracle">
<foreach collection="emps" item="emp" open="begin" close="end;">
insert into employee (id,last_name,email,gender)
  values
oracle不支持自增主键,只有序列,通过序列插入id
( employees.seq.nextval,#{emp.last_name} , #{email} , #{gender} )
</foreach>
</insert>

还有一种中间表方式进行批量插入,我没有去了解,这里偷下懒,大家有兴趣可以去了解一下

原文地址:https://www.cnblogs.com/a-small-lyf/p/10324788.html

时间: 2024-08-30 10:22:31

mybatis使用foreach进行批量保存的相关文章

mybatis使用foreach进行批量插入和删除操作

一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Param中的参数必须和mapper.xml中foreach的collection对应,若果不写@Param注解并且只传入一个list作为参数,则collection默认填list 2.mapper.xml <insert id="insertBatchRoleUser"> INS

Mybatis批量操作与JDBC批量操作效率测试

Mybatis-spring配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springfr

Mybatis 针对oracle的批量插入

xml文件参数对象: 1 public class Columns{ 2 private String basedate_code; 3 private String tableName; 4 private String codeDescribeindex; 5 private String codeInfoindex; 6 private String codePymindex; 7 private List<Info> infos; Dao层方法: public void bulkIns

mybatis学习之路----批量更新数据两种方法效率对比

原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方式有两种, 一种用for循环通过循环传过来的参数集合,循环出N条sql, 另一种 用mysql的case when 条件判断变相的进行批量更新 下面进行实现. 注意第一种方法要想成功,需要在db链接url后面带一个参数  &allowMultiQueries=true 即:  jdbc:mysql:

MyBatis的foreach语句详解

MyBatis的foreach语句详解 MyBatis的foreach语句详解 1人收藏此文章, 我要收藏 发表于3个月前 , 已有113次阅读 共0个评论 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,se

【java】itoo项目实战之hibernate 批量保存优化

在itoo中.基本上每一个系统都有一个导入功能,大量的数据填写进入excel模板中.然后使用导入功能导入的数据库中,这样能够大大的提高工作效率. 那么导入就涉及到了批量保存数据库的问题了. 那么通常情况下,在一个Session对象的缓存中数量有限的持久化对象,等到Session对象处理事务完成,还要关闭Session对象,从而及时释放session的缓存占用的内存.在批量保存1万条数据,假设一次性把须要保存的1万条数据载入到内存职工,当运行事务提交的时候,就会清理缓存,hibernate运行1万

sqlbulkcopy 多表批量保存

/// <summary> /// 批量保存多表 /// </summary> /// <param name="dt1"></param> /// <param name="TableName"></param> /// <returns></returns> public static void SqlBatchCopy(DataTable dt, string Ta

利用Python批量保存51CTO博客

一.背景 最近在整理博客,近在51CTO官网存在文章,想将之前写的全部保存到本地,发现用markdown写的可以导出,富文本的则不行,就想利用Python批量保存自己的博客到本地. 二.代码 git地址 #!/bin/env python # -*- coding:utf-8 -*- # _auth:kaliarch import requests import time from bs4 import BeautifulSoup from selenium import webdriver c

mybatis批量保存

a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2px; } a:hover { outline: 0; } a:active { outline: 0; } a:hover { color: #005580; text-decoration: underline; } blockquote small:before { content: '\201