mybatis傳入map參數,map中包含list(輸入參數)

1.xml中配置:java

<!-- 根據條件查詢知足條件的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測試:sql

    // 測試查詢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());
    }

 

結果:安全

 

總結:app

  map中的list同普通的同樣,只是在遍歷的時候collection要寫出map中的List的鍵值。如:測試

 

 

補充:也能夠獲取到遍歷集合的順序,好比:spa

    <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傳入的參數以下:code

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以下:orm

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 ? 
相關文章
相關標籤/搜索