MyBatis 判断条件为等于的问题(<if test="type == '1'.toString()"> xxx </if>

本文探讨了在MyBatis中处理等于条件时常见的问题及解决方案。特别关注如何正确使用字符串转换来确保比较操作的准确性。
 

MyBatis 判断条件为等于的问题

标签: mybatisJava
  22042人阅读  评论(6)  收藏  举报
  分类:
 

在用MyBatis操作数据库的时候相信很多人都用到,当在判断null, 大于,大于等于,小于,小于等于,不等于时估计很多都用到,比较容易实现了,这里就省略了,但唯独判断条件为等于时估计蛮多人遇到坑了, 俺在这个问题上坑了差不多一天,于是把这个实验总结并简要记录一下;

当MyBatis 判断条件为等于的时候,常量需要加 .toString() 来转换,这种方法是稳定的,推荐使用,比如:

[html]  view plain  copy
  1. <!-- 正确的,稳定,推荐使用 -->  
  2. <if test="newsImage != null and newsImage == '1'.toString()">  
  3.     <![CDATA[ and len(newsImage) > 0 ]]>  
  4. </if>  

其中判断 newsImage == '1' 时,人为认为成功,但实际上是不成功的,需要改为   newsImage == '1'.toString()方可成功,原因具体没有细入研究,根据实际使用推测应该是 “等于” 在java中是个比较复杂问题,涉及的“等于”有可能是变量地址相等,或者是变量值内容相等,在XML文件中简单的 == 在经过MyBatis处理后无法判断是哪种类型的“相等”,所以加 .toString()做强制转换操作,MyBatis就知道是值内容的比较,当然就成功了; 注意这个常量不限于数字,对于字母,如 'y' 同样需要加上  .toString()方可成功,如下: 

[html]  view plain  copy
  1. <!-- 正确的,稳定,推荐使用 -->  
  2. <if test="newsImage != null and newsImage == 'y'.toString()">  
  3.     <![CDATA[ and len(newsImage) > 0 ]]>  
  4. </if>  

那给变量加  .toString() 可以吗?这个是错误的,至少实际在所使用的myBatis版本( mybatis-3.2.5.jar, mybatis-spring-1.2.1.jar)是不可以,以后版本不知道,这应该是在经过 myBatis 时,影响到其转换操作,故出现错误,如下是错误的:

[html]  view plain  copy
  1. <!-- 错误的 -->  
  2. <if test="newsImage != null and newsImage.toString() == 'y'">  
  3.     <![CDATA[ and len(newsImage) > 0 ]]>  
  4. </if>  

既然是值内容的比较,我们自然联想到 Java 的  equals , equalsIgnoreCase 关键字,用这个可以吗? 实际测试过, 有时成功,有时不成功(有可能跟我的机子和我使用的Java环境的原因), 很不稳定不推荐使用,所以如下是不稳定的:

[html]  view plain  copy
  1. <!--有时成功,有时不成功,不推荐使用-->  
  2. <if test="newsImage != null and newsImage.equal('y')">  
  3.     <![CDATA[ and len(newsImage) > 0 ]]>  
  4. </if>  
<select id="getValuationSummary" parameterType="com.hundsun.amos.queryrpt.api.bean.AssetValueDTO" resultMap="BaseAssetResultMap"> select * from (with email as (select ff.fund_code, ff.fund_id, <if test="isPpos"> a.DATA_SOURCE, a.publish_rate, a.publish_state, a.ACTIVE_VERIFYRATE, </if> ff.service_type, ff.FUND_TYPE, ff.TRANSACTION_SERVICE_TYPE, apex.FUND_ORIGIN_INFO, apex.SETTLEMENT_MODE, ff.NON_STANDARD_PDT, ff.SPECIAL_RISK_TYPE from amos_pdt_fundinfo ff <if test="isToasseessment"> right join AMOS_TOASSESSMENT_M atm on ff.fund_code=atm.FUND_CODE </if> inner join AMOS_BASE_FUNDAUTHDETAL auth on ff.fund_id = auth.fund_id inner join AMOS_PDT_FUNDINFOEX apex on ff.fund_id = apex.fund_id <if test="isPpos"> left join ( select s.DATA_SOURCE,s.publish_rate,s.publish_state,s.fund_id,s.ACTIVE_VERIFYRATE from AMOS_QYRPT_VALUATIONSET s where not exists (select 1 from (select fund_id from AMOS_QYRPT_VALUATIONSET group by fund_id having count(1) >1) t where t.fund_id=s.fund_id) union ( select f1.DATA_SOURCE,f1.publish_rate,f1.publish_state,f1.fund_id,f1.ACTIVE_VERIFYRATE from (select DATA_SOURCE,publish_rate,fund_id,publish_state,ACTIVE_VERIFYRATE from AMOS_QYRPT_VALUATIONSET where DATA_SOURCE='WB')f1 left join (select DATA_SOURCE,publish_rate,fund_id,publish_state,ACTIVE_VERIFYRATE from AMOS_QYRPT_VALUATIONSET where DATA_SOURCE='TG')f2 on f1.fund_id=f2.fund_id where f2.data_source is not null) )a on ff.fund_id = a.fund_id </if> where 1 = 1 <if test="opeator!=null and opeator!=''"> and auth.user_id=#{opeator} </if> <if test="inputList != null and inputList.size()>0"> and ff.fund_code in <foreach collection="inputList" item="item" index="index" open="(" close=")" separator=","> #{item.vcFundcode} </foreach> </if> <if test="fundType != null and fundType != ''"> AND ff.FUND_TYPE IN <foreach item="item" index="index" collection="fundType.split(',')" open="(" separator="," close=")"> #{item} </foreach> </if> <if test="nonStandardPdt != null and nonStandardPdt != ''"> AND ff.NON_STANDARD_PDT IN <foreach item="item" index="index" collection="nonStandardPdt.split(',')" open="(" separator="," close=")"> #{item} </foreach> </if> <if test="settlementMode != null and settlementMode != ''"> AND apex.settlement_mode IN <foreach item="item" index="index" collection="settlementMode.split(',')" open="(" separator="," close=")"> #{item} </foreach> </if> <if test="serviceType != null and serviceType != ''"> AND <foreach item="item" index="index" collection="serviceType.split(',')" open="(" separator="or" close=")"> instr(ff.service_type, #{item}) > 0 </foreach> </if> <if test="transactionServiceType != null and transactionServiceType != ''"> AND <foreach item="item" index="index" collection="transactionServiceType.split(',')" open="(" separator="or" close=")"> instr(ff.TRANSACTION_SERVICE_TYPE, #{item}) > 0 </foreach> </if> <if test="specialRiskType != null and specialRiskType != ''"> AND <foreach item="item" index="index" collection="specialRiskType.split(',')" open="(" separator="or" close=")"> instr(nvl(ff.SPECIAL_RISK_TYPE, '0'), #{item}) > 0 </foreach> </if> <if test="fundOriginInfo != null and fundOriginInfo != ''"> AND <foreach item="fundOriginInfoItem" index="index" collection="fundOriginInfo.split(',')" open="(" separator="or" close=")"> instr(apex.FUND_ORIGIN_INFO, #{fundOriginInfoItem}) > 0 </foreach> </if> ) <if test="enSjly !='WB'"> select h.vc_glr vc_glr, <include refid="resultSql"/> <if test="isPpos"> nvl(email.publish_rate,'1') plpl, nvl( email.publish_state,'1') plzt, decode(email.publish_rate,'3',email.ACTIVE_VERIFYRATE) actual_check_frequency, nvl(email.ACTIVE_VERIFYRATE, decode(email.publish_rate, '3', least(foe.compare_Freq)) ) sjplpl, </if> '托管估值' en_sjly_name, 'TG' en_sjly, email.FUND_TYPE, email.TRANSACTION_SERVICE_TYPE, email.FUND_ORIGIN_INFO, email.SETTLEMENT_MODE, email.NON_STANDARD_PDT, email.SPECIAL_RISK_TYPE from tgcbs.tjjmrhjsj t inner join email on t.vc_jjdm=email.fund_code and ( INSTR(','||email.SERVICE_TYPE||',',',1,')>0 and INSTR(','||email.SERVICE_TYPE||',',',2,')=0 and INSTR(','||email.SERVICE_TYPE||',',',6,')=0 and instr(','||email.SERVICE_TYPE||',',',7,')=0) <include refid="mfcodeSql"/> left join tgcbs.ttmpgzb_index a on t.l_ztbh = a.l_ztbh and t.d_rq = a.d_ywrq left join (select l_ztbh, l_dzjg , d_begin from (select zb.l_ztbh, zb.l_dzjg, zb.d_begin, row_number() over(partition by zb.l_ztbh, zb.d_begin order by zb.d_scrq desc) rn from tgcbs.tycdz_tgr_zb zb where zb.l_sfbz = 0 <if test="startDate != null and startDate!= '' and endDate!=null and endDate!=''"> and zb.d_begin between to_date(#{startDate},'yyyy-mm-dd') and to_date(#{endDate},'yyyy-mm-dd') </if> <if test="dateRate != null and dateRate!= ''"> <choose> <when test="dateRateWeekMonthFlag != null and dateRateWeekMonthFlag != '' and dateRateWeekMonthFlag == '1'.toString()"> and to_char(zb.d_begin ,'yyyy-MM-dd') IN <foreach collection="dateRateWeekMonthDates" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </when> <otherwise> and exists ( <include refid="tradeDateSql1"/> ) </otherwise> </choose> </if> ) where rn = 1) b on t.l_ztbh = b.l_ztbh and b.d_begin=t.d_rq , (select g.vc_mc vc_glrmc, f.vc_glr, f.vc_tgr, f.vc_name, f.l_fundid from tgcbs.tfundinfo f left join tgcbs.tglrxx g on f.vc_glr = g.vc_bh ) h <include refid="filterArgs"/> <if test="isPpos"> and nvl(email.publish_state, '1') in (nvl(a.l_sfqr, 0),'3') <include refid="pposSql"/> </if> </if> <if test="enSjly==null"> union all </if> <if test="enSjly !='TG'"> select h.vc_glrmc vc_glr, <include refid="resultSql"/> <if test="isPpos"> nvl(email.publish_rate,'1') plpl, nvl( email.publish_state,'3') plzt, decode(email.publish_rate,'3',email.ACTIVE_VERIFYRATE) actual_check_frequency, nvl(email.ACTIVE_VERIFYRATE, decode(email.publish_rate, '3', least(foe.compare_Freq)) ) sjplpl, </if> '外包估值' en_sjly_name, 'WB' en_sjly, email.FUND_TYPE, email.TRANSACTION_SERVICE_TYPE, email.FUND_ORIGIN_INFO, email.SETTLEMENT_MODE, email.NON_STANDARD_PDT, email.SPECIAL_RISK_TYPE from hsfa.tjjmrhjsj t inner join email on t.vc_jjdm=email.fund_code and (INSTR(','||email.SERVICE_TYPE||',',',2,')>0 or INSTR(','||email.SERVICE_TYPE||',',',6,')>0 or instr(','||email.SERVICE_TYPE||',',',7,')>0) <include refid="mfcodeSql"/> left join hsfa.ttmpgzb_index a on t.l_ztbh = a.l_ztbh and t.d_rq = a.d_ywrq left join (select l_ztbh, l_dzjg , d_begin from (select zb.l_ztbh, zb.l_dzjg, zb.d_begin, row_number() over(partition by zb.l_ztbh, zb.d_begin order by zb.d_scrq desc) rn from hsfa.tycdz_zb zb where zb.l_sfbz = 0 <if test="startDate != null and startDate!= '' and endDate!=null and endDate!=''"> and zb.d_begin between to_date(#{startDate},'yyyy-mm-dd') and to_date(#{endDate},'yyyy-mm-dd') </if> <if test="dateRate != null and dateRate!= ''"> <choose> <when test="dateRateWeekMonthFlag != null and dateRateWeekMonthFlag != '' and dateRateWeekMonthFlag == '1'.toString()"> and to_char(zb.d_begin ,'yyyy-MM-dd') IN <foreach collection="dateRateWeekMonthDates" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </when> <otherwise> and exists ( <include refid="tradeDateSql1"/> ) </otherwise> </choose> </if> ) where rn = 1) b on t.l_ztbh = b.l_ztbh and b.d_begin=t.d_rq , (select g.vc_mc vc_glrmc, f.vc_glr, f.vc_tgr, f.vc_name, f.l_fundid from hsfa.tfundinfo f left join hsfa.tglrxx g on f.vc_glr = g.vc_bh ) h <include refid="filterArgs"/> <if test="isPpos"> and nvl(email.publish_state, '3') in (nvl(a.l_sfqr, 0),'3') <include refid="pposSql"/> </if> </if>) order by d_rq desc </select>换成可测试的
11-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值