5.解决属性名和字段名不一致的问题
实例程序mybatis-03包
1.起别名解决问题
就是java程序的实体类的命名如password和数据库表的字段命名如pwd不一样。
在查询时会出现返回为null,这是因为在查询过程中,找不到实体类的属性值,在数据库表的字段上,没有匹配的。
-
解决方法
-
起别名
select * from mybatis.user where id={#id}
select id ,name,pwd as password from mybatis.user where id={#id}
-
通过把pwd转化为password来解决。
2.resultMap结果集映射
column是数据库中的列,property是实体类的字段。
<!--结果集映射-->
<resultMap id="UserMap