Mybatis之foreach标签的使用
本帖主要分享mybatis的批量操作,由于工作繁忙暂不对标签进行讲解
本人不推荐在for循环中进行数据库操作,因为会进行大量的io操作,导致查询性能极差,并对数据库造成压力,
1.根据id进行批量查询
实现类层
@Overridepublic List<User> selectUserByIds() {Integer[] ids = {1, 4};return userMapper.selectUserByIds(ids);}
mapper接口
/*** 批量查询* @param ids 用户信息* @return 用户信息列表*/List<User> selectUserByIds(Integer[] ids);
xml文件
<select id="selectUserByIds" resultType="com.jt.pojo.User">SELECT id, username, password, phone, email, status, created, updatedFROMuserwhereidin<foreach collection="array" item="ids" open="(" close=")" separator=",">(#{ids})</foreach></select>
简化写法
<select id="selectUserByIds1" resultType="com.jt.pojo.User">SELECT id, username, password, phone, email, status, created, updatedFROMuserwhereidin(<foreach collection="array" item="ids" separator=",">#{ids}</foreach>)</select>
2.根据多个条件进行查询
实现类层
@Overridepublic List<User> selectUserByList() {List<User> users = new ArrayList<>();User user = new User();user.setUsername("admin").setStatus(true);User user1 = new User();user1.setUsername("admin666").setStatus(true);users.add(user);users.add(user1);return userMapper.selectByUserList(users);}
mapper接口
/*** 批量查询* @param users 用户信息* @return 用户信息列表*/List<User> selectByUserList(List<User> users);
xml文件
<select id="selectByUserList" resultType="com.jt.pojo.User">SELECT id, username, password, phone, email, status, created, updatedFROMuserwhere<foreach collection="list" item="users" open="(" close=")" separator=" or ">(username = #{users.username}andstatus = #{users.status})</foreach></select>
3.批量新增
实现类层
@Overridepublic List<User> insertByUserList() {List<User> users = new ArrayList<>();User user = new User(); user.setId(7).setUsername("admin852").setStatus(true).setPassword("conversation").setPhone("10010").setEmail("1600973491@qq.com").setCreated(new Date()).setUpdated(new Date());User user1 = new User(); user1.setId(6).setUsername("admin5476").setStatus(true).setPassword("admin5476").setPhone("10010").setEmail("1205498999@qq.com").setCreated(new Date()).setUpdated(new Date());users.add(user);users.add(user1);//以下代码为查看插入后结果int i = userMapper.insertByUserList(users);Integer[] ids = {6,7};return userMapper.selectUserByIds(ids);}
mapper接口
/*** 批量新增* @param users 用户信息* @return 用户信息列表*/int insertByUserList(List<User> users);
xml文件
<insert id="insertByUserList">INSERT INTOuser(id,password,username,phone,email,status,created,updated)VALUES<foreach collection="list" item="users" index="index" separator=",">(#{users.id},#{users.password},#{users.username},#{users.phone},#{users.email},#{users.status},#{users.created},#{users.updated})</foreach></insert>
4.根据多个条件进行批量修改
实现类层
@Overridepublic List<User> updateByUserList() {List<User> users = new ArrayList<>();User user = new User();user.setId(7).setUsername("admin852").setStatus(false).setPassword("conv").setPhone("112").setEmail("1600973491@qq.com").setCreated(new Date()).setUpdated(new Date());User user1 = new User();user1.setId(6).setUsername("admin5476").setStatus(false).setPassword("ad76").setPhone("156").setEmail("1205498999@qq.com").setCreated(new Date()).setUpdated(new Date());users.add(user);users.add(user1);//以下代码为查看修改后结果int i = userMapper.updateByUserList(users);Integer[] ids = {6,7};return userMapper.selectUserByIds(ids);}
mapper接口
/*** 批量修改* @param users 用户信息* @return 用户信息列表*/int updateByUserList(List<User> users);
xml文件
<update id="updateByUserList"><foreach collection="list" item="users" index="index" separator=";">UPDATE usersetid=#{users.id},password=#{users.password},username=#{users.username},phone=#{users.phone},email=#{users.email},status=#{users.status},created=#{users.created},updated=#{users.updated}whereid=#{users.id}andusername=#{users.username}</foreach></update>
如需要根据id进行修改 则参考根据id批量查询 二者如出一辙
Mybatis之foreach标签的使用相关推荐
- Mybatis之foreach标签
Mybatis之foreach标签 案例:通过foreach标签实现如下sql查询,并在测试类中传入参数: select * from mybatis.blog where id in=(1 or 2 ...
- MyBatis学习——foreach标签的使用
一.foreach标签属性解读 MyBatis的foreach标签应用于多参数的交互如:多参数(相同参数)查询.循环插入数据等,foreach标签包含collection.item.open.clos ...
- mybatis中foreach标签详解
转载自:https://blog.csdn.net/gwd1154978352/article/details/75408498 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一 ...
- 【Mybatis】foreach标签在mybatis中的使用
mapper.xml如下: <select id="selectCkspcb" parameterType="java.util.Map"resultTy ...
- Mybatis 的foreach标签 1
foreach一共有三种类型,分别为List,[](array),Map三种. foreach的第一篇用来将List和数组(array). 下面表格是我总结的各个属性的用途和注意点. foreach属 ...
- Mybatis系列:解决foreach标签内list为空的问题
我把之前发布在简书的两篇文章通过拦截器Interceptor优化Mybatis的in查询 和Mybatis中foreach标签内list为空的解决方案进行了整合,整理为本文的内容.此外,我还对代码部分 ...
- 【MyBatis框架】mapper配置文件-foreach标签
foreach标签 下面介绍一下一个mapper配置文件中的foreach标签(注意,要跟着前面的总结来看,这里使用的例子是结合前面的工程写的,大部分代码没有再赘述) foreach的作用是向sql传 ...
- mybatis where、set、trim、sql、foreach标签的使用
mybatis where标签的使用 where后面跟查询条件 简化sql语句中判断条件的书写 例: <select id="user" parameterType=&quo ...
- MyBatis foreach 标签常用方法总结
一.前言 在 MyBatis 中,常常会遇到集合类型的参数,虽然我们可以通过 OGNL 表达式来访问集合的某一个元素,但是 OGNL 表达式无法遍历集合.foreach 标签就是专门用来解决这类问 ...
最新文章
- Linux那些事儿 之 戏说USB(12)接口是设备的接口(一)
- python xmxl 无法启动_问题引发由于与GI相关的python脚本中的错误,Gnome终端无法启动...
- 项目管理的五个核心风险区
- macOS 10.13 安装Virtualbox失败
- python自动抠头像图_Python实现AI自动抠图实例解析
- Python-21-socket编程
- Overview of ISA and TMG Networking and ISA Networking Case Study (Part 2)
- easyui 添加下拉框数据_功能更新:熟用仪表盘这个功能,你可以少建90%的数据报表...
- matlab2c使用c++实现matlab函数系列教程-conv函数
- 数字图像相关基础知识
- 计算机上没有系统软件应用软件也一样能使用,2010判断题一般双击桌面上的程序图标可以打开该程序...
- a标签href不跳转_HTML常用标签
- leetCode----day01---- 从排序数组中删除重复项
- mysql 如何抓慢查询_如何进行 MySQL慢查询 操作
- JS base64 加密和 后台 base64解密(防止中文乱码)
- linux hostid 12位,linux下修改hostid
- MSDN我告诉你 一个没有人注意的工具站
- 【机器视觉】二维码检测(QR 码)
- 2021-12-17
- 别人学到失眠,而你看书就犯困?这样提神让你赶走疲惫!
热门文章
- 【51单片机】0.96寸OLED取模教程(图片、汉字)+ 代码
- 使用java程序下载远程zip文件并解压文件( 带注释解释代码)
- Magic ship
- 人活着是为了什么(转载)
- matlab 画海面图,波光粼粼的海面画法!轻松解决你不知道如何绘画海面水纹的问题...
- [XR]人脸捕捉(Unity + iPhone)快速简易版
- Eclipse小游戏-俄罗斯方块
- 旺店通·企业版与畅捷通T+对接集成查询销售出库单连通创建销售出库单(WT+销售出库单)
- office中正则表达式、定位对象、导航栏、拆分单元格、通过关键字筛选、替换(文档格式、内容等)
- 计算机基础知识win10,计算机应用基础Windows XP及win10试题