按条件查询

按条件查询分为几个步骤:

       1.从页面上获取查询条件
       2.将查询条件传到后台相应的方法中去
       3.将这些查询条件全部放在一个map中
       4.写sql语句进行查询并把结果返回给前端页面上去展示



比较关键的就是sql语句的书写,将所有的条件都列出,如果为空则不要,不为空在带入查询,举例:

<select id = "findOrdersByCon" parameterType="java.util.Map" resultType="com.chemguan.entity.Order"> 
 
SELECT * FROM `order` where 1=1 
 
<if test="orderNumber != null">   
 
         
 and orderNumber = #{orderNumber}   
 
         </if>  
 
         <if test="columnName != null">   
 
         
 and columnName = #{columnName}  
 
         </if> 
 
         <if test="productName != null">   
 
         
 and productName = #{productName}  
 
         </if> 
 
         <if test="customerNumber != null">   
 
         
 and customerNumber = #{customerNumber}  
 
         </if> 
 
         <if test="mid != null">   
 
         
 and mid = #{mid}  
 
         </if> 
 
         <if test="storeName != null">   
 
         
 and storeName = #{storeName}  
 
         </if> 
 
         <if test="date1 != null">   
 
         
 and createTime >=#{date1}  
 
         </if> 
 
         <if test="date2 != null">   
 
         
 and createTime <=#{date2}  
 
         </if> 
 
</select>


可以通过ajax方式,也可以通过form表单形式,看情况而定,通过ajax的话要拼接字符串,用form的话则要将获取的查询条件返回到页面上进行显示。反正都可以实现按条件查询的需求,看自己的喜好来!!!