批量插入
void batchInsertBackup(List<UserRights> list);
<insert id ="batchInsertBackup" parameterType="java.util.List" >
insert into user_rights_backup
(id,supplier_id,user_id,rights_id,rules_id,
start_time,end_time,goods_no,has_num,type,
status,ctime,mtime,biz_type,first_type,
is_limit_time)
values
<foreach collection ="list" item="entityRecord" index= "index" separator =",">
(
#{entityRecord.id}, #{entityRecord.supplierId},#{entityRecord.userId},#{entityRecord.rightsId},#{entityRecord.rulesId},
#{entityRecord.startTime}, #{entityRecord.endTime}, #{entityRecord.goodsNo},#{entityRecord.hasNum},#{entityRecord.type},
#{entityRecord.status},#{entityRecord.ctime},#{entityRecord.mtime},#{entityRecord.bizType},#{entityRecord.firstType},
#{entityRecord.isLimitTime}
)
</foreach >
</insert > 批量删除源数据
void batchDelete(List<UserRights> list);
<delete id="batchDelete" parameterType="java.util.List" >
delete from user_rights where id in (
<foreach collection="list" item="entity" index= "index" separator=",">
#{entity.id}
</foreach>
)
</delete>

根据id,批量查询
List<UserRights> batchQuery(List<Long> idList);
<select id="batchQuery" resultMap="BaseResultMap" parameterType="java.util.List">
select
<include refid="base_column_list"/>
from user_rights
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item,jdbcType=BIGINT}
</foreach>
</select>

根据id,批量更新时间和状态
<update id="updateBatch" parameterType="java.util.List">
update user_rights
<trim prefix="set" suffixOverrides=",">
<trim prefix="status = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.status}
</foreach>
</trim>
<trim prefix=" start_time = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.startTime}
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>

首先,mysql需要数据库连接配置&allowMultiQueries=true

jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

oracle下支持执行多条语句,下面3个相同

<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" >
update T_EMP_1
<set>
age = #{item.age}+1,name=#{item.name}
</set>
where id = #{item.id}
</foreach>
</update>
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" >
update T_EMP_1
<set>
age = #{item.age}+1,name=#{item.name}
</set>
where id = #{item.id};
</foreach>
</update>
<update id="batchUpdate" parameterType="java.util.List">
begin
<foreach collection="list" item="item" index="index" separator="" >
update T_EMP_1
<set>
age = #{item.age}+1,name=#{item.name}
</set>
where id = #{item.id};
</foreach>
end;
</update>

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,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key

批量删除

<delete id="batchDeleteStudent" parameterType="List">
DELETE FROM STUDENT WHERE id IN
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>

批量更新 注意:oracle中 形如 update *** set *** where ** in(....) 这种语句 in所在的集合有条数限制 为1000条

<update id="batchUpdateStudent" parameterType="List">
UPDATE STUDENT SET name = "5566" WHERE id IN
<foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
#{item}
</foreach>
</update>
<update id="batchUpdateStudentWithMap" parameterType="Map" >
UPDATE STUDENT SET name = #{name} WHERE id IN
<foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</update>

批量插入  mysql和oracle不一样

mysql:
<insert id="batchInsertStudent" parameterType="java.util.List">
INSERT INTO STUDENT (id,name,sex,tel,address)
VALUES
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
</foreach>
</insert>
oracle:
<insert id="insertBatch4Oracle" parameterType="List">
insert into aa(a,b)
<foreach collection="list" item="item" index="index" separator="union all" >
  select #{item.a},#{item.b} from dual
</foreach>
</insert>

