今天写crud摸鱼的时候莫名其妙遇到了一个很奇葩的问题,然后奇妙的解决了。再此记录一下
- 首先是接口
double XXX(@Param("statrtTime") String statrtTime,@Param("endTime")String endTime,@Param("kind")String kind);
- 在写xml的mapper文件的时候,需要对变量
kind
进行一次字符串的判断,本来满足判断的情况下却没通过
<select id="XXX" resultType="double">
SELECT SUM(cost) FROM XXX where state=3
<if test="#{statrtTime}!=null and #{statrtTime}!='' and #{endTime}!=null and #{endTime}!=''">
and DATE_FORMAT(success_time,'%Y-%m-%d') BETWEEN #{statrtTime} AND #{endTime}
</if>
<if test="#{kind}!=null and #{kind}!=''">
<if test="#{kind} == '1'.toString()">
and cost > 0
</if>
<if test="#{kind} == '-1'.toString()">
and cost < 0
</if>
</if>
</select>
然后经过一顿骚操作后,发现如果把变量的#{}
给删掉的话就可以进入<if>
判断了
<select id="XXX" resultType="double">
SELECT SUM(cost) FROM XXX where state=3
<if test="#{statrtTime}!=null and #{statrtTime}!='' and #{endTime}!=null and #{endTime}!=''">
and DATE_FORMAT(success_time,'%Y-%m-%d') BETWEEN #{statrtTime} AND #{endTime}
</if>
<if test="#{kind}!=null and #{kind}!=''">
<if test="kind == '1'.toString()">
and cost > 0
</if>
<if test="kind == '-1'.toString()">
and cost < 0
</if>
</if>
</select>