1. 程式人生 > 程式設計 >Mybatis 動態sql if 判讀條件等於一個數字的案例

Mybatis 動態sql if 判讀條件等於一個數字的案例

在Mybatis中 mapper中

boolean updateRegisterCompanyFlag(@Param(value = "companyId") String companyId,

@Param(value = "flag") String flag);

傳入的flag型別為String,但在mapper.XML中進行判斷是下意識地以為判斷的值要加上引號

 <if test=" '4' == flag ">,LAST_CHECK_TIME = sysdate
 </if>

但是這樣寫,傳入flag=“4”後,這個if語句一直不成立s

ql沒有拼接。後來網上查了一番後將 test 中的 4 的引號去除才成功。

 <if test=" 4 == flag ">,LAST_CHECK_TIME = sysdate
 </if>

記錄一下,以防忘記。。。

補充知識:Mybatis中xml的有關if test以及==的用法

Mybatis中,通常都要在mapper.xml中寫複雜的sql語句,結合頁面的查詢篩選條件,需要在sql語句中加入

<if test=""></if>等的判斷。而這test 後面的欄位也是來自jsp或者html頁面,但是不是隨便書寫的。

Mybatis 動態sql if 判讀條件等於一個數字的案例

Mybatis 動態sql if 判讀條件等於一個數字的案例

與頁面都要一一對應,除此之外,還需要在實體類中有定義。

Mybatis 動態sql if 判讀條件等於一個數字的案例

另外,mybatis中 <if test=>等於的條件怎麼寫?

注意 僅僅需要將雙引號和單引號的位置換一下即可!!!

Mybatis 動態sql if 判讀條件等於一個數字的案例

