网站首页 > 技术文章 正文
作者 | 这个名字想了很久~
来源 | urlify.cn/6RnyAz
MyBatis中批量插入
方法一:
<insert id="insertbatch" parameterType="java.util.List">
<selectKey keyProperty="fetchTime" order="BEFORE"
resultType="java.lang.String">
SELECT CURRENT_TIMESTAMP()
</selectKey>
insert into kangaiduoyaodian ( depart1, depart2, product_name,
generic_name, img, product_specification, unit,
approval_certificate, manufacturer, marketPrice, vipPrice,
website, fetch_time, productdesc ) values
<foreach collection="list" item="item" index="index"
separator=",">
( #{item.depart1}, #{item.depart2}, #{item.productName},
#{item.genericName}, #{item.img},
#{item.productSpecification}, #{item.unit},
#{item.approvalCertificate}, #{item.manufacturer},
#{item.marketprice}, #{item.vipprice}, #{item.website},
#{fetchTime}, #{item.productdesc} )
</foreach>
</insert>
方法二:
<insert id="batchInsertB2B" parameterType="ArrayList">
insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.hkgs,jdbcType=VARCHAR},
#{item.hkgsjsda,jdbcType=VARCHAR},
#{item.office,jdbcType=VARCHAR},
#{item.asdf,jdbcType=VARCHAR},
#{item.ddd,jdbcType=VARCHAR},
#{item.ffff,jdbcType=VARCHAR},
#{item.supfullName,jdbcType=VARCHAR},0,0,
#{item.remark,jdbcType=VARCHAR} from dual
</foreach>
</insert>
可以考虑用union all来实现批量插入。
例如:
insert into XX_TABLE(XX,XX,XX)select 'xx','xx','xx' union all select 'xx','xx','xx' union all select 'xx','xx','xx' ...
先拼装好语句再动态传入insert into XX_TABLE(XX,XX,XX)后面部分
MyBatis中批量删除
<!-- 通过主键集合批量删除记录 -->
<delete id="batchRemoveUserByPks" parameterType="java.util.List">
DELETE FROM LD_USER WHERE ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
MyBatis中in子句
mybatis in 参数 使用方法
1.只有一个参数
参数的类型要声明为List或Array
Sql配置如下:
<select id="selectProduct" resultMap="Map">
SELECT *
FROM PRODUCT
WHERE PRODUCTNO IN
<foreach item="productNo" index="index" collection="参数的类型List或array">
#{productNo}
</foreach>
</select>
2.多个参数
首先要将多个参数写入同一个map,将map作为一个参数传入mapper
Sql配置如下:
<select id="selectProduct" resultMap="Map">
SELECT *
FROM PRODUCT
WHERE PRODUCTNO IN
<foreach item="productNo" index="index" collection="map中集合参数的名称">
#{productNo}
</foreach>
</select>
MyBatis批量修改
<update id="updateOrders" parameterType="java.util.List">
update orders set state = '0' where no in
<foreach collection="list" item="nos" open="(" separator="," close=")">
#{nos}
</foreach>
</update>
MyBatis的关于批量数据操作的体会
- MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。
MyBatis所说是轻量级的ORM框架,在网上看过一个测试报告,感觉相比于Hibernate来说,优势并不明显。
下面说一下比较有趣的现象,根据MyBatis的官方文档,在获得sqlSession时,它有为批量更新而专门准备的:
session = sessionFactory.openSession();//用于普通update
session = sessionFactory.openSession(ExecutorType.BATCH, true);//用于批量update
一般来说,对MYSQL数据库批量操作时速度取决于,是为每一个处理分别建立一个连接,还是为这一批处理一共建立一个连接。按MyBatis的手册说明,选择ExecutorType.BATCH意味着,获得的sqlSession会批量执行所有更新语句。不过我测试了一下,批量插入1000条数据,发觉ExecutorType.BATCH方式的效率居然比普通的方式差很多。我测试用的Mapper中的insert配置如下,再用for循环插入1000条记录:
1 <insert id="insert" parameterType="sdc.mybatis.test.Student">
2 <!-- WARNING - @mbggenerated This element is automatically generated by
3 MyBatis Generator, do not modify. This element was generated on Mon May 09
4 11:09:37 CST 2011. -->
5 insert into student (id, name, sex,
6 address, telephone, t_id
7 )
8 values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
9 #{sex,jdbcType=VARCHAR},
10 #{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
11 )
12 </insert>
1 <insert id="insert" parameterType="sdc.mybatis.test.Student">
2 <!-- WARNING - @mbggenerated This element is automatically generated by
3 MyBatis Generator, do not modify. This element was generated on Mon May 09
4 11:09:37 CST 2011. -->
5 insert into student (id, name, sex,
6 address, telephone, t_id
7 )
8 values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
9 #{sex,jdbcType=VARCHAR},
10 #{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
11 )
12 </insert>
猜你喜欢
- 2024-10-26 MybatisPlus —注解汇总(mybatis中注解)
- 2024-10-26 MyBatis使用需谨慎,看看这里有没有你曾踩到过的坑
- 2024-10-26 最新版本的MyBatis Plus 代码生成器使用指南
- 2024-10-26 解决mybatis动态生成sql错误的问题
- 2024-10-26 基于 MyBatis 的动态 SQL 技术详解
- 2024-10-26 mybatis插入获取主键的方式和原理
- 2024-10-26 Mybatis执行多条语句/批量更新方式
- 2024-10-26 MyBatis-Plus扫盲啦(mybatis plus vo)
- 2024-10-26 一文带你搞定mybatis的映射配置文件
- 2024-10-26 什么是mybatis-plus,你没用过吧,我刚学的,...
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 505℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 484℃MySQL service启动脚本浅析(r12笔记第59天)
- 465℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 462℃启用MySQL查询缓存(mysql8.0查询缓存)
- 442℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 422℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 418℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)