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