笔记:mybatis中where为啥加1=1
实例:<select id="getCarByCarnoLast" resultType="com.ho.railwaystack.modular.BMS.entity.Car"> select b.* from t_car b left join t_car_rfid a on a.carno=b.carno where 1=1 <if test="carno != null and carno != ''"> and b.carno like CONCAT ('%',#{carno}) </if></select>
因为在没有加的情况下但if条件不成立时,where后就会直接为and
如上面那条语句,但if不成立,sql语句就会变成:
select b.* from t_car b left join t_car_rfid a on a.carno=b.carno where and b.carno like CONCAT ('%',#{carno})
这样就报错
参考:为啥加1=1添加链接描述