批量插入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. 使用Generator(小黑鸟)反向生成Java项目(IDEA + Maven)

    一.生成Maven项目 二.配置pom.xml文件 通用代码 <properties> <!-- 设置项目编码编码 --> <project.build.sourceEn ...

  2. maven的统一版本管理实践

    为什么要使用maven的统一版本管理? 在进行项目开发的时候,我们使用maven来做项目的构建和管理.为了方便项目中各个模块之间的复用,项目通常会有多个模块构成.不同的模块,会各自应用自己需要的jar ...

  3. springboot+thymeleaf 模板中传递参数误报错误 红色波浪线

    在使用IDEA开发SpringBoot项目时,使用了Thymeleaf模板引擎,在使用动态传参数时,HTML页面的动态参数出现了红色波浪线,情况如下如: 解决办法: 选择 File -> Set ...

  4. 【assembly】用汇编写的一个BMP图片读取器

    ;----------------------------- ;文件满足256色调的 ;----------------------------- Stack    Segment           ...

  5. ZOJ 3235 Prototype

    Prototype Time Limit: 1 Second      Memory Limit: 32768 KB Prototype is a 3D game which allow you to ...

  6. Python学习笔记(七)—字典的学习

    总结内容: 1.字典的定义 2.字典的好处 3.字典的增删改查 4.字典常用方法及内置函数 5.字典的多层嵌套 6.字典的循环 7.字典小练习 1.字典的定义 字典是另一种可变容器模型,且可存储任意类 ...

  7. 图解PCB布线数字地、模拟地、电源地,单点接地抗干扰!

    我们在进行pcb布线时总会面临一块板上有两种.三种地的情况,傻瓜式的做法当然是不管三七二十一,只要是地 就整块敷铜了.这种对于低速板或者对干扰不敏感的板子来讲还是没问题的,否则可能导致板子就没法正常工 ...

  8. Syncovery : Google Docs protocol completely replaced with Google Drive

    Google Docs protocol completely replaced with Google Drive In May 2015, the older Google Docs API wa ...

  9. 5V and 3V Level Translators

    http://www.daycounter.com/Circuits/Level-Translators/Level-Translators.phtml Interfacing 5V and 3V l ...

  10. DM368 UBL和u-boot的裁剪

    转载:http://blog.csdn.net/olei_oleitao/article/details/7919307   一.DM36X的BOOT过程介绍 DM36x的BOOT过程和DM6446. ...