问题发生背景:
1. 有一个对象A,属性命名中包含下划线。
2. 该属性对应数据库的字段命名为 sh_count。
public class A{
private String sh_count;
public Integer getSh_count() {
return sh_count;
}
public void setSh_count(Integer sh_count) {
this.sh_count = sh_count;
}
}
问题发生现象:
数据库中有一条符合查询条件的数据,查询返回List时,aList.size()=1,但是aList.get(0)报NullPoint异常。
List<A> aList=aService.find();
于是将返回值改为A对象试一下,a的值为NULL。
A a=aService.find();
解决方法:
将对象属性命名改为驼峰命名法,问题解决。
public class A{
private String shCount;
public Integer getShCount() {
return shCount;
}
public void setShCount(Integer shCount) {
this.shCount = shCount;
}
}
问题原因猜测:
mybatis查询结果与Java对象属性的映射规则问题,注意对象属性使用驼峰命名法,避免使用下划线命名对象属性可解决这一问题。具体原因未详查。

本文介绍了一个关于MyBatis映射时遇到的问题:当Java对象的属性名包含下划线时,从数据库查询数据会出现空指针异常。通过调整属性命名规则为驼峰命名法解决了该问题。
1127

被折叠的 条评论
为什么被折叠?



