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集合中。

  • 相关阅读:
    界面布美观布局
    登陆界面验证码设置
    很好的JAVESRIPT控件
    c#导入EXCEL数据
    微软:系列课程 >Silverlight for Windows Phone 开发系列课程
    JavaScript动态网页制作宝库
    Silverlight经典教程书籍汇总
    SQL删除表中有重复的记录
    Javascript鼠标事件
    Android系统架构(转)
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10466616.html
Copyright © 2011-2022 走看看