记录一个使用mybatis中的小问题
使用
当条件中有判断值是, 要是用 ==,而不能使用单个 =,
mybatis是用OGNL表达式来解析的,在OGNL的表达式中,’1’会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。
总结下使用方法:单个的字符要写到双引号里面或者使用.toString()才行!
以下为错误示范:
<if test="customerState!=null and customerState= '1' ">
and rc.user_id is not null
</if>
<if test="customerState!=null and customerState='2'">
and rc.name_tags_dcount is not null
</if>
<if test="customerState!=null and customerState='3'">
and rc.name_tags_scount is not null
</if>
<if test="customerState!=null and customerState='4'">
and rc.status = '7'
</if>
<if test="customerState!=null and customerState='5'">
and rc.status = '11'
</if>
查看打印出的sql
WHERE
1 = 1
AND rc.shareId = ?
AND rc.user_id IS NOT NULL
AND rc.name_tags_dcount IS NOT NULL
AND rc.name_tags_scount IS NOT NULL
AND rc.STATUS = ‘7’
AND rc.STATUS = ‘11’
很明显这不是我想要的, 修改一下
<if test="customerState!=null and customerState== '1'.toString()">
and rc.user_id is not null
</if>
<if test="customerState!=null and customerState=='2'.toString()">
and rc.name_tags_dcount is not null
</if>
<if test="customerState!=null and customerState=='3'.toString()">
and rc.name_tags_scount is not null
</if>
<if test="customerState!=null and customerState=='4'.toString()">
and rc.status = '7'
</if>
<if test="customerState!=null and customerState=='5'.toString()">
and rc.status = '11'
</if>
控制台打印sql为
FROM
room_clues rc
LEFT JOIN cloud_user cu ON cu.id = rc.user_id
LEFT JOIN cartype ct ON ct.id = rc.carTypeId
LEFT JOIN cartype ct1 ON ct1.id = rc.deal_car_type_id
WHERE
1 = 1
AND rc.shareId = ?