MyBatis之foreach的用法

MyBatis提供foreach语句实现In查询。foreach语法如下:

collection:该属性的对应方法的参数类型可以是List、数组、Map。如果方法的参数类型不属于前三种,则必须和方法参数@Param指定的元素名一致。
item: 表示迭代过程中每个元素的别名。可以随便起名,但是必须跟元素中的#{}里面的名称一致。
index:在list、array中,index为元素的序号索引。但是在Map中,index为遍历元素的key值,该参数为可选项;
open:前缀
close:后缀
separator:分隔符,表示迭代时每个元素之间以什么分隔。

String[]、List<> 类型

dao层

public interface UserInfoDao {
    
    
    // 法1:参数类型是Array
    List<UserInfoDO> getUserInfobyNames1(String[] names);
    // 法2:参数类型是List
    List<UserInfoDO> getUserInfobyNames2(List<String> names);
    // 法3:参数类型是Map,Map里面的List对象,如下
    //List<String> list = new ArrayList<>();
    //list.add("1");
    //list.add("2");
    //list.add("3");
    //Map<String, Object> map = new HashMap<>();  
    //map.put("userids", list);
    List<UserInfoDO> getUserInfobyNames3(Map<String,Object> params);
    // 法4:参数类型是Map,Map里面的Map对象,如下
    //Map<String,Object> map= new HashMap<>();
    //Map<String, Object> map2 = new HashMap<>();  
    //map2.put("map22", "123456");
    //map.put("map2", map2);
    List<UserInfoDO> getUserInfobyNames4(Map<String,Object> params);

}

xml

<!-- 法1:参数类型是Array -->
<select id="getUserInfobyNames1" resultType="com.springboottest.model.UserInfoDO">
    SELECT * FROM user_info WHERE user_name in
    <foreach collection="array" item="name" index="index" open="(" close=")" separator=",">
        #{name}
    </foreach>
</select>

<!-- 法2:参数类型是List -->
<select id="getUserInfobyNames2" resultType="com.springboottest.model.UserInfoDO">
    SELECT * FROM user_info WHERE user_name in
    <foreach collection="list" item="name" index="index" open="(" close=")" separator=",">
        #{name}
    </foreach>
</select>

<!-- 法3:参数类型是Map,循环List对象,注意这里的collection 即为 userids-->
<select id="getUserInfobyIdAndName3" resultType="com.springboottest.model.UserInfoDO">
    SELECT * FROM user_info WHERE user_info_id=#{id} and user_name in
    <foreach collection="userids" item="name" index="index" open="(" close=")" separator=",">
        #{name}
    </foreach>
</select>


<!-- 法4:参数类型是Map,循环map对象,注意这里的collection 即为 userids-->
<select id="getUserInfobyIdAndName4" resultType="com.springboottest.model.UserInfoDO">
    SELECT * FROM user_info WHERE user_info_id=#{id} and user_name in
    <foreach collection="map2.entrySet()" open="(" close=")" separator="or" index="key" item="value" >
     #{value}
   </foreach>
</select>

参考文章
【1】https://blog.csdn.net/zijikanwa/article/details/103028352?spm=1001.2014.3001.5506
【2】https://blog.csdn.net/m0_37965811/article/details/117635299?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168421847016800188593365%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=168421847016800188593365&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-117635299-null-null.142v87control_2,239v2insert_chatgpt&utm_term=mybatis%E4%B8%ADforeach%E7%94%A8%E6%B3%95&spm=1018.2226.3001.4187
【3】https://blog.csdn.net/weixin_38192427/article/details/121443045?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168421847016800188593365%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=168421847016800188593365&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-2-121443045-null-null.142v87control_2,239v2insert_chatgpt&utm_term=mybatis%E4%B8%ADforeach%E7%94%A8%E6%B3%95&spm=1018.2226.3001.4187

猜你喜欢

转载自blog.csdn.net/qq_20236937/article/details/130704480