MyBatis03:ResultMap和分页
搭建项目
当数据库字段和实体类属性名不一致时
public class User {
private int id;
private String name;
private String password;
//get\set\有参无参构造
}
查询结果为null
sql映射文件中
<select id="selectUser" resultType="com.wgg.pojo.User">
select * from user
</select>
相当于
select id,name,pwd from user
- mybatis会根据这些查询的列名(会将列名转化为小写,数据库不区分大小写) , 去对应的实体类中查找相应列名的set方法设值 , 由于找不到setPwd() , 所以password返回null ; 【自动映射】
使用结果集映射解决–>ResultMap
UserMapper.xml
<mapper namespace="com.wgg.dao.UserMapper">
<select id="selectUser" resultMap="UserMap">
select * from user
</select>
<resultMap id="UserMap" type="user">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!-- column是数据库表的列名 , property是对应实体类的属性名 -->
<result column="pwd" property="password"/>
</resultMap