1、批量插入

ServiceImpl层

List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象
personMapper.insetPeopleReturnIds(addPeople);

Dao层接口(这里的注解param中的list对应xml中的 collection的值, 两者要保持一致! )

int insetPeopleReturnIds(@Param("list") List<Person> addPeople);

Mapper.xml

(keyColumn是数据库的字段,keyProperty对应的是实体类的属性,为的是让ID自增长)

<insert id="insetPeopleReturnIds" keyColumn="person_id" keyProperty="personId" parameterType="java.util.List"
useGeneratedKeys="true">
insert into person (person_name, id_type,
id_num, phone,org_id)
values
<foreach collection="list" index="index" item="item" separator=",">
(#{item.personName,jdbcType=VARCHAR}, #{item.idType,jdbcType=INTEGER},
#{item.idNum,jdbcType=VARCHAR}, #{item.phone,jdbcType=VARCHAR},#{item.orgId,jdbcType=INTEGER})
</foreach>
</insert>

2、批量删除

ServiceImpl层

List<String> list; //list中作者存放的是字符串,格式["123","456"]
uploadListMapper.deleteByPrimaryUUid(list);

Dao层接口

int deleteByPrimaryUUid(@Param("lists") List<String> list);

Mapper.xml

<delete id="deleteByPrimaryUUid" parameterType="java.util.List">
delete from upload_list
where uuid in
<foreach close=")" collection="lists" index="index" item="item" open="(" separator=",">
#{item,jdbcType=VARCHAR}
</foreach>
</delete>

3、批量更新

ServiceImpl层

List<Person> oldPeople = new ArrayList<>();//oldPeople存放多个person对象
personMapper.updateBatch(oldPeople);

Dao层接口

int updateBatch(@Param("list") List<Person> list);

Mapper.xml

 <update id="updateBatch" parameterType="java.util.List">
update person
<trim prefix="set" suffixOverrides=",">
<trim prefix="person_name =case" suffix="end,">
<foreach collection="list" index="index" item="item">
when person_id = #{item.personId} then #{item.personName}
</foreach>
</trim>
<trim prefix="id_type =case" suffix="end,">
<foreach collection="list" index="index" item="item">
when person_id = #{item.personId} then #{item.idType}
</foreach>
</trim>
<trim prefix="id_num =case" suffix="end,">
<foreach collection="list" index="index" item="item">
when person_id = #{item.personId} then #{item.idNum}
</foreach>
</trim>
<trim prefix="phone =case" suffix="end,">
<foreach collection="list" index="index" item="item">
when person_id = #{item.personId} then #{item.phone}
</foreach>
</trim>
</trim>
where person_id in
<foreach close=")" collection="list" item="item" open="(" separator=",">
#{item.personId}
</foreach>
</update>

4、批量查询

ServiceImpl层

List<String> list; //list中作者存放的是字符串,格式["123","456"]
List<UploadList> uploadLists = uploadListMapper.selectByPrimaryUUid(list);

Dao层接口

List<UploadList> selectByPrimaryUUid(@Param("lists") List<String> list);

Mapper.xml 

 <select id="selectByPrimaryUUid" resultMap="BaseResultMap">
select *
from upload_list
where uuid in
<foreach close=")" collection="lists" index="index" item="item" open="(" separator=",">
#{item,jdbcType=VARCHAR}
</foreach>
</select>

参考文章https://www.cnblogs.com/javalanger/p/10899088.html

https://blog.csdn.net/q957967519/article/details/88669552

MySQL进行 批量插入,批量删除,批量更新,批量查询的更多相关文章

  1. mybatis + mysql 批量插入、删除、更新

    mybatis + mysql 批量插入.删除.更新 Student 表结构 批量插入 public int insertBatchStudent(List<Student> studen ...

  2. mysql DML 数据插入,删除,更新,回退

    mysql插入,删除,更新地址:https://wenku.baidu.com/view/194645eef121dd36a32d82b1.html http://www.cnblogs.com/st ...

  3. java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)

    插播一段广告哈:我之前共享了两个自己写的小应用,见这篇博客百度地图开发的两个应用源码共享(Android版),没 想到有人找我来做毕设了,年前交付,时间不是很紧,大概了解了下就接下了,主要用到的就是和 ...

  4. SqlServer设置特定用户操作特定表(插入、删除、更新、查询 的权限设置)

    目录 一.需求场景: 二.操作步骤: 表上右键选择[属性],选择[权限]选项卡: 点击[搜索],在弹出的框中点击[浏览],选择需要设置的用户: 在上面点击[确定]后,就可以在[权限]选项卡中看到权限列 ...

  5. 个人永久性免费-Excel催化剂功能第32波-空行空列批量插入和删除

    批量操作永远是效率提升的王道,也是Excel用户们最喜欢能够实现的操作虽说有些批量操作不一定合适Excel的最佳实践操作,但万千世界,无奇不有,特别是在国人眼中领导最大的等级森严的职场环境下.Exce ...

  6. JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能

    主要内容:  JDBC连接数据库步骤. 一个简单详细的查询数据的例子. 封装连接数据库,释放数据库连接方法. 实现查询,插入,删除,更新等十一个处理数据库信息的功能.(包括事务处理,批量更新等) 把十 ...

  7. arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新

    风过无痕 原文  arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新 深入理解游标Cursors,实现数据的快速查找,插入,删除,更新 1.查找数据Search Cu ...

  8. SqlBulkCopy与触发器,批量插入表(存在则更新,不存在则插入)

    临时表:Test /****** 对象: Table [dbo].[Test] 脚本日期: 05/10/2013 11:42:07 ******/ SET ANSI_NULLS ON GO SET Q ...

  9. mybatis使用foreach进行批量插入和删除操作

    一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...

  10. .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑

    在使用SqlBulkCopy类操作DataTable批量插入数据,这种操作插入数据的效率很高,就会导致每一条数据在保存的时间基本一样,在我们分页查询添加的数据是,使用数据的添加时间来排序就会出现每页的 ...

随机推荐

  1. android软件简约记账app开发day10-主页面模块--头信息的展示,和之后功能完善的目标。

    android软件简约记账app开发day10-主页面模块--头信息的展示,和之后功能完善的目标. 今天来写主界面头信息的展示,也就是将第一天的写的layout中的item_main_top展示到主界 ...

  2. java第十二周作业

    1.定义一个点类Point, 包含2个成员变量x.y分别表示x和y坐标,2个构造器Point()和Point( intx0,y0),以及一个movePoint (int dx,intdy)方法实现点的 ...

  3. input 标签的 pattern 属性

    定义和用法 pattern 属性规定用于验证输入字段的模式. 模式指的是正则表达式. 注释:pattern 属性适用于以下 <input>类型:text, search, url, tel ...

  4. [AcWing 51] 数字排列

    点击查看代码 class Solution { public: vector<vector<int>> res; vector<vector<int>> ...

  5. ZooKeeper 到底解决了什么问题?

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 目标 ZooKeeper 很流行,有个基本的疑问: Zo ...

  6. 过早的给方法中 引用对象 设为 null 可被 GC提前回收吗?

    经常在代码中看到有人将 null 赋值给引用类型,来达到让 GC 提前回收的目的,这样做真的有用吗?今天我们就来研究一下. 为了方便讲解,来一段测试代码,提前将 test1=null ,然后调用 GC ...

  7. drools中query的使用

    一.背景 我们知道在drools中是存在工作内存的,我们的Fact对象会加入到工作内存中,同时我们自己也可以在drl文件中使用insert/modify/update/delete等方法,修改工作内存 ...

  8. Redis设计与实现3.2:Sentinel

    Sentinel哨兵 这是<Redis设计与实现>系列的文章,系列导航:Redis设计与实现笔记 哨兵:监视.通知.自动故障恢复 启动与初始化 Sentinel 的本质只是一个运行在特殊模 ...

  9. 安装Python到Linux(Pyenv)

    pyenv是一个多Python版本的托管工具,我们可以使用它安装Python和随意的切换系统环境中默认使用的Python版本. 运行环境 系统版本:CentOS Linux release 7.6.1 ...

  10. Dockerfile 使用 SSH

    如果在书写 Dockerfile 时,有些命令需要使用到 SSH 连接,比如从私有仓库下载文件等,那么我们应该怎么做呢? Dockerfile 使用 SSH Dockerfile 文件配置 为了使得 ...