mybatis传入map参数,map中包含list(输入参数)

本文介绍了一个使用MyBatis动态SQL的例子,通过XML配置文件实现基于不同条件查询试题ID集合的功能。具体展示了如何利用<if>和<foreach>等标签进行条件判断和集合处理。

1.xml中配置:

<!-- 根据条件查询满足条件的ID集合开始 -->
    <select id="getQuestionsIdsForExamPaper" resultType="java.lang.String"
        parameterType="hashmap">
        select
        questionId from questions
        <where>
            <include refid="query_questionIds_where"></include>
        </where>
    </select>

    <!-- 查询试题ID的条件 -->
    <sql id="query_questionIds_where">
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="level!=null">
            and level=#{level}
        </if>
        <!-- 知识点 -->
        <if test="konwledges!=null">
            and knowledgeType in
            <foreach collection="konwledges" item="knowledge" separator=","
                open="(" close=")">
                #{knowledge}
            </foreach>
        </if>
        <if test="num!=null">
            ORDER BY RAND() LIMIT #{num}
        </if>
    </sql>

 2.Java测试:

    // 测试查询ID集合
    @Test
    public void test3() throws SQLException {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("type", "单选题");
        condition.put("level", 1);
        condition.put("num", 3);
        List<String> konwledges = new ArrayList<String>();
        konwledges.add("安全生产管理知识");
        konwledges.add("电力安全规程制度");
        condition.put("num", 3);
        condition.put("konwledges", konwledges);
        List<String> IDs = questionsCustomMapper.getQuestionsIdsForExamPaper(condition);
        System.out.println(IDs.size());
    }

 

结果:

 

总结:

  map中的list同普通的一样,只是在遍历的时候collection要写出map中的List的键值。如:

 

Mybatis可以使用Map类型作为参数传递给Mapper接口方法。通过使用Map类型作为参数,可以在SQL语句中直接使用Map的key来获取参数值。比如,在Mapper接口中定义一个使用Map类型作为参数的方法,如下所示: ```java User checkLoginByMap(Map<String, Object> map); ``` 在Mapper的XML文件中,可以通过`#{key}`来获取Map中对应key的value值,如下所示: ```xml <select id="checkLoginByMap" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND password = #{password} </select> ``` 这样,在调用Mapper接口方法时,可以将参数封装到一个Map中传递给Mapper方法,Mybatis会自动将Map中的key-value对映射到SQL语句中的参数位置,执行查询并返回结果。 请注意,使用Map作为参数传递可能会导致SQL语句的可读性降低,因为无法直观地了解参数的含义。因此,在实际开发中,应根据具体情况选择使用Map还是自定义的POJO类作为参数类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mybatis传参1 - 传入map类型的参数](https://blog.csdn.net/m0_53753920/article/details/124155904)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【MyBatis04 - MyBatis 使用Map传参](https://blog.csdn.net/nyc__/article/details/119221970)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值