Mybatis <foreach>遍历查询, 根据:数组、List、Map, 简洁例子

注:xxx为某某某的意思

数组

关键部分

        select * from xxxx where xxx in
        <foreach collection="array" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>

List

关键部分

        select * from xxxx where xxx in
        <foreach collection="list" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>

Map

关键部分

        select * from t_customer where jobs=#{jobs} and id in
        <foreach collection="id" index="index" item="roleMap" open="(" separator="," close=")">
            #{roleMap}
        </foreach>

对于<foreach collection="id" index="index" item="roleMap" open="(" separator="," close=")">中的内容:

open="("     以前括号开头,
separator=","     逗号分隔,
close=")"     后括号结尾。
collection="”  当为List时写list,当为数组时写
array,当为Map时写Map里面对应的键名,如:
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);

        Map<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("id",ids);
        conditionMap.put("jobs","job");

;

        <foreach collection="id" index="index" item="roleMap" open="(" separator="," close=")">
            #{roleMap}
        </foreach>
id指的是Map里的键名“id”  item指的是“id”指的叫ids的List中的元素
最终SQL语句可能是 select * from t_customer where jobs=#{jobs} and id in (1,2);

完整例子

//数组
        Integer[]  roleIds = {2,3};
        List<ccc> cccs= session.selectList("com.xxx.xxx.cccMapper.findByArray",roleIds);


//List
/*
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        List<ccc> cccs= session.selectList("com.xxx.xxx.cccMapper.findByList",ids);
*/


//Map
/*
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);

        Map<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("id",ids);
        conditionMap.put("jobs","teacher");
        List<ccc> cccs= session.selectList("com.xxx.xxx.cccMapper.findByMap",conditionMap);
*/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.xxx.cccMapper">
    <select id="findByArray" parameterType="java.util.Arrays"
            resultType="com.xxx.xxx.ccc">
<!--数组-->
        select * from xxxx where xxx in
        <foreach collection="array" index="index" item="id" open="(" separator="," close=")">


<!--List-->
<!--
        select * from xxxx where xxx in
        <foreach collection="list" index="index" item="id" open="(" separator="," close=")">
           #{id}
        </foreach>
-->


<!--Map-->
<!--
        select * from t_customer where jobs=#{jobs} and id in
        <foreach collection="id" index="index" item="roleMap" open="(" separator="," close=")">
            #{roleMap}
        </foreach>
-->

    </select>
</mapper>

在使用<foreach>标签传递List<String>参数时,可以使用以下两种方式。 第一种方式是在Mapper接口的方法参数前面添加@Param注解,并将List<String>参数命名为list。例如: 引用\[1\]中的示例代码: ```java public List<Map<String, Object>> dynamicForeachTest(@Param("list") List<String> ids); ``` 引用\[1\]中的示例代码: ```xml <select id="dynamicForeachTest" resultType="java.util.Map"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 第二种方式是直接将List<String>参数命名为ids,不需要添加@Param注解。例如: 引用\[2\]中的示例代码: ```java public List<Map<String, Object>> dynamicForeach3Test(List<String> ids, @Param("contractId") String contractId); ``` 引用\[2\]中的示例代码: ```xml <select id="dynamicForeach3Test" resultType="java.util.Map"> select * from xxx where cid=#{contractId} and fID in <foreach collection="ids" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 无论使用哪种方式,都可以通过<foreach>标签遍历List<String>参数中的每个元素,并将其作为SQL语句的参数进行查询。 #### 引用[.reference_title] - *1* *2* *3* [MyBatis中in <foreach>的使用](https://blog.csdn.net/xujunkai66/article/details/81413818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值