世界上并没有完美的程序,但我们并不因此而沮丧,因为写程序本来就是一个不断追求完美的过程。 ——摘自周志明

mybaties返回值对对象,字段名与属性名不匹配问题

字段名与属性名不一致,会造成属性值为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;
get\set方法;
}
user表中字段名为:user_name
解决思路:
1、select user_name AS userName,起别名。
优点:最简单,易理解
缺点:如果有N个查询,每个查询写一遍别名,需要N遍,有耐心你就写,等到字段名有修改,你在一处一处去修改,加班开始哈哈哈。
2、添加类属性名与数据库字段名映射关系。(column为表中字段名)



优点:映射关系可以复用
缺点:不能一眼看出对应的实体类,需要先看resultMap,在找到其type属性

posted @ 2021-01-14 22:34  白杯与咖啡  阅读(398)  评论(0编辑  收藏  举报