7.ResulyMap结果映射

问题描述:如果数据库查询出的字段名称和实体类的字段名称对应不上,会导致查询出的为空

解决办法
    1.更改sql中的查询字段名称,起个别名如:(暴力直接,不推荐使用)
        select id,name as person_name,age,messeg from person
    2.使用resultmap(推荐使用)
    具体示例如下:mapper.xml中的写法
        <mapper namespace="cn.com.wmd.dao.PersonMapper">
            重点2:使用resultMap标签,id是自定义的resultMap id,和下面的对应,type是对应的实体类,此处使用的是实体类别名,实体类别名参考之前笔记
            <resultMap id="personResultMap" type="wmd">
                重点3:使用result标签对应数据库表中字段和实体类字段的不一样处
                column代表的是数据库字段,property代表的是实体类字段
                <result column="name" property="person_name"/>
            </resultMap>
            重点1:返回的结果集是resultMap="自定义的结果映射id"
            <select id="getPersonList" resultMap="personResultMap">
                select * from person
            </select>
        </mapper>

 

posted @ 2022-05-13 19:45  努力的达子  阅读(31)  评论(0编辑  收藏  举报