MySQL进行 批量插入,批量删除,批量更新,批量查询
1、批量插入
ServiceImpl层
1 2 | List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象 personMapper.insetPeopleReturnIds(addPeople); |
Dao层接口(这里的注解param中的list对应xml中的 collection的值, 两者要保持一致! )
1 | 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层
1 2 | List<String> list; //list中作者存放的是字符串,格式["123","456"] uploadListMapper.deleteByPrimaryUUid(list); |
Dao层接口
1 | 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层
1 2 | List<Person> oldPeople = new ArrayList<>(); //oldPeople存放多个person对象 personMapper.updateBatch(oldPeople); |
Dao层接口
1 | 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层
1 | List<String> list; //list中作者存放的是字符串,格式["123","456"]List<UploadList> uploadLists = uploadListMapper.selectByPrimaryUUid(list); |
Dao层接口
1 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)