这两天做了一个随机出题的功能,用到了多条件模糊查询,在此记录一下。
<select id="selectLike" parameterType="map" resultMap="AllBaseResultMap">
select * from choice_question,type_of_test,question_types,knowledge_point
<where>
choice_question.type_id=type_of_test.type_id AND
choice_question.question_id=question_types.question_id AND
choice_question.knowledge_id=knowledge_point.knowledge_id AND
<if test="questionName != null">
question_name LIKE CONCAT('%','${questionName}','%')
</if>
<if test="choiceDegree != null">
AND choice_degree LIKE CONCAT('%','${choiceDegree}','%')
</if>
<if test="typeName !=null">
AND type_name LIKE CONCAT('%','${typeName}','%')
</if>
</where>
</select>
接口
List<NewChoiceQuestionEntity> selectLike(Map map);
service层
public List<NewChoiceQuestionEntity> selectLike(Map<String, String> map) {
String a = map.get("questionName");
String b = map.get("choiceDegree");
String c = map.get("typeName");
Integer d = Integer.parseInt(map.get("number"));
System.out.println(a + b + c + d);
Map map1 = new HashMap();
map1.put("questionName", a);
map1.put("choiceDegree", b);
map1.put("typeName", c);
List list = choiceQuestionEntityMapper.selectLike(map1);
List list3 = new ArrayList(); //新数据存的地方
List<Integer> list2 = new ArrayList<>();//随机数存的地方
while (list2.size() < d) {
int i = (int) (Math.random() * list.size());//产生随机数
if (!list2.contains(i)) {
list2.add(i);
}
}
for (int j = 0; j < list2.size(); j++) {
list2.get(j);
list3.add(list.get(list2.get(j)));
}
return list3;
}
}