在使用ibatis做数据访问层时,有时会出现Error setting a property: IllegalArgumentException 异常,
该异常出现的一个原因是因为数据库中有null字段,尤其是非字符串字段,比如int类型,ibatis会使用Domain对象的set方法将数据库拿出来的值赋给Domain对象的属性,Domain对象属性为int时,将数据库中拿出的null赋值给int就会抛出这种错误,解决办法:
1. 将数据库类似int类型的字段赋上一个默认值,比如说0等。(当然不影响业务逻辑)
2. 将Domain对象的属性int等改为对应的装箱类型,比如Integer等,因为Integer可以为null,所以可以赋值。
3. 如果属性实在不想使用装箱类型,我们知道ibatis是依靠Domain的set方法进行赋值,所以可以改写set方法,比如:
public void setYear(Integer year) {
if (year != null) {
this.year = year;
}
}