异常
org.apache.ibatis.reflection.ReflectionException:There is no getter for property named ‘xxx’ in ‘class xxx’
原因: 一般是 动态sql中 if test= " " 中出现参数不对应得情况
- mapper文件中入参参数与bean(model)中属性不对应
- 如果不是1中得原因,那就是mybatis部分版本出现得问题,需要将入参改为mybatis内置参数 _parameter即可
错误还原:
/**
* 员工实体
* @author Jerry
* @create 2019-08-25 16:30
*/
public class Employee {
private Integer empId;
private String empName;
private String email;
private String gender;
private String dId;
...get 省略
...set 省略
}
数据库字段:
emp_id emp_name gender email d_id
/**
* mapper接口
* @author Jerry
* @create 2019-08-25 16:30
*/
public interface EmployeeDynamicSqlMapper {
/**
* 根据条件查询员工信息if
* @param employee
* @return
*/
List<Employee> getEmpsByConditionIf(Employee employee);
}
mapper.xml文件中得方法
<select id="getEmpsByConditionIf" resultType="Employee" parameterType="Employee">
select emp_id empId,emp_name empName,gender,email,d_id dId from tbl_emp
<where>
<!--这里test中 如果empId写错,就会报上述异常-->
<if test="empId!=null">
emp_id=#{empId}
</if>
<if test="empName!=null and empName!=''">
and emp_name like #{empName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<if test="gender == 'M' and gender == 'WM' ">
and gender=#{gender}
</if>
<if test="dId != null">
and d_id=#{dId}
</if>
</where>
</select>