1. 程式人生 > >Mybaties相關的坑(一)

Mybaties相關的坑(一)

一、注意>=、<=、<> 等sql關係符號的使用

如下程式碼:

<select id="queryGoodsList" resultType="com.sanbang.vo.GoodsInfo" parameterType="java.util.Map">
		select g.id,
		g.name,
		IFNULL((select s.salePrice from ezs_goods_audit_process s where s.goods_id = g.id and s.deleteStatus = '0'),0.00) as price,
		g.inventory,
		CONCAT("https://m.ezaisheng.com/",REPLACE(a.path,'\\','/'),"/",a.name) picturePath,
		d1.name colorName,
		ar.areaName areaName,
		d2.name utilName,
		g.area_id ,
		(select IFNULL(SUM(gc.count),0) from ezs_orderform ef LEFT JOIN ezs_goodscart gc on ef.id = gc.of_id where ef.deleteStatus = '0' and gc.goods_id = g.id and (ef.order_status BETWEEN 50 AND 110 and ef.order_status &lt;&gt; 100)) as salecount 
		from ezs_goods g,ezs_accessory a,ezs_dict d1,ezs_area ar,ezs_dict d2
		where g.goods_main_photo_id = a.id and g.color_id = d1.id and g.area_id = ar.id and g.util_id = d2.id AND g.`status`=2 and g.deleteStatus = '0'
		<if test="sales != null and sales == 1">
			ORDER BY salecount DESC
		</if>
	    limit #{pageStart},#{pageSize}
	</select>

注意其中:

and (ef.order_status &gt;= 50 and  ef.order_status &lt;= 110 and ef.order_status <> 100)

在SQL中是沒問你題的,在Mybaties中會報錯。

其中可以使用 BETWEEN 50 AND 110 取代  ef.order_status &gt;= 50 and  ef.order_status &lt;= 110

亦可以使用如下模式:

and (ef.order_status BETWEEN 50 AND 110 and ef.order_status &lt;&gt; 100)

用 &gt;=取代 >=、&lt;=取代<=、使用 &lt;&gt; 取代 <>,否者編譯是過不去的。

注意 between    and   包含邊界值 區間 如:[2,4]

二、在sql片段裡面對同一欄位使用多個foreach標籤

<select id="getPriceTrendcyNew" resultMap="PriceTrendcyNewResultMap">
    select
	IFNULL(AVG(a.price),0) currentPrice,
	a.data_time dealDate,
	gc.name className,
	(select area.areaName from ezs_area area where area.id =  a.region_id) areaName ,
	a.region_id,
	(
	select IFNULL(AVG(p.price),0)
	from ezs_price_trend p where p.status = '2' and p.goodClass2_id = a.goodClass2_id and date_add(DATE_FORMAT(p.data_time,'%Y-%m-%d'),interval 1 day) = DATE_FORMAT(a.data_time,'%Y-%m-%d')
      <if test="areaIds != null and areaIds.size > 0 " >
        and p.region_id 
		<foreach collection="areaIds" index="index" item="areaIds_1" open="in (" close=")" separator=",">
         #{areaIds_1}
     	</foreach>
      </if>
 	GROUP BY DATE_FORMAT(p.data_time,'%Y-%m-%d')
	) prePrice
	from ezs_price_trend a LEFT JOIN ezs_goods_class gc on a.goodClass2_id = gc.id
	where  a.status = '2'
      <if test="kindId != null" >
        and a.goodClass2_id = #{kindId}
      </if>
      <if test="areaIds != null and areaIds.size > 0 " >
        and a.region_id 
		<foreach collection="areaIds" index="index" item="areaIds_2" open="in (" close=")" separator=",">
         #{areaIds_2}
     	</foreach>
      </if>
      <!-- 篩選條件:一週、一月、一季度、一年 -->
      <choose>
      	<when test="dateBetweenType != null and dateBetweenType == 'WEEK' ">
      		and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 WEEK)
      	</when>
      	<when test="dateBetweenType != null and dateBetweenType == 'MONTH' ">
      		and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH)
      	</when>
      	<when test="dateBetweenType != null and dateBetweenType == 'QUARTER' ">
      		and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 QUARTER)
      	</when>
      	<when test="dateBetweenType != null and dateBetweenType == 'YEAR' ">
      		and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 YEAR)
      	</when>
      	<otherwise>
      		<!-- and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH) -->
      		and DATE_FORMAT(a.data_time,'%Y-%m-%d') > DATE_ADD(NOW(),INTERVAL -1 MONTH)
      	</otherwise>
      </choose>
 	GROUP BY DATE_FORMAT(a.data_time,'%Y-%m-%d') ORDER BY DATE_FORMAT(a.data_time,'%Y-%m-%d') desc <!-- LIMIT #{startPos},#{pageSize} -->
  </select>

其中:

and p.region_id 
		<foreach collection="areaIds" index="index" item="areaIds_1" open="in (" close=")" separator=",">
         #{areaIds_1}
     	</foreach>

和:

 and a.region_id 
		<foreach collection="areaIds" index="index" item="areaIds_2" open="in (" close=")" separator=",">
         #{areaIds_2}
     	</foreach>

第二段針對父查詢的region_id進行篩選,第一段是針對子查詢裡面的region_id進行篩選,傳入mapper中集合areaIds是一個,item需要區分開,例如:areaIds_2、areaIds_1。

注意裡面兩個foreach標籤的使用!!!!