Mybatis批量操作的更多相关文章

  1. mybatis批量操作-xml方式

    在实际项目中,我们一般都会用到批量insert.delete.update等操作,由于使用频率还是蛮高的,这里就做个简单的记录,供以后学习和参考. 批量insert 在数据库中,批量插入可以是多条in ...

  2. Mybatis 批量操作-删除、修改和查询

          批量操作的核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中一个,其它的就可以举一反三,触类旁通.它之所以执行效率高,是因为合并后日志量(MySQL的binlog和InnoDB的 ...

  3. MyBatis 批量操作、集合遍历-foreach

    在使用mybatis操作数据库时,经常会使用到批量插入.IN条件查询的情况,这时就难免要使用到foreach元素.下面一段话摘自mybatis官网: foreach 元素的功能是非常强大的,它允许你指 ...

  4. MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]

    问题背景: 在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下: com.chenzhou.base.mybati ...

  5. mybatis 批量操作增删改查

    在介绍批量操作之前,首先先介绍一个语法:foreach.可以说是,foreach是整个批量操作的灵魂. 属性 描述 item 循环体中的具体对象. 支持属性的点路径访问,如item.age,item. ...

  6. mybatis 批量操作 ------持续更新

    mybatis 不存在则才进行添加 # 添加的 sql 语句insert into sys_link_post_user(post_id,user_id)# 进行批量添加 (若不需要可以取消 froe ...

  7. (七)mybatis批量操作,分页插件

    首先使用方式很简单: SqlSession sqlSession = sessionFactory.openSession(ExecutorType.BATCH); 批量操作核心:改变执行sql的方式 ...

  8. on duplicate key update 的用法说明(解决批量操作数据,有就更新,没有就新增)mybatis批量操作数据更新和添加

    项目用的ORM框架是用springdatajpa来做的,有些批量数据操作的话,用这个效率太低,所以用mybatis自己写sql优化一下. 一般情况,我们肯定是先查询,有就修改,没有就添加,这样的话,单 ...

  9. 170515、mybatis批量操作

    //Java代码 public void batchAdd(){ SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); Stud ...

随机推荐

  1. Webpack:前端资源模块化管理和打包工具

    一.介绍: Webpack 是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生 产环境部署的前端资源.还可以将按需加载的模块进行代码分隔,等到实际需要的时候再 ...

  2. laravel下的数据序列化

    $data=$this->user->get(); //is obj $data=(string)$data; //is string $data=$data->toArray(); ...

  3. java重置定时器频率

    public class BallUtil { public static Timer fisTimer ; public static void fisStartBall(){ long first ...

  4. 【CoreAnimation】1 到 5

    学习资源来自:图层树 . Quartz 2D Core Animation 复合引擎,职责为尽可能快地组合屏幕上不同的可视内容.这些内容被分解成多个独立的图层,存储在 图层树 的体系中.于是这个树形成 ...

  5. BZOJ1492: [NOI2007]货币兑换Cash

    设$x_j$,$y_j$为第$j$天能买的A,B券数量,$f_i$为第$i$天的最大收益.$f_i=\max_{1\le j<i}a_ix_j+b_iy_j$,最大化$f_i$即找一个点$(x_ ...

  6. Make 命令教程

    http://www.ruanyifeng.com/blog/2015/02/make.html 作者: 阮一峰 日期: 2015年2月20日 代码变成可执行文件,叫做编译(compile):先编译这 ...

  7. macOS Sierra U盘USB启动安装盘

    http://www.iplaysoft.com/macos-usb-install-drive.html 文章里提供了一个软件可以自动化操作,但是试了下,不行,试了两三次还是不行,只有参照文章中的第 ...

  8. Google数据交换格式:ProtoBuf

    Protocol Buffer ProtocolBuffer是Google公司的一个开源项目,用于结构化数据串行化的灵活.高效.自动的方法,有如XML,不过它更小.更快.也更简单.你可以定义自己的数据 ...

  9. MongoDB【第二篇】MongoDB逻辑与物理存储结构

    基本的操作 一.常用的命令和基础知识 1.进入MongoDB sehll 首先我们进入到MongoDB所在目录执行 cd /work/app/mongodb/bin/ #启动 ./mongo 为了方便 ...

  10. nginx配置ssl证书的方法

    Nginx (读音"engine x") 是一个高性能的HTTP和反向代理服务器,比Apache占用更少的内存,同时也像Apache一样支持HTTPS方式访问(SSL加密).本教程 ...