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 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的键值。如:

 

 

补充:也可以获取到遍历集合的顺序,比如:

    <select id="listFirstChargeReport" resultType="map"
        parameterType="map">
    SELECT user_name,gmt_created, '1' AS 当天
    <foreach collection="syncDates" item="syncDate" index="indexNum" open="," separator=",">
        IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name  AND sync_Date = '${syncDate}'),'-') AS 第${indexNum + 1}天
    </foreach>
    FROM first_charge c WHERE gmt_created LIKE '${gmtCreated}%' 
    </select>

 

java传入的参数如下:

Map<String, Object> tmpCondition = new HashMap<>();
        tmpCondition.put("gmtCreated", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        List<String> syncDates = getDates(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), 15);
        tmpCondition.put("syncDates", syncDates);

private List<String> getDates(String dateStr, int i) {
        List<String> result = new ArrayList<>();

        try {
            Date parseDate = DateUtils.parseDate(dateStr, "yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parseDate);

            while (i > 0) {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                i--;

                Date time = calendar.getTime();
                String formatedTime = DateFormatUtils.format(time, "yyyy-MM-dd");
                result.add(formatedTime);
            }
        } catch (ParseException e) {
            // ignore
        }

        return result;
    }

 

生成的SQL如下:

SELECT user_name,gmt_created, '1' AS 当天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-05'),'-') AS 第1天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-04'),'-') AS 第2天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-03'),'-') AS 第3天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-02'),'-') AS 第4天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-01'),'-') AS 第5天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-31'),'-') AS 第6天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-30'),'-') AS 第7天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-29'),'-') AS 第8天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-28'),'-') AS 第9天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-27'),'-') AS 第10天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-26'),'-') AS 第11天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-25'),'-') AS 第12天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-24'),'-') AS 第13天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-23'),'-') AS 第14天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-22'),'-') AS 第15天 FROM first_charge c WHERE gmt_created LIKE '2019-09-06%' LIMIT ? 

 

posted @ 2017-09-29 13:56  QiaoZhi  阅读(47178)  评论(1编辑  收藏  举报