首页 > 其他 > 详细

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

时间:2017-09-29 14:35:16      阅读:763      评论:0      收藏:0      [点我收藏+]

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参数,map中包含list(输入参数)

原文:http://www.cnblogs.com/qlqwjy/p/7610723.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!