resultMap 和 resultType

版权声明:@渔闻520 https://blog.csdn.net/weixin_41060905/article/details/86628933

返回的结果集可以用resultType也可以用resultMap。

使用resultType:

<select id="getRole" parameterType="long" resultType="role">
        select id,role_name as roleName,note from t_role where id=#{id}
    </select>

使用resultMap:

<!--下面使用resultMap-->
    <resultMap id="roleResultMap" type="role">
        <id property="id" column="id"></id>
        <result property="roleName" column="role_name"></result>
        <result property="note" column="note"></result>
    </resultMap>
    <select id="getRole" parameterType="long" resultMap="roleResultMap">
        select id,role_name,note from t_role where id=#{id}
    </select>

其中,id是对应着的主键,而下面的result的属性是对应的pojo对象的属性。resultMap的type可以是完整的类名也可以是像role这样的别名(需要在MyBatis中配置).column是对应的数据库中的名称。这样就将数据库和pojo对象映射在一起了。

从上面的代码中可以看到二者的select语句的不同。

猜你喜欢

转载自blog.csdn.net/weixin_41060905/article/details/86628933