属性名和字段名不一致
1. 问题
环境:新建一个项目,将之前的项目拷贝过来
1、查看之前的数据库的字段名
新建一个项目,拷贝之前的,测试实体类字段不一致的情况
测试出现问题
// select * from user where id = #{id}
// 类型处理器
// select id,name,pwd from user where id = #{id}
解决方法:
起别名
<select id="getUserById" parameterType="int" resultType="User">
select id,name,pwd as password from mybatis.user where id=#{id}
</select>
结果集映射
2 、resultMap
结果集映射
id name pwd
id name password
resultMap 元素是 MyBatis 中最重要最强大的元素。
ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
ResultMap 的优秀之处——你完全可以不用显式地配置它们。
<!-- 结果集映射-->
<resultMap id="UserMap" type="User">
<!-- column数据库中的字段,property是对应实体中的属性 -->
<result column="id" property="id" />
<result column="name" property="name" />
<result column="pwd" property="password" />
</resultMap>
<select id="getUserById" parameterType="int" resultMap="UserMap">
select * from mybatis.user where id=#{id}
</select>