动态sql-if判断无效

在一次写项目时,自定前端传过来的参数,并通过此参数进行if判断之后拼接sql,导致拼接不成功

    <select id="schedule1" resultType="com.xysoft.project.student.course.entity.CourseScoreRecord">
        select t1.* from
        t_s_course_score_record t1
        left join t_conduct_lanuch_record t2 on t1.id = t2.business_id
        where t1.is_add = #{isAdd,jdbcType=VARCHAR}
        and t1.id = t2.business_id
        <if test="schedule == '1'">
            and t2.status = '6'
        </if>
        <if test="schedule == '2'">
            and t2.status = '1'
        </if>
        <if test="schedule == '3'">
            and t2.status = '0'
        </if>
    </select>

经过多次搜索寻找原因,最后发现是因为该参数不是表中的字段导致的;因为在进行if判断时如果参数不是表中存在的字段那么就会默认将它理解为一个值,从而导致判断没有意义,这个时候就需要写成 <if test=" schedule.toString() == ‘2’.toString()"></if>

正确写法如下:

    <select id="schedule1" resultType="com.xysoft.project.student.course.entity.CourseScoreRecord">
        select t1.* from
        t_s_course_score_record t1
        left join t_conduct_lanuch_record t2 on t1.id = t2.business_id
        where t1.is_add = #{isAdd,jdbcType=VARCHAR}
        and t1.id = t2.business_id
        <if test="schedule.toString() == '1'.toString()">
            and t2.status = '6'
        </if>
        <if test="schedule.toString() == '2'.toString()">
            and t2.status = '1'
        </if>
        <if test="schedule.toString() == '3'.toString()">
            and t2.status = '0'
        </if>
    </select>

或者不用单引号包裹判断的条件:

    <select id="schedule1" resultType="com.xysoft.project.student.course.entity.CourseScoreRecord">
        select t1.* from
        t_s_course_score_record t1
        left join t_conduct_lanuch_record t2 on t1.id = t2.business_id
        where t1.is_add = #{isAdd,jdbcType=VARCHAR}
        and t1.id = t2.business_id
        <if test="schedule == 1">
            and t2.status = '6'
        </if>
        <if test="schedule == 2">
            and t2.status = '1'
        </if>
        <if test="schedule == 3">
            and t2.status = '0'
        </if>
    </select>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值