第四章 mybatis批量insert
批量插入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的更多相关文章
- Mybatis批量insert 返回主键值和foreach标签详解
Mybatis批量insert 返回主键 Mybatis从3.3.1版本开始,支持批量插入后返回主键ID.首先对于支持自增主键的数据库使用useGenerateKeys和keyProperty,对于不 ...
- Mybatis批量insert报错的解决办法【the right syntax to use near '' at line...】
Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下: <insert id="batchSave"> into t_emp(emp_name, ...
- oracle数据库,mybatis批量insert,缺失values字段
报错:### Error updating database. Cause: java.sql.SQLException: ORA-00926: 缺失 VALUES 关键字### The error ...
- Mybatis 批量insert
@Override public int insertHouseTypeScene(int htid, String name, String icon,int sort, List<House ...
- 第五章 mybatis批量更新update
一.所有的指定id的模型类的同一个字段进行批量更新 实际上: update t set fileld='xx' where id in (id1,id2,...,idn) 代码: <update ...
- mybatis 批量insert,update报错 The error occurred while setting parameters
数据脚本执行正常,但是报错,搜索关键信息 The error occurred while setting parameters ,发现了解决帖子: http://blog.csdn.net/jing ...
- springboot-mybatis 批量insert
springboot mybatis 批量insert 操作 直接上代码: 1.首先要在pom.xml中导入包: 略...... 2.springboot mybatis配置: package com ...
- MyBatis :Insert (返回主键、批量插入)
一.前言 数据库操作怎能少了INSERT操作呢?下面记录MyBatis关于INSERT操作的笔记,以便日后查阅. 二.insert元素 属性详解 其属性如下: parameterType , ...
- MyBatis 3(中文版) 第四章 使用注解配置SQL映射器
本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l ...
随机推荐
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
- Python内存管理方式和垃圾回收算法解析
在列表,元组,实例,类,字典和函数中存在循环引用问题.有 __del__ 方法的实例会以健全的方式被处理.给新类型添加GC支持是很容易的.支持GC的Python与常规的Python是二进制兼容的. 分 ...
- django使用admin
1.在应用下的admin.py添加 #!/usr/bin/python # coding:utf-8 from django.contrib import admin from .models imp ...
- luoguP4503 [CTSC2014]企鹅QQ hash
既然只有一位的不同,那么我们可以枚举这一位.... 我们只需要快速地计算去掉某一位的$hash$值.... 由于$hash(S) = \sum s[i] * seed^i$,因此去掉第$i$位的权值只 ...
- UC浏览器 垂直水平居中
今天使用下述方式定义水平垂直居中不起作用 #box{ position: fixed; left:; right:; top:; bottom:; margin: auto; } 然后改用: #box ...
- Codeforces Round #358 (Div. 2) D. Alyona and Strings dp
D. Alyona and Strings 题目连接: http://www.codeforces.com/contest/682/problem/D Description After return ...
- Codeforces Round #296 (Div. 1) B. Clique Problem 贪心
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ASP.NET与MVC架构区别总结
1)ASP.NET Webforms Behind Code利于快速开发,方便可视化操作. 2)ASP.NET 使用了“基于视图”的解决方案去应对“基于行为”的需求,它处理了客户端的请求,IIS将请求 ...
- 通过adb把apk安装到系统分区
通过adb把apk安装到系统分区 以谷歌拼音为例:GooglePinyin1.4.2.apk提取出so文件libjni_googlepinyinime_4.solibjni_googlepinyini ...
- Short Circuit Protection Circuit
http://www.daycounter.com/Circuits/Short-Circuit-Protection/Short-Circuit-Protection.phtml Short cir ...