1. 程式人生 > >mybatis傳入map引數,map中包含list(輸入引數)

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());
    }

 3.Java測試:

private Map<String, Object> getStepStatus(String username, String loginType) {
    Map<String, Object> map = new HashMap<String, Object>();
    //有種可能,xxx既是運營人員也是合規人員,所以step/status為list
    List<String> stepList = new ArrayList<String>();
    List<String> statusList = new ArrayList<String>();
    if (Constants.LOGIN_TYPE_abc.equalsIgnoreCase(loginType)) {
        boolean isAuditor = StringUtils.isNoneBlank(simpleModelService.findBy(SimpleModelType.Auditor, username));
        boolean isOperator = StringUtils.isNoneBlank(simpleModelService.findBy(SimpleModelType.Operator, username));
        if (isOperator) {
            stepList.add("2");
            statusList.add("Submit");
            statusList.add("Reject");
        }
        if (isAuditor) {
            stepList.add("3");
            statusList.add("Pass");
        }
    } else {
        stepList.add(String.valueOf(1));
        statusList.add("Reject");
    }
    map.put("stepList", stepList);
    map.put("statusList", statusList);
    return map;
}

4. Dao

List<WorkbenchEventVo> searchMyWorkFlows(Map<String, Object> map);

5. Mapper

<sql id="searchMyWorkFlows">
       <if test="stepList!=null and stepList.size>0">
           and wf.fstep in
           <foreach collection="stepList" item="step" index="i" open="(" close=")" separator=",">
               #{step}
           </foreach>
       </if>
       <if test="statusList!=null and statusList.size>0">
           and wf.fstatus in
           <foreach collection="statusList" item="status" index="i" open="(" close=")" separator=",">
               #{status}
           </foreach>
       </if>
   </sql>
   <select id="searchMyWorkFlows" parameterType="map" resultMap="WorkbenchResultMap">
   SELECT
   wf.Fid id,
   wf.Fcreate_time time,
   CASE
   WHEN wf.Fstatus = 'Submit' THEN
   '新增'
   WHEN wf.Fstatus = 'Reject' THEN
   '駁回'
   WHEN wf.Fstatus = 'Pass' THEN
   '運營稽核通過'
   WHEN wf.Fstatus = 'Finish' THEN
   '合規稽核通過'
   END type,
   CASE
   WHEN wf.Fstep = 1
   AND wf.Fstatus = 'Reject' THEN
   wf.Foperator
   WHEN wf.Fstep = 2
   AND wf.Fstatus = 'Submit' THEN
   wf.Fproposer
   WHEN wf.Fstep = 2
   AND wf.Fstatus = 'Reject' THEN
   wf.Fauditor
   WHEN wf.Fstep = 3
   AND wf.Fstatus = 'Pass' THEN
   wf.Foperator
   WHEN wf.Fstep = 4
   AND wf.Fstatus = 'Finish' THEN
   wf.Fauditor
   END creator,
   CASE
   WHEN wf.Fstep = 1
   AND wf.Fstatus = 'Reject' THEN
   wf.Foper_reason
   WHEN wf.Fstep = 2
   AND wf.Fstatus = 'Reject' THEN
   wf.Faud_reason
   END reason,
   CASE
   WHEN wf.Fobject_type = 0 THEN
   '機構資訊'
   WHEN wf.Fobject_type = 1 THEN
   '運營頁面'
   END objectType,
   wf.Fope_id objectId,
   mkt.ftitle objectEvent,
   wf.Forg_name orgName
   FROM
   (
         SELECT * FROM
      (
   SELECT
      *
   FROM
      t_work_flow
   ORDER BY
      Fcreate_time DESC
      ) temp
       GROUP BY
fope_id
     ORDER BY
Fcreate_time DESC) wf, t_marketing mkt
   where wf.Fope_id=mkt.fid
   <include refid="searchMyWorkFlows"/>
   ORDER BY wf.Fcreate_time desc
</select>