实现多条件的模糊查询

这两天做了一个随机出题的功能,用到了多条件模糊查询,在此记录一下。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值