Mybatis中根据不同条件执行不同sql之test 条件判断

test 条件判断

近期开发过程中,想在mybatis中根据不同条件执行不同的sql,想到了用

<if test="quoteFlag != null and quoteFlag !='' ">
sql 语句
<if test="quoteFlag != null and quoteFlag =='2' ">
sql 语句
<if test="quoteFlag != null and quoteFlag =='1' ">
sql语句

执行过程中发现报错了,具体报错内容就不展示,总体原因是以上代码语法不正确导致的。下面展示错误代码

错误代码块

 <when test="quoteFlag != null and quoteFlag == '1'">
                <include refid="Base_Column_List2"/>,rf.quoteCount
                from (select <include refid="Base_Column_List1"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = r.LSRF_NO  and r.COMPANY_CODE=#{companyCode}) as quoteCount
                from lsp_rfx r where 1=1 AND r.COMPANY_CODE=#{companyCode}) rf where 1=1 and rf.quoteCount > '0'
                <include refid="findCondition2"/>
            </when>
            <when test="quoteFlag == ''">
                <include refid="Base_Column_List2"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = rf.LSRF_NO) as quoteCount
                from lsp_rfx rf where 1=1
                <include refid="findCondition2"/>
            </when>
            <!--<when test="quoteFlag != null and quoteFlag == '0'">-->
                <!--<include refid="Base_Column_List2"/>,rf.quoteCount-->
                <!--from (select <include refid="Base_Column_List1"/>,-->
                <!--(select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = r.LSRF_NO  and r.COMPANY_CODE = #{companyCode}) as quoteCount-->
                <!--from lsp_rfx r where 1=1 and r.COMPANY_CODE = #{companyCode}) rf where 1=1 and rf.quoteCount= '0'-->
                <!--<include refid="findCondition2"/>-->
            <!--</when>-->
        </choose>
这种传递方式springMVC中解析为字符,不是强对象类型,因此需要做对应的转义操作,正确的代码展示如下
 <when test='quoteFlag != null and quoteFlag == "1"'>
                <include refid="Base_Column_List2"/>,rf.quoteCount
                from (select <include refid="Base_Column_List1"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = r.LSRF_NO  and r.COMPANY_CODE=#{companyCode}) as quoteCount
                from lsp_rfx r where 1=1 AND r.COMPANY_CODE=#{companyCode}) rf where 1=1 and rf.quoteCount > '0'
                <include refid="findCondition2"/>
            </when>
            <when test='quoteFlag != null and quoteFlag == "2"'>
                <include refid="Base_Column_List2"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = rf.LSRF_NO) as quoteCount
                from lsp_rfx rf where 1=1
                <include refid="findCondition2"/>
            </when>
        </choose>
或者
  <when test="quoteFlag != null and quoteFlag == '1'.toString() ">
                <include refid="Base_Column_List2"/>,rf.quoteCount
                from (select <include refid="Base_Column_List1"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = r.LSRF_NO  and r.COMPANY_CODE=#{companyCode}) as quoteCount
                from lsp_rfx r where 1=1 AND r.COMPANY_CODE=#{companyCode}) rf where 1=1 and rf.quoteCount > '0'
                <include refid="findCondition2"/>
            </when>
            <when test="quoteFlag != null and quoteFlag == '2'.toString() ">
                <include refid="Base_Column_List2"/>,
                (select count(q.ID) from lsp_rfx_quote q where q.LSRQ_NO = rf.LSRF_NO) as quoteCount
                from lsp_rfx rf where 1=1
                <include refid="findCondition2"/>
            </when>
        </choose>

<if test='quoteFlag == "1" '>或者改为<if test="quoteFlag == '1'.toString() ">
这样就可以解决以上错误了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值