多条件组合模糊查询(也适用于多个条件中选一个的情况)

同时满足多个条件进行模糊查询(主要针对有多个查询框,此时查询框有无查询条件都可以);

也适用于只有一个查询框但是可以有多个查询条件进行随机选择的情况,当然查询条件也可以为空。

 1 <select id="findFuzzyRoster" resultType="com.example.pojo.Roster">
 2        select job_number, full_name, sex, mobile, id_type, id_card, bank_name,
 3        bank_account, register_status, type_of_work
 4        from roster where customer_id = #{customerId}    //若是没有固定条件,可以将customer_id = #{customerId}换成1 = 1
 5                                      //主要为了防止下面的条件都不满足时出现的空指针异常
 6        <if test="mobile!=null and mobile.length!=0">   //test后的判断条件内容为传入参数
 7             and  mobile like CONCAT('%',#{mobile},'%')    //如果条件不满足就会断开此处的and连接,进行下一个if判断;满足条件则执行。
 8        </if>
 9        <if test="fullName!=null and fullName.length!=0">
10             and full_name like CONCAT('%',#{fullName},'%')  
11        </if>
12        <if test="jobNumber!=null and jobNumber.length!=0">
13             and job_number like CONCAT('%',#{jobNumber},'%')
14        </if> 
15     </select>

注意:上述情况的模糊查询不建议使用类似mobile like '%#{mobile}%'的形式,这种形式可能会出现?无法替换的异常。

转载于:https://www.cnblogs.com/H-Dream/p/11306197.html