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来完 ...
随机推荐
- Python 三级菜单与优化(一层循环嵌套)
优化的思路是使用单层循环嵌套完成三级菜单,这个优化思路我非常喜欢,我喜欢在编程的时候用最少的东西写出同样的效果,通常这样会绕来绕去,但非常有趣!!! 需求: 1.运行程序输出第一级菜单: 2.选择一级 ...
- 读书笔记 effective c++ Item 53 关注编译器发出的警告
许多程序员常常忽略编译器发出的警告.毕竟,如果问题很严重,它才将会变成一个error,不是么?相对来说,这个想法可能在其它语言是无害的,但是在C++中,我敢打赌编译器的实现者对于对接下来会发生什么比你 ...
- 深入浅出学习HTTP协议
之前学习javaWeb只是大致了解了一下,今天重点介绍下http请求,当是复习吧! 一.http基础概念 1.什么是http协议? HTTP是Hyper Text Transfer Protocol( ...
- MySQL 的Coalesce函数
今天用到了coalesce 函数,原因在于,我想要查找合同到期日的字段是否有值(因为合同到期日分3个字段,对应着不同的日期) select coalesce(contract_date1,contra ...
- Composer 中国全量镜像(二)
一.查看当前镜像地址 在命令行输入如下命令,即可查看镜像地址: $ composer config -g repo.packagist {"type":"composer ...
- Play学习 - 体验网页模板
在经过无数个尝试后,最终用sbt把play所依赖的所有包都下载下来了,现在可以非常快速编译运行了.今天体验了下网页模板,觉得非常不错,在这里做个简单的介绍. 原文说明: A Play Scala te ...
- windows下编译java源文件的编码错误
import java.util.Arrays;public class ArrayAsAReference{ public static void main(String[] args) { int ...
- 结构体的vector resize()与初始化
序: 我们在使用vector的时候可以自定义里面的数据类型.例如这样: struct Edge{ int from; int to; int weight; }; vector<Edge> ...
- JDK安装、java环境配置
JDK是Java语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. JRE(Java ...
- 学习笔记:javascript 表单对象(form)
Form 对象属性 属性 描述 acceptCharset 服务器可接受的字符集. action 设置或返回表单的 action 属性. enctype 设置或返回表单用来编码内容的 MIME 类型. ...