resultMap 元素是 MyBatis 中最重要最强大的元素
ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
属性名和字段名不一致
测试方法返回结果:
User{id=1, name='zy', password='null'}
User{id=3, name='张六', password='null'}
User{id=4, name='王五', password='null'}
User{id=6, name='阿里', password='null'}
User{id=7, name='王三', password='null'}
password 都为null
解决方法:
(1) 起别名
    <select id="getUserList" resultType="hello">
        select id,name,psw as password from user
    </select>
(2) resultMap 结果集映射
<mapper namespace="com.zy.dao.UserMapper">
    <resultMap id="getUserMap" type="hello">
     只映射不一样的字段属性
        <result column="psw" property="password"/>
    </resultMap>
    <select id="getUserList" resultMap="getUserMap">
        select * from user
    </select>
</mapper>
<resultMap> 的id 要与 <select>中resultMap 的值 对应
<resultMap> type就是 实体类别名或者映射的实体类
<result> 中column就是表中字段名 property是实体类属性名
启动:
User{id=1, name='zy', password='123456'}
User{id=3, name='张六', password='595959'}
User{id=4, name='王五', password='7789'}
User{id=6, name='阿里', password='258789'}
User{id=7, name='王三', password='7789'}
                    
                
                
            
        
浙公网安备 33010602011771号