mybais-foreach遍历的使用

在类似于select * from user where id in(1,2,3…);这种包含不确定参数条件时,需要用到foreach语句来处理,用法示例如下:

mapper:

public interface UserMapper {
    //查找多个id的用户数据
    public List<User> findUserByIds(UserQueryVO vo);
}

xml配置:

<?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.gyf.mapper.UserMapper">
 <!-- 4.foreac使用讲解-->
    <select id="findUserByIds" parameterType="userQueryVO" resultType="user">
        <!--性别和名字 SELECT * FROM user WHERE id in (1,2,3) -->
       SELECT * FROM user
       <where>
           <if test="ids != null and ids.size > 0">
               <!--
               collection:集合,写集合属性
               item:遍历接收变量
               open:遍历开始
               close:遍历结束
               separator:拼接格式
               for(Integer id : ids){
               }
               -->
                <foreach collection="ids" item="id" open="id in(" close=")" separator=",">
                    ${id}
                </foreach>
            </if>
        </where>
    </select>
 </mapper>

测试类:

public class Demo01 {
    SqlSession session;
    @Before
    public void before() throws IOException {
        System.out.println("before....获取session");
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过SqlSessionFactory创建SqlSession
        session = sessionFactory.openSession();
    }

    @After
    public void after(){
        session.close();
    }
   
    @Test
    public void test4() throws IOException {
        UserMapper userMapper = session.getMapper(UserMapper.class);
        UserQueryVO query = new UserQueryVO();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(10);
        ids.add(16);
        query.setIds(ids);
        List<User> users = userMapper.findUserByIds(query);
        System.out.println(users);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashMap的几种遍历方式如下: 1. Entry遍历使用entrySet()方法可以同时遍历Map里面的Key和Value。可以通过迭代器或者foreach循环来实现。例如: ```java HashMap<String, Integer> map = new HashMap<>(); // 添加键值对 map.put("A", 1); map.put("B", 2); map.put("C", 3); // 使用entrySet()方法遍历 for (Map.Entry<String, Integer> entry : map.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); // 处理key和value } ``` 2. keySet遍历使用keySet()方法可以遍历Map里面的Key。可以通过迭代器或者foreach循环来实现。例如: ```java HashMap<String, Integer> map = new HashMap<>(); // 添加键值对 map.put("A", 1); map.put("B", 2); map.put("C", 3); // 使用keySet()方法遍历 for (String key : map.keySet()) { Integer value = map.get(key); // 处理key和value } ``` 3. foreach遍历:在JDK8及以上版本中,可以直接使用foreach循环来遍历HashMap的键值对。例如: ```java HashMap<String, Integer> map = new HashMap<>(); // 添加键值对 map.put("A", 1); map.put("B", 2); map.put("C", 3); // 使用foreach遍历 map.forEach((key, value) -> { // 处理key和value }); ``` 4. keySet foreach遍历:在JDK8及以上版本中,可以使用keySet()方法获取键的集合,然后使用foreach循环遍历。例如: ```java HashMap<String, Integer> map = new HashMap<>(); // 添加键值对 map.put("A", 1); map.put("B", 2); map.put("C", 3); // 使用keySet foreach遍历 for (String key : map.keySet()) { Integer value = map.get(key); // 处理key和value } ``` 以上是HashMap的几种常见遍历方式。根据具体的需求,可以选择适合的遍历方式来操作HashMap的键值对。 #### 引用[.reference_title] - *1* *3* [Java - 关于HashMap通过keySet遍历kv的二次调用问题](https://blog.csdn.net/Zong_0915/article/details/120905738)[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.239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [HashMap的七种遍历方式](https://blog.csdn.net/maojian_ohhey/article/details/115431835)[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.239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值