where 1 = 1 起到一个占位的作用
<select id="listPage3" resultType="com.qcby.shujia.demo.entity.UserBlog">
select ub.*,u.username,u.head_Img,u.reason as userReason from user_blog ub
left join user u on ub.user_id = u.id
where 1=1 /*如果去掉,下面的条件就直接是where and*/
<if test="userBlog.title != null">
and ub.title like CONCAT('%',#{userBlog.title},'%')
</if>
<if test="userBlog.reason != null">
and ub.reason like CONCAT('%',#{userBlog.reason},'%')
</if>
<if test="userBlog.userReason != null">
and u.reason like CONCAT('%',#{userBlog.userReason},'%')
</if>
</select>
如果将where 1 = 1去掉,则会产生语法错误,所以起到一个占位的作用
用<where>标签可以自动辨别后面的条件
<select id="listPage1" resultType="com.qcby.shujia.demo.entity.UserBlog">
select ub.*,u.username,u.head_Img,u.reason as userReason from user_blog ub
left join user u on ub.user_id = u.id
<where>
<if test="userBlog.title != null">
and ub.title like CONCAT('%',#{userBlog.title},'%')
</if>
<if test="userBlog.reason != null">
and ub.reason like CONCAT('%',#{userBlog.reason},'%')
</if>
<if test="userBlog.userReason != null">
and u.reason like CONCAT('%',#{userBlog.userReason},'%')
</if>
</where>
</select>
Mybatis标签解析时会进行优化,把多余的关键词去掉,如果检测到第一个条件的前面有and则会自动把and去掉