一般使用mybati的if标签进行动态sql拼接是,要进行字段判空,会进行这样判断
<if test="username != null and username != ''">
and username=#{username}
</if >
但是当实体类里面包含另一个实体类时
public class user{
private String username;
private Dept dept;
}
要使用dept的字段作为查询条件时,要先判断dept是否为空,再判断dept的属性
<if test="user.dept!=null ">
<if test="user.dept.deptname != null and user.dept.deptname != ''">
and deptname like concat('%',#{user.dept.deptname} , '%')
</if>
</if>
这样就不会报错