mapper.xml :
<select id="count" parameterType="com.pinyu.system.web.page.Page" resultType="java.lang.Integer">
select count(m.id) from hr_push_msg_model as m
<where>
<if test="page.keyword != null and page.keyword != ''">
m.text like '%${page.keyword}%'
</if>
<if test="page.entity != null">
<if test="page.entity.text != null and page.entity.text != ''">
and m.text like '%${page.entity.text}%'
</if>
<if test="page.entity.title != null and page.entity.title != ''">
and m.title like '%${page.entity.title}%'
</if>
<if test="page.entity.state != null and page.entity.state != ''">
and m.state = #{page.entity.state}
</if>
<if test="page.entity.type != null and page.entity.type != ''">
and m.type = #{page.entity.type}
</if>
</if>
</where>
</select>
比如
<if test="page.entity.state != null and page.entity.state != ''">
and m.state = #{page.entity.state}
</if>
当state这个值为0的时候,mybatis为默认为空字符串"",所以如果状态这种类似的场景有0值得,查询就不要加上xxxx!=""这种。或者or xxx==0
代码示例:
1、
<if test="page.entity.state != null">
and m.state = #{page.entity.state}
</if>
2、
<if test="page.entity.state != null and page.entity.state != '' or page.entity.state==0">
and m.state = #{page.entity.state}
</if>