zoukankan      html  css  js  c++  java
  • 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集合中。

  • 相关阅读:
    第三节 单因素方差分析
    第四十一节 ORM介绍和用元类实现
    第四十节 通过type创建复杂的类,元类应用
    第二节 检验方法使用条件考察
    HDFS HA误删namenode后报错Nameservice testCluster has no SecondaryNameNode or High-Availability partner的恢复
    spark sql cache时发现的空字符串问题
    centos7环境下ELK部署之elasticsearch
    CDH升级 5.7.5 --> 5.13.3(tar包方式)
    CDH部署(以5.7.5为例)
    人生苦短,Let's Go
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10466616.html
Copyright © 2011-2022 走看看