<sql id="queryData">
	select '集團' src_dbct_cd,substr(stats_mo,1,4) year,5,6) month,sum(all_zb_cars) all_zb_cars,sum(all_kb_cars) all_kb_cars,sum(all_wd_cars) all_wd_cars,sum(all_zb_cars+all_kb_cars+all_wd_cars) all_cars_total,sum(round(all_zb_exp/10000,2)) all_zb_exp,sum(round(all_kb_exp/10000,2))
	all_kb_exp,sum(round(all_wd_exp/10000,2)) all_wd_exp,sum(round(all_zb_exp/10000+all_kb_exp/10000+all_wd_exp/10000,2))
	all_exp_total,sum(zb_cars) zb_cars,sum(kb_cars) kb_cars,sum(zb_cars+kb_cars) cars_total,sum(round(zb_net_wgt/10000000,2)) zb_net_wgt,sum(round(kb_net_wgt/10000000,2)) kb_net_wgt,sum(round(ifnull(zb_net_wgt/10000000,0)+ifnull(kb_net_wgt/10000000,0),2))
	wgt_total,sum(round(zb_exp/10000,2)) zb_exp,sum(round(kb_exp/10000,2)) kb_exp,sum(round(zb_exp/10000+kb_exp/10000,2)) exp_total,sum(zb_lod_mnt) zb_lod_mnt,sum(kb_lod_mnt) kb_lod_mnt,sum(ifnull(zb_lod_mnt,0)+ifnull(kb_lod_mnt,0)) lod_mnt_total,sum(zb_wait_mnt) zb_wait_mnt,sum(kb_wait_mnt) kb_wait_mnt,sum(ifnull(zb_wait_mnt,0)+ifnull(kb_wait_mnt,0)) wait_mnt_total,concat(round(sum(zb_dlyd_cars/zb_cars)*100,2),'%') zb_dlyd_pt,concat(round(sum(kb_dlyd_cars/kb_cars)*100,'%') kb_dlyd_pt,concat(round(sum(ifnull(zb_dlyd_cars/zb_cars,0)+ifnull(kb_dlyd_cars/kb_cars,0))*100,'%')
	dlyd_pt_total,concat(round(sum(zb_frqc_dlyd_cars/zb_cars)*100,'%') zb_frqc_dlyd,concat(round(sum(kb_frqc_dlyd_cars/kb_cars)*100,'%') kb_frqc_dlyd,concat(round(sum(ifnull(zb_frqc_dlyd_cars/zb_cars,0)+ifnull(kb_frqc_dlyd_cars/kb_cars,'%')
	frqc_dlyd_total,sum(zb_dlyd_mnt) zb_dlyd_mnt,sum(kb_dlyd_mnt) kb_dlyd_mnt,sum(ifnull(zb_dlyd_mnt,0)+ifnull(kb_dlyd_mnt,0)) dlyd_mnt_total,round(ifnull(sum(zb_sys_mlg)/sum(zb_stip_mnt),2) zb_stip_speed,round(ifnull(sum(kb_sys_mlg)/sum(kb_stip_mnt),2) kb_stip_speed,(round(ifnull(sum(zb_sys_mlg)/sum(zb_stip_mnt),0)+ifnull(sum(kb_sys_mlg)/sum(kb_stip_mnt),2))
	stip_speed_total,round(ifnull(sum(zb_est_mlg)/sum(zb_actl_mnt),2) zb_actl_speed,round(ifnull(sum(kb_est_mlg)/sum(kb_actl_mnt),2) kb_actl_speed,(round(ifnull(sum(zb_est_mlg)/sum(zb_actl_mnt),0)+ifnull(sum(kb_est_mlg)/sum(kb_actl_mnt),2))
	actl_speed_total,sum(round(zb_pnlt/10000,2)) zb_pnlt,sum(round(kb_pnlt/10000,2)) kb_pnlt,sum(ifnull(round(zb_pnlt/10000,0)+ifnull(round(kb_pnlt/10000,0))
	pnlt_total,sum(zb_uld_wait_mnt) zb_uld_wait_mnt,sum(kb_uld_wait_mnt) kb_uld_wait_mnt,sum(wd_uld_wait_mnt) wd_uld_wait_mnt,round((ifnull(sum(zb_uld_wait_mnt),0)+ifnull(sum(kb_uld_wait_mnt),0)+ifnull(sum(wd_uld_wait_mnt),0))/
	(ifnull(sum(all_zb_cars),0)+ifnull(sum(all_kb_cars),0)+ifnull(sum(all_wd_cars),0)),2)
	uld_wait_mnt_total,sum(zb_uld_mnt) zb_uld_mnt,sum(kb_uld_mnt) kb_uld_mnt,sum(wd_uld_mnt) wd_uld_mnt,sum(ifnull(zb_uld_mnt,0)+ifnull(kb_uld_mnt,0)+ifnull(wd_uld_mnt,0)) uld_mnt_total,concat(round(sum(zb_brk_cars/zb_mlg/10000)*100,'%') zb_brk_cars,concat(round(sum(kb_brk_cars/kb_mlg/10000)*100,'%') kb_brk_cars,concat(round(sum(wd_brk_cars/wd_mlg/10000)*100,'%') wd_brk_cars,concat((round(sum(ifnull(zb_brk_cars/zb_mlg/10000,0)+ifnull(kb_brk_cars/kb_mlg/10000,0)+ifnull(wd_brk_cars/wd_mlg/10000,2)),'%')
	brk_cars_total,concat(round(sum(zb_accdt_cars/zb_mlg/10000)*100,'%') zb_accdt_cars,concat(round(sum(kb_accdt_cars/kb_mlg/10000)*100,'%') kb_accdt_cars,concat(round(sum(wd_accdt_cars/wd_mlg/10000)*100,'%') wd_accdt_cars,concat((round(sum(ifnull(zb_accdt_cars/zb_mlg/10000,0)+ifnull(kb_accdt_cars/kb_mlg/10000,0)+ifnull(wd_accdt_cars/wd_mlg/10000,'%')
	accdt_cars_total,concat(round(sum(actl_tms/stip_tms)*100,'%') tms_total
	from bdrpt.kd_car_trans_mont_stats
	where 1=1
	<if test="stats_mo != null and stats_mo != ''" >
	and concat(SUBSTR(stats_mo,4),'-',SUBSTR(stats_mo,6))=#{stats_mo}
	</if> 
	<if test='SRC_DBCT_CD == "2"'>
	and src_dbct_cd = '分撥'
	</if>
	GROUP BY substr(stats_mo,6) 
	UNION 
	select l.location_name src_dbct_cd,all_zb_cars,all_kb_cars,all_wd_cars,(all_zb_cars+all_kb_cars+all_wd_cars) all_cars_total,round(all_zb_exp/10000,2) all_zb_exp,round(all_kb_exp/10000,2)
	all_kb_exp,round(all_wd_exp/10000,2) all_wd_exp,round(all_zb_exp/10000+all_kb_exp/10000+all_wd_exp/10000,2)
	all_exp_total,zb_cars,kb_cars,(zb_cars+kb_cars) cars_total,round(zb_net_wgt/10000000,2) zb_net_wgt,round(kb_net_wgt/10000000,2) kb_net_wgt,round(ifnull(zb_net_wgt/10000000,2)
	wgt_total,round(zb_exp/10000,2) zb_exp,round(kb_exp/10000,2) kb_exp,round(zb_exp/10000+kb_exp/10000,2) exp_total,zb_lod_mnt,kb_lod_mnt,(ifnull(zb_lod_mnt,zb_wait_mnt,kb_wait_mnt,(ifnull(zb_wait_mnt,concat(round(zb_dlyd_cars/zb_cars*100,concat(round(kb_dlyd_cars/kb_cars*100,concat(round((ifnull(zb_dlyd_cars/zb_cars,concat(round(zb_frqc_dlyd_cars/zb_cars*100,concat(round(kb_frqc_dlyd_cars/kb_cars*100,concat(round((ifnull(zb_frqc_dlyd_cars/zb_cars,zb_dlyd_mnt,kb_dlyd_mnt,(ifnull(zb_dlyd_mnt,round(zb_pnlt/10000,2) zb_pnlt,round(kb_pnlt/10000,2) kb_pnlt,(ifnull(round(zb_pnlt/10000,zb_uld_wait_mnt,kb_uld_wait_mnt,wd_uld_wait_mnt,zb_uld_mnt,kb_uld_mnt,wd_uld_mnt,(ifnull(zb_uld_mnt,concat(round(zb_brk_cars/zb_mlg/10000*100,concat(round(kb_brk_cars/kb_mlg/10000*100,concat(round(wd_brk_cars/wd_mlg/10000*100,concat((round((ifnull(zb_brk_cars/zb_mlg/10000,concat(round(zb_accdt_cars/zb_mlg/10000*100,concat(round(kb_accdt_cars/kb_mlg/10000*100,concat(round(wd_accdt_cars/wd_mlg/10000*100,concat((round((ifnull(zb_accdt_cars/zb_mlg/10000,concat(round(actl_tms/stip_tms*100,'%') tms_total
	from bdrpt.kd_car_trans_mont_stats t LEFT JOIN bdkdbase.location l on t.src_dbct_cd = l.location_xid
	where 1=1
	<if test="stats_mo != null and stats_mo != ''" >
	and concat(SUBSTR(stats_mo,6))=#{stats_mo}
	</if>
	<if test='SRC_DBCT_CD == "1"'>
	and src_dbct_cd = '集團'
	</if>
	<if test="SRC_DBCT_NM != null and SRC_DBCT_NM != ''">
	and src_dbct_cd = #{SRC_DBCT_NM}
	</if>
	GROUP BY src_dbct_cd,6)
	</sql>
	
	<!-- 查詢資料 -->
	<select id="pageQuery" resultType="map">
	<include refid="queryData"></include>
	</select>

以上這篇Mybatis 動態sql if 判讀條件等於一個數字的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。