mybatis的批量操作
来源:互联网 发布:slack软件应用 编辑:程序博客网 时间:2024/05/21 08:53
1.批量更新
1)mysql
<update id="updateStoreTask" parameterType="Object"> <foreach collection="storeTaskList" item="item" index="index" separator=";"> update t_store_task set store_sale_task=#{item.storeSaleTask} where task_id=#{taskId} AND store_no=#{item.storeNo} </foreach> </update>
注意:
1.在配置时必须加上allowMultiQueries=true如:
url=”jdbc:mysql://localhost:3306/testDatabase?allowMultiQueries=true”
allowMultiQueries=true时,可以允许一次执行多条sql(通过分号分割)
2.最后编译的结果为
Preparing: update t_store_task set store_sale_task=? where task_id=? AND store_no=? ; update t_store_task set store_sale_task=? where task_id=? AND store_no=?
2)oracle
<!--oracle--> <update id="updateStoreTask" parameterType="Object"> <foreach collection="storeTaskList" item="item" index="index" open="begin" close=";end;" separator=";"> update t_store_task set store_sale_task=#{item.storeSaleTask} where task_id=#{taskId} AND store_no=#{item.storeNo} </foreach> </update>
@see https://www.cnblogs.com/feixian/p/5960111.html
2.批量插入
1)mysql
<insert id="batchAdd" parameterType="Object"> insert into t_store_task(task_id,store_no,store_name,store_sale_task) values <foreach collection="storeTaskList" item="item" index="index" separator=","> (#{taskId},#{item.storeNo},#{item.storeName},#{item.storeSaleTask}) </foreach> </insert>
最后编译的结果为
Preparing: insert into t_store_task(task_id,store_no,store_name,store_sale_task) values (?,?,?,?) , (?,?,?,?)
3.批量筛选
in条可以实现通过id批量查询,批量删除,批量更新
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。
<if test="categoryList != null and categoryList.size()!=0"> and produt_pl IN <foreach collection="categoryList" item="item" index="index" open="(" close=")" separator=","> #{item.categoryNo} </foreach> </if>
编译结果:
and produt_pl IN ( ? , ? )
二。插入不成功执行更新
1.oracle
<!--插入不成功,则执行更新--> <insert id="batchSaveOrUpdate" parameterType="Object"> MERGE INTO t_store_task T1 USING ( <foreach collection="storeTaskList" item="item" index="index" separator="union"> SELECT #{taskId} task_id, #{item.storeNo} store_no, #{item.storeName} store_name, <choose> <when test="item.storeSaleTask!=null"> #{item.storeSaleTask} store_sale_task </when> <otherwise> 0 store_sale_task </otherwise> </choose> FROM DUAL </foreach> ) T ON (T1.task_id = T.task_id AND T1.store_no = T.store_no) WHEN MATCHED THEN UPDATE SET T1.store_sale_task=T.store_sale_task WHEN NOT MATCHED THEN INSERT (task_id,store_no,store_name,store_sale_task) VALUES (T.task_id,T.store_no,T.store_name,T.store_sale_task) </insert>
@see http://blog.csdn.net/gjldwz/article/details/17414155
阅读全文
0 0
- mybatis 批量的操作
- MyBatis的批量操作
- mybatis的批量操作
- mybatis的批量操作记录
- oracle+mybatis的批量操作
- mybatis 的crud及批量cud操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- mybatis批量执行mysql的update操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- MyBatis的几种批量操作
- 关于mybatis的批量删除操作
- MyBatis批量的增删改查操作
- retrofit的功能介绍
- 添加数据库
- MyBatis中PageHelper分页
- Struts2入门--第一个例子
- JavaScript——块级作用域?
- mybatis的批量操作
- 人工智障学习笔记——机器学习(7)FM/FFM
- 关于tomcat 中的 tomcat-users.xml 配置不生效原因
- Gym 101572 D- 多源bfs- Distinctive Character
- 面挂脸书之后怎么办?
- @Controller和@RestController的区别?
- NOIP2017解题报告
- tomcat+redis实现session共享配置之路 支持Tomcat 8
- 点击弹出 listview