mybatis中的mapper.xml使用循环语句
作者:代码搬晕工 时间:2022-02-06 22:23:13
mapper.xml使用循环语句
mapper.java,传的参数是map
List<实体类> getList(Map<String,Object> paraMap);
mapper.xml
<!--select:对应sql的select语句, id:方法名,parameterType:参数类型,resultMap:返回对象类型(BaseResultMap:标签-->
<!--<resultMap id="BaseResultMap" type="实体类包路径"> 实体类的映射 不改的话一般都是这个名字)-->
<select id="getList" parameterType="java.util.Map" resultMap="BaseResultMap">
select * from table where
<!-- 判断-->
<if test="a!= null">
a = #{a,jdbcType=VARCHAR}
</if>
<if test="list!= null">
and id in
<!-- for循环, item:循环后的值, index:循环下标列式for循环的 i ,collection:参数名-->
<!-- open="(" close=")" separator="," 就是把循环的值组成 (item1,item2,item3)的格式-->
<foreach item="item" index="index" collection="list" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
</select>
参数,数组,list都行
Map<String,Object> map = new HashMap<String, Object>();
map.put("a","参数");
map.put("list",数组、List都行)
List<实体类> list = mapper.getList(map);
mybatis xml循环语句
MyBatis很好的支持批量插入,使用foreach即可满足
首先创建DAO方法
package com.youkeda.comment.dao;
import com.youkeda.comment.dataobject.UserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List;
@Mapper
public interface UserDAO {
int batchAdd(@Param("list") List<UserDO> userDOs);
}
<insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified)
VALUES
<foreach collection="list" item="it" index="index" separator =",">
(#{it.userName}, #{it.pwd}, #{it.nickName}, #{it.avatar},now(),now())
</foreach >
</insert>
foreach相当于执行力java的for循环,他的属性:
collection
指定集合的上下文参数名称比如这里的@Param("list")item
指定遍历的每一个数据的变量,一般叫it,可以使用it.userName来获取具体的值index
集合的索引值,从0开始separator
遍历每条记录并添加分隔符
除了批量插入,使用SQL in查询多个用户时也会使用
package com.youkeda.comment.dao;
import com.youkeda.comment.dataobject.UserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List;
@Mapper
public interface UserDAO {
List<UserDO> findByIds(@Param("ids") List<Long> ids);
}
<select id="findByIds" resultMap="userResultMap">
select * from user
<where>
id in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
open
表示的是节点开始时自定义的分隔符
close
表示是节点结束时自定义的分隔符
执行后会变成:
select * from user where id in (?,?,?)
来源:https://blog.csdn.net/qq_30059235/article/details/72473942
标签:mybatis,mapper.xml,循环语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MybatisPlus中@TableField注解的使用详解
2021-11-01 23:05:35
![](https://img.aspxhome.com/file/2023/0/82040_0s.png)
深入浅析C#泛型类型
2023-01-30 06:45:54
![](https://img.aspxhome.com/file/2023/9/111139_0s.png)
C#怎样才能将XML文件导入SQL Server
2022-02-16 17:51:13
java开发线上事故理解RocketMQ异步精髓
2023-07-25 07:41:20
![](https://img.aspxhome.com/file/2023/1/57501_0s.jpg)
JAVA的LIST接口的REMOVE重载方法调用原理解析
2021-07-20 16:35:27
解析Spring Mvc Long类型精度丢失问题
2021-11-06 12:27:37
![](https://img.aspxhome.com/file/2023/5/61275_0s.png)
Ubuntu搭建Java开发环境笔记
2023-10-10 14:27:49
idea中如何去掉不想commit的文件
2021-11-09 15:51:30
![](https://img.aspxhome.com/file/2023/0/113480_0s.png)
Android实现使用微信登录第三方APP的方法
2021-06-09 00:05:02
Java基础学习之字符串知识总结
2021-06-30 10:23:56
![](https://img.aspxhome.com/file/2023/9/84209_0s.png)
redisson分布式限流RRateLimiter源码解析
2021-05-29 13:10:15
![](https://img.aspxhome.com/file/2023/4/110924_0s.jpg)
springBoot Junit测试用例出现@Autowired不生效的解决
2023-01-24 12:57:59
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
Java 详解Collection集合之ArrayList和HashSet
2021-09-10 06:27:02
![](https://img.aspxhome.com/file/2023/6/113516_0s.png)
Mybatis结果集自动映射的实例代码
2023-07-09 02:13:58
springboot如何去除debug日志
2023-02-14 08:43:37
![](https://img.aspxhome.com/file/2023/9/92679_0s.png)
java 命名空间 命名规则第1/2页
2021-06-06 14:20:19
通过Java实现bash命令过程解析
2023-01-07 17:38:25
C语言实现贪吃蛇小游戏开发
2022-08-20 00:03:07
![](https://img.aspxhome.com/file/2023/7/120827_0s.jpg)
详解WPF中的对象资源
2023-10-28 14:09:41
![](https://img.aspxhome.com/file/2023/8/100118_0s.png)
Android自定义Drawable实现圆形和圆角
2022-05-07 14:59:59