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

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 konwledges.size>0">
            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的键值。如:

 

【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值