mybatis_04 resultType和resultMap区别

 

resultType

使用resultType进行结果映射时,查询的列名和映射的pojo属性名完全一致,该列才能映射成功。

如果查询的列名和映射的pojo属性名全部不一致,则不会创建pojo对象;

如果查询的列名和映射的pojo属性名有一个一致,就会创建pojo对象。

   <select id="findOrderExtbyId" parameterType="int" resultType="com.ahd.model.OrderExt">
    select
      o.*,u.username,u.address
    from
      `user` u,orders o
    where u.id=o.user_id
    and u.id=#{id}
   </select>

 

 

输出简单类型

当输出结果只有一列时,可以使用ResultType指定简单类型作为输出结果类型。

(解释:输出的为count…和查询内容无关的,可以使用ResultType)

 

 

resultMap

如果查询出来的列名和属性名不一致,通过定义一个resultMap将列名和pojo属性名之间作一个映射关系。

1、  定义resultMap

2、  使用resultMap作为statement的输出映射类型

<resultMap id="userByresultmap" type="user">
    <id property="id" column="id_"></id>
    <result property="username" column="username_"></result>
    <result property="birthday" column="birthday_"></result>
    <result property="sex" column="sex_"></result>
    <result property="address" column="address_"></result>
</resultMap>
<select id="findUserByResultMap" parameterType="userQueryVO" resultMap="userByresultmap">
    select
      id id_,
      username username_,
      birthday birthday_,
      sex sex_,
      address address_
    from user where username like "%${user.username}%"
</select>

 

resultType:将查询结果按照sql列名pojo属性名一致性映射到pojo中。

resultMap:使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求)。

association:将关联查询信息映射到一个pojo对象中。

collection:将关联查询信息映射到一个list集合中。

 

posted @ 2019-03-03 17:45  爱华顿g  阅读(631)  评论(0编辑  收藏  举报