批量插入sql语句:

INSERT INTO table (field1,field2,field3) VALUES ('a',"b","c"), ('a',"b","c"),('a',"b","c")

mybatis通过foreach循环拼装了如上的sql语句。

一、xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper
namespace="com.xxx.mapper.XXXRecordMapper">
<resultMap id="BaseResultMap" type="com.xxx.model.XXXRecord">
<id column="AutoId" property="autoid" jdbcType="BIGINT" />
<result column="UserId" property="userid" jdbcType="BIGINT" />
<result column="NoticedTime" property="noticedtime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List">
AutoId, UserId, NoticedTime
</sql>
<!-- myself:批量插入 -->
<insert id="insertBatch" parameterType="java.util.List">
insert into T_XXXRecord (AutoId, UserId, NoticedTime) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.autoid,jdbcType=BIGINT},
#{item.userid,jdbcType=BIGINT},
#{item.noticedtime,jdbcType=TIMESTAMP})
</foreach>
</insert>
</mapper>

说明:

  • mysql批量插入的限制是一次批量:1M
  • 我这里插入的List,如上就好,如果是其他结构,查看这篇博客:http://www.cnblogs.com/admol/articles/4248159.html
  • collection属性:

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

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

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

第四章 mybatis批量insert的更多相关文章

  1. Mybatis批量insert 返回主键值和foreach标签详解

    Mybatis批量insert 返回主键 Mybatis从3.3.1版本开始,支持批量插入后返回主键ID.首先对于支持自增主键的数据库使用useGenerateKeys和keyProperty,对于不 ...

  2. Mybatis批量insert报错的解决办法【the right syntax to use near '' at line...】

    Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下: <insert id="batchSave"> into t_emp(emp_name, ...

  3. oracle数据库,mybatis批量insert,缺失values字段

    报错:### Error updating database.  Cause: java.sql.SQLException: ORA-00926: 缺失 VALUES 关键字### The error ...

  4. Mybatis 批量insert

    @Override public int insertHouseTypeScene(int htid, String name, String icon,int sort, List<House ...

  5. 第五章 mybatis批量更新update

    一.所有的指定id的模型类的同一个字段进行批量更新 实际上: update t set fileld='xx' where id in (id1,id2,...,idn) 代码: <update ...

  6. mybatis 批量insert,update报错 The error occurred while setting parameters

    数据脚本执行正常,但是报错,搜索关键信息 The error occurred while setting parameters ,发现了解决帖子: http://blog.csdn.net/jing ...

  7. springboot-mybatis 批量insert

    springboot mybatis 批量insert 操作 直接上代码: 1.首先要在pom.xml中导入包: 略...... 2.springboot mybatis配置: package com ...

  8. MyBatis :Insert (返回主键、批量插入)

    一.前言    数据库操作怎能少了INSERT操作呢?下面记录MyBatis关于INSERT操作的笔记,以便日后查阅. 二.insert元素 属性详解   其属性如下: parameterType , ...

  9. MyBatis 3(中文版) 第四章 使用注解配置SQL映射器

    本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l ...

随机推荐

  1. Wannafly挑战赛9 C - 列一列

    链接:https://www.nowcoder.com/acm/contest/71/C来源:牛客网 题目描述 小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An.尽管他计算 ...

  2. POJ - 1329 Circle Through Three Points 求圆

    Circle Through Three Points Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4112   Acce ...

  3. 《Playing hard exploration games by watching YouTube》论文解读

    论文链接 油管链接 一.摘要   当环境奖励特别稀疏的时候,强化学习方法通常很难训练(traditionally struggle).一个有效的方式是通过人类示范者(human demonstrato ...

  4. python中 .write 无法向文件写入内容

    问题代码如下 links = open("new") out = open("out.txt","w+") for link in link ...

  5. nyoj素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  6. 高并发编程之synchronized

    一.什么是线程? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...

  7. Redis在Window服务下的安装

    Redis 安装 1.首先在Windows下下载安装Redis 下载地址:https://github.com/MicrosoftArchive/redis/releases 根据你电脑系统的实际情况 ...

  8. TCP的三次握手与四次释放

    TCP的三次握手与四次释放 一.名词解释     序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上序号后,就给 ...

  9. [CodeChef-QUERY]Observing the Tree

    题目大意: 给你一棵树,一开始每个点的权值都是0,要求支持一下三种操作: 1.路径加等差数列. 2.路径求和. 3.回到以前的某次操作. 强制在线. 思路: 树链剖分+主席树. 最坏情况下,n个点的树 ...

  10. Linux重新学习

    一.由来 unix到minix到linux 1991正式发行 1.内核版本:linux核心版本 linux内核官网www.kernel.org 2.发行版本:比如redhat.centOS.suse. ...