数据库中表的字段如下:
public class User {
private int id;
private String name;
private String password;
}
运行结果:
解决方法:
- 起别名
select id,name,pwd as password from mybatis.user where id=#{id}
2.ResultMap(结果集映射)
原来结果:id name pwd
实体类:id name password
<!--结果集映射-->
<resultMap id="map" type="com.qi.pojo.User">
<!-- column数据库中的字段,propertie实体类中的属性-->
<!-- <result column="id" property="id"/>-->
<!-- <result column="name" property="name"/>-->
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserById" resultMap="map">
select * from mybatis.user where id=#{id}
</select>
运行结果:
ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。