实体类属性名与数据库字段名如下所示
此时我们进行查询:
可以看到PWD属性为空
<!-- 全字段查询语句本质上如下: select id,name,password from mybatis.user where id = #{ID}; 数据库返回password字段的数据并查找password属性进行赋值 但实体类中并没有password属性 故会发生属性名与字段名不一致时查询失败的问题 -->
解决方式:
一.直接在sql语句中设置别名
<!--根据ID查询-->
<select id="getUserByID" resultType="cn.alan.POJO.User" parameterType="int">
select id,name,password as pwd from mybatis.user where id = #{ID};
</select>
二.配置resultMap
<!--column为数据库中的字段名 property为实体类中的属性名-->
<resultMap id="rMap" type="cn.alan.POJO.User">
<result property="PWD" column="password"/>
</resultMap>
<!--根据ID查询-->
<select id="getUserByID" resultMap="rMap" parameterType="int">
select id,name,password from mybatis.user where id = #{ID};
</select>
运行效果