比如说有一个user表和一个User类,两者一一映射,user表中有一个字段为pwd,User类中有一个属性为password,如果在这样的情况下在mybatis 中运行,最后得到的User对象其中的password=null,这就是因为数据库字段不匹配的原因,如何解决?
在mapper.xml中select等标签有一个属性叫做resultMap,可以进行名字之间的一个映射,参照下方代码,但是这只适合于简单的。
<mapper namespace="com.li.dao.UserMapper">
<resultMap id="UserMap" type="user">
<result property="password" column="pwd"/>
</resultMap>
<select id="getUserById" resultMap="UserMap" >
select * from USER where id=#{id};
</select>
</mapper>