Mybatis的resultMap结果集映射
一、解决问题
解决数据库中字段名和属性名不一致的情况
比如说:
public class User {
private Integer id;
private String name;
private String password;
}
<select id="selectUserById" resultMap="User">
select id , name , pwd from user where id = #{id}
</select>
如果这样去查询操作的话,User的password就会对应不上,会为null
解决方案1
在sql语句中指定列名的别名
<select id="selectUserById" resultMap="User">
select id , name , pwd as password from user where id = #{id}
</select>
解决方案2
- 使用ResultMap映射
<resultMap id="UserMap" type="User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="selectUserById" resultMap="UserMap">
select id , name , pwd from user where id = #{id}
</select>
和中最基本的映射内容
他们可以将查询的结果中一个对应字段的值映射到返回结果对象的属性。
返回结果对象就是 标签中的type
二、ResultMap的嵌套查询使用
给定Teacher和Student类
public class