1.设置association
<association property="configCustomer" column="customer_id" select="com.hdax.mapper.ConfigCustomerMapper.queryById" javaType="ConfigCustomer" ></association>
2.数据库中表设计id允许0值,但这样就无法查到任何数据,也就是null
3.layui表格,定义方法获取封装对象的字段值
{field: 'theCustomerName', width: 220, title: '客户',},
4.pojo中定义方法
//用于页面展示的方法
public String getTheCustomerName(){
return configCustomer.getCustomer_name();
}
5.此时运行时报错
Statement returned more than one row, where no more than one was expected
6.与ssm框架和mybatis无关系,报错原因是因为页面中调用到的方法:getTheCustomerName(),该方法无非空判断,当查询数据库返回null值的customer值时,调用该方法会报错如上,很奇怪为什么不是空指针异常,而且报错指向mybatis,我找了很久才找到报错原因
7.解决方式:第一种是添加非空判断,如果configCustomer为空则返回其他值,杜绝空指针的可能性;第二种是在页面中使用三元表达式,示例:
{field: 'customer_name', width: 220, title: '客户',
templet(d){
return d != null ? d.configCustomer != null ? d.configCustomer.customer_name : '无' : '无'
}},