在数据量大的情况下,可以使用批量 提高性能

批量插入insert

方法一:

<insert id="insertbatch" parameterType="java.util.List">

<selectKey keyProperty="id" order="AFTER"

resultType="int">

SELECT LAST_INSERT_ID()

</selectKey>

INSERT INTO  sourcedoc (

sdate, sweek, 

      roomno, daysched, nightsched, 

      adminsched, vacationsched, programdept, 

      programname 

)values

<foreach collection="list" item="item" index="index" separator=",">

(

#{item.sdate},#{item.sweek},#{item.roomno},

#{item.daysched},#{item.nightsched},#{item.adminsched},

#{item.vacationsched},#{item.programdept},#{item.programname}

        )

</foreach>
</insert>

方法二:

<insert id="batchInsert" parameterType="ArrayList">

insert into table1(sdate,sweek,roomno,daysched,nightsched,adminsched,vacationsched,programdept,programname)

<foreach collection="list" item="item" index="index" separator="union all">

select #{item.sdate,jdbcType=VARCHAR},

#{item.sweek,jdbcType=VARCHAR},

#{item.roomno,jdbcType=VARCHAR},

#{item.nightsched,jdbcType=VARCHAR},

#{item.adminsched,jdbcType=VARCHAR},

#{item.vacationsched,jdbcType=VARCHAR},

#{item.programdept,jdbcType=VARCHAR},0,0,

#{item.programname,jdbcType=VARCHAR}
from dual

</foreach>

</insert>

批量删除(delete)

<!-- 通过主键集合批量删除记录 -->

<delete id="batchRemoveUserByPks" parameterType="java.util.List">

DELETE FROM LD_USER WHERE ID in

<foreach item="item" index="index" collection="list" open="(" separator="," close=")">

#{item}

</foreach>

</delete>

批量修改(update)

update orders set state = '0' where no in
 <foreach collection="list" item="nos" open="(" separator="," close=")">
  #{nos}
 </foreach>
 </update>

MyBatis中in子句  in 参数 使用方法

1.只有一个参数

参数的类型要声明为List或Array

Sql配置如下:

<select id="selectProduct" resultMap="Map">

SELECT * FROM PRODUCT

WHERE PRODUCTNO IN

<foreach item="productNo" index="index" collection="参数的类型List或array">

#{productNo}

</foreach>

</select>

2.多个参数

首先要将多个参数写入同一个map,将map作为一个参数传入mapper

Sql配置如下:

<select id="selectSoucedoc" resultMap="Map">

SELECT *

FROM PRODUCT

WHERE PRODUCTNO IN

<foreach item="id" index="index" collection="map中集合参数的名称">

#{id}

</foreach>

</select>

##############

    1. <insert id="addTrainRecordBatch" useGeneratedKeys="true" parameterType="java.util.List">
    2. <selectKey resultType="long" keyProperty="id" order="AFTER">
    3. SELECT
    4. LAST_INSERT_ID()
    5. </selectKey>
    6. insert into t_train_record (add_time,emp_id,activity_id,flag)
    7. values
    8. <foreach collection="list" item="item" index="index" separator="," >
    9. (#{item.addTime},#{item.empId},#{item.activityId},#{item.flag})
    10. </foreach>
    11. </insert>

对于foreach标签的解释参考了网上的资料,具体如下:

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,

open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,

close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,

但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map

mybatis---------insert,delete ,update的批量操作的更多相关文章

  1. MyBatis insert/delete/update 的返回值

    insert,返回值是:新插入行的主键(primary key):需要包含<selectKey>语句,才会返回主键,否则返回值为null. <insert id="inse ...

  2. 带有OUTPUT的INSERT,DELETE,UPDATE

    原文地址:http://blog.sina.com.cn/s/blog_71460d950100nld2.html OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可 ...

  3. Use Select To Generate Any Insert/Delete/Update Statement

    If you don't have the permission to generate script according to an existing db, but you have the re ...

  4. sqlserver触发器insert,delete,update

    Create Trigger [dbo].[upemployee_kefyu_sale] on [dbo].[employee] for update as if update(FullName) b ...

  5. sql server 带有OUTPUT的INSERT,DELETE,UPDATE

    原文:sql server 带有OUTPUT的INSERT,DELETE,UPDATE OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可以看作是"返回结果 ...

  6. MySQL进阶10--DML数据操纵预言: insert/delete/update --多表连接修改/.多表连接删除/多表连接查询-- truncate 和 delete的区别

    /* DML -- 数据操纵预言: insert/delete/update */ #一: 插入语句 /* 语法1: insert into 表名(列名,..,列名....) values(值1,值2 ...

  7. mybatis insert、update 、delete默认返回值解释与如何设置返回表主键

    在使用mybatis做持久层时,insert.update.delete,sql语句默认是不返回被操作记录主键的,而是返回被操作记录条数: 那么如果想要得到被操作记录的主键,可以通过下面的配置方式获取 ...

  8. mybatis insert转update,duplicate关键字的使用示例,及返回情况说明

    主键存在时又insert转为update某个关键字段,示例如下,注意,如果这条数据曾经不存在,此时执行insert返回条目是1,如果已存在,执行update返回条目是2!!!<insert id ...

  9. MyBatis日记(四):MyBatis——insert、update、delete、select

    MyBatis简单增删改查操作,此处所做操作,皆是在之前创建的MyBatis的Hello world的工程基础上所做操作. 首先在接口文件(personMapper.java)中,添加操作方法: pa ...

  10. Mybatis Insert、update、delete流程

    上文mybatis源码简书我们讲到sqlsession中通过executor来执行sql,我们接着往下看 update方法点进去,我们进到baseexecutor 这里我们看到 clearLocalC ...

随机推荐

  1. ansible 判断和循环

    标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...

  2. Mssql 行转列

    ) set @sql='' --初始化变量@sql --变量多值赋值 ,,'')--去掉首个',' set @sql=' select * from( select objectid,name,jyj ...

  3. rune is alias of int32

    I think chendesheng's quote gets at the root cause best: Go uses a lot of signed values, not just fo ...

  4. C++设计模式-singleton单例模式_new

      class nocopyable { protected: nocopyable(){}; virtual ~nocopyable(){}; nocopyable(const nocopyable ...

  5. 用户输入密码隐藏之getpass的使用

    有的时候,比如商城登录的时候,我希望输入的时候我的密码不为明文,如何实现呢? 这里就需要利用getpass模块中的getpass方法.注意,需要在linux上或者windows下运行,在pycharm ...

  6. python报错Could not open PYTHONSTARTUP

    root@liqian-python:/pythonShare/monitor/m_server/core# pythonPython 2.7.10 (default, Oct 14 2015, 16 ...

  7. redis13---事务处理。

    Jedis事务我们使用JDBC连接Mysql的时候,每次执行sql语句之前,都需要开启事务:在MyBatis中,也需要使用openSession()来获取session事务对象,来进行sql执行.查询 ...

  8. Memcached内存存储

    早就听说过Memcached独特的内存管理方式,写着篇文章的目的就是了解Memcached的内存管理,学习其源代码. 1.什么是Slab Allocator memcached默认情况下采用了名为Sl ...

  9. 更新sdk

    更新sdk,遇到了更新下载失败问题: Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xmlFetched Ad ...

  10. Google Dremel 原理 - 如何能3秒分析1PB

    简介 Dremel 是Google 的“交互式”数据分析系统.可以组建成规模上千的集群,处理PB级别的数据.MapReduce处理一个数据,需要分钟级的时间.作为MapReduce的发起人,Googl ...