字段名与属性名不一致,会造成属性值为null
1、默认行为根据名称相同自动匹配
When auto-mapping results MyBatis will get the column name and look for a** property** with the same name ignoring case.
2、一般开启驼峰命名
Usually database columns are named using uppercase letters and underscores between words and java properties often follow the camelcase naming covention. To enable the auto-mapping between them set the setting mapUnderscoreToCamelCase to true.
例如
class User{
private String userName;
getset方法;
}
user表中字段名为:user_name
解决思路:
1、select user_name AS userName,起别名。
优点:最简单,易理解
缺点:如果有N个查询,每个查询写一遍别名,需要N遍,有耐心你就写,等到字段名有修改,你在一处一处去修改,加班开始哈哈哈。
2、添加类属性名与数据库字段名映射关系。(column为表中字段名)
优点:映射关系可以复用
缺点:不能一眼看出对应的实体类,需要先看resultMap,在找到其type属性