数据库字段和实体类字段名称不同,导致有些字段无法映射:User{id=1, name='zsq', password='null'}
select * from `user` where id = #{uid} select id,name,psw from `user` where id = #{uid}
解决办法:给对应字段添加别名
<select id="getOneUserById" parameterType="int" resultType="User"> select id,name,pws as password from `user` where id = #{uid} </select>
User{id=1, name='zsq', password='123456'}
resultMap
元素是 MyBatis 中最重要最强大的元素。
<resultMap id="userResultMap" type="user"> <!--column:对应数据库字段名称,property:对应实体类字段名称--> <id column="id" property="id"/> <result column="name" property="name"/> <result column="pws" property="password"/> </resultMap> <select id="getOneUserById" parameterType="int" resultMap="userResultMap"> select * from `user` where id = #{uid} </select>
User{id=1, name='zsq', password='123456'}