mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记
前言:用mybatis也好几年了,mybatis在批量的增删操作也写起来也是比较简单的,只有批量更新这一块是特别坑,特此记录。
一、批量插入
1、oracle写法:
insert into b_dbgl_zaixcs (
zaixcsid, mingc, pingsyid, xinxid, fujid,
jieg, pingfjg, pingf, zhuangt, shic,
startriq, endriq, pingfriq, datr, pingfr, beiz
)
<foreach collection="list" item="item" index="index" separator="union all">
(select #{item.zaixcsid,jdbcType=VARCHAR}, #{item.mingc,jdbcType=VARCHAR},
#{item.pingsyid,jdbcType=VARCHAR},#{item.xinxid,jdbcType=VARCHAR},
#{item.fujid,jdbcType=VARCHAR}, #{item.jieg,jdbcType=VARCHAR},
#{item.pingfjg,jdbcType=VARCHAR}, #{item.pingf,jdbcType=DECIMAL},
#{item.zhuangt,jdbcType=VARCHAR},#{item.shic,jdbcType=DECIMAL},
#{item.startriq,jdbcType=TIMESTAMP}, #{item.endriq,jdbcType=TIMESTAMP},
#{item.pingfriq,jdbcType=TIMESTAMP}, #{item.datr,jdbcType=VARCHAR},
#{item.pingfr,jdbcType=VARCHAR},#{item.beiz,jdbcType=VARCHAR}
from dual)
</foreach>
</insert>
2、mysql写法:
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.fujId,jdbcType=VARCHAR},
#{item.relateId,jdbcType=VARCHAR},
#{item.relateTableName,jdbcType=VARCHAR},
#{item.fujLx,jdbcType=VARCHAR},
#{item.wenjlx,jdbcType=VARCHAR},
#{item.wenjm,jdbcType=VARCHAR},
#{item.fjmc,jdbcType=VARCHAR},
#{item.fujPath,jdbcType=VARCHAR},
#{item.createUserId,jdbcType=VARCHAR},
#{item.createUser,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP},
#{item.relateTableZiduan,jdbcType=VARCHAR},
#{item.contentType,jdbcType=VARCHAR},
#{item.zhuangt,jdbcType=VARCHAR}
)
</foreach>
二、批量删除
1、删除数组数组
<delete id="batchDeleteEmpArr" parameterType="int">
delete from emp where empno in
<foreach item="empnoItem" collection="array" open="(" separator="," close=")">
#{empnoItem}
</foreach>
</delete>
2、删除list列表数据
<delete id="batchDeleteEmpList" parameterType="int">
delete from emp where empno in
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
3、删除查询到的数据
<delete id="deleteByParent" parameterType="string">
delete from QIYDFBZ where BIAOZBID in(
SELECT biaozbid
FROM
B_DBGL_QIYDFBZ
CONNECT BY PRIOR FENXID = FUJID start WITH BIAOZBID = #{biaozbid,jdbcType=VARCHAR} )
</delete>
三、批量更新
1、oracle写法:
begin
<foreach collection="list" item="item" index="index" separator=";">
update B_DBGL_ZAIXCS
<trim prefix="set" suffixOverrides=",">
<if test="item.mingc != null and item.mingc !=''">
MINGC= #{item.mingc,jdbcType=VARCHAR},
</if>
<if test="item.pingf != null and item.pingf !=''">
PINGF=#{item.pingf,jdbcType=DECIMAL},
</if>
<if test="item.zhuangt != null and item.zhuangt !=''">
ZHUANGT=#{item.zhuangt,jdbcType=VARCHAR},
</if>
<if test="item.shic != null and item.shic !=''">
SHIC=#{item.shic,jdbcType=DECIMAL},
</if>
<if test="item.startriq != null and item.startriq !=''">
STARTRIQ=#{item.startriq,jdbcType=TIMESTAMP},
</if>
</trim>
where ZAIXCSID = #{item.zaixcsid,jdbcType=VARCHAR}
</foreach>
;end;
</update>
2、mysql写法:
例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update ZAIXCS
<trim prefix="set" suffixOverrides=",">
<if test="item.mingc != null and item.mingc !=''">
MINGC= #{item.mingc,jdbcType=VARCHAR},
</if>
<if test="item.shic != null and item.shic !=''">
SHIC=#{item.shic,jdbcType=DECIMAL},
</if>
<if test="item.startriq != null and item.startriq !=''">
STARTRIQ=#{item.startriq,jdbcType=TIMESTAMP},
</if>
<if test="item.beiz != null and item.beiz !=''">
BEIZ=#{item.beiz,jdbcType=VARCHAR},
</if>
</trim>
where ZAIXCSID = #{item.zaixcsid,jdbcType=VARCHAR}
</foreach>
</update>
mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记的更多相关文章
- 怎样从C#中打开数据库并进行 增 删 改 查 操作
首先 在C#中引用数据库的操作! (因为我们用的是SQLserver数据库,所以是SqlClient) using System.Data.SqlClient; 1:要实现对数据库的操作,我们必须先登 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- StringBuilder修改字符串内容,增,删,改,插
package seday01;/** * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作的性能.因此String不适合频繁修改内容. * 若有频繁修改操作,使用StringBuilder来完 ...
随机推荐
- AOJ/数据结构习题集
ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...
- 人生苦短,我用Python
Life is short, You need Python. 工作中常常要用到脚本来完成许多重复性的工作,刚开始是查数据库的时候,也曾用shell 来写脚本,但终于还是觉得shell太艰涩, 一行命 ...
- [ABP实战开源项目]--YoYoCms目录
ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WE ...
- makefile文件模板介绍
1 src : = $(shell ls *.c)2 objs : = $(patsubst %.c, %.o, $(src))3 test : $(objs)4 ...
- java中一个重要思想:面向对象
面向对象: 1, 面向过程的思想(合适的方法出现在合适的类里面) 准备去一个地方: 先买车, 挂牌, 开导航, 踩油门, 过黄河, 穿越珠穆朗玛峰... 2, 面向对象的思想 我开着车去, 车怎么去随 ...
- 分享一个完整的Mybatis分页解决方案
Mybatis 的物理分页是应用中的一个难点,特别是配合检索和排序功能叠加时更是如此. 我在最近的项目中开发了这个通用分页器,过程中参考了站内不少好文章,阅读源码帮助更大minglisoft.cn/t ...
- PreparedStatement/Statement处理insert update等操作时乱码,以及URL
原文: 在顶目中无意中碰到PreparedStatement 在存DB时出现乱码,困扰了好久终于解决问题 问题代码如下 ps = con.prepareStatement(INSERT_SQL); p ...
- 用node编写自己的cli工具
工作中接到新项目,开发前都需要先规划项目目录,然后一个个创建文件,搭建sass编译环境,下载jquery,Swiper等类库... 这些准备工作都要花上不少时间.每做一个项目,都会遇到同样的问题,再重 ...
- Bash Scripting Learn Notes
References: [1] http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ 1. Executing programs from a scri ...
- java虚拟机学习-JVM调优总结-典型配置举例(10)
以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理 ...