在一次写项目时,自定前端传过来的参数,并通过此参数进行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>