MyBatis中if标签判断数字(条件)相等
判断的字段是Integer类型
query类:
public class Query{
/**
* 条件
*/
private Integer a;
}
下面两种都可以:
<select id="countTable" resultType="java.lang.Long">
select count(*) from table_name
<where>
<if test="a == 3">
and A = #{a}
</if>
</where>
</select>
<select id="countTable" resultType="java.lang.Long">
select count(*) from table_name
<where>
<if test="a == '3'.toString()">
and A = #{a}
</if>
</where>
</select>
判断的字段是String类型
- 如果这个字段的值是数字,那么和Integer类型是一样的,3 或者 ‘3’.toString() 都可以,但是 ‘3’ 不行
- 如果这个字段的值不是数字,比如: “y” ,那么就要这样写:‘y’.toString()
query类:
public class Query{
/**
* 条件
*/
private String a;
}
sql:
<select id="countTable" resultType="java.lang.Long">
select count(*) from table_name
<where>
<if test="a == '3'.toString()">
and A = #{a}
</if>
<if test="a != null and a != '3'.toString()">
and B = #{a}
</if>
</where>
</select>