• 关联查询


    一对一  嵌套查询

    association标签 嵌套结果方式 常用属性:
    property :对应实体类中的属性名,必填项。
    javaType : 属性对应的 Java 类型 。
    resultMap : 可以直接使用现有的 resultMap ,而不需要在这里配置映射关系。
    columnPrefix :查询列的前缀,配置前缀后,在子标签配置 result 的 column 时可以省略前缀


    1.resultMap可以通过使用extends实现继承关系,简化很多配置工作量;
    2.关联的表查询的类添加前缀是编程的好习惯;
    3.通过添加完整的命名空间,可以引用其他xml文件的resultMap;

    <resultMap id="BaseResultMap" type="TUser">
            <id column="id" property="id" />
            <result column="user_name" property="userName" />
            <result column="real_name" property="realName" />
            <result column="sex" property="sex" />
            <result column="mobile" property="mobile" />
            <result column="email" property="email" />
            <result column="note" property="note" />
     </resultMap>
    <resultMap id="userAndPosition1" extends="BaseResultMap" type="TUser">
    <association property="position" javaType="TPosition" columnPrefix="post_"> <!-- columnPrefix 加前缀-->
          <id column="id" property="id"/>
    <result column="name" property="postName"/>
    <result column="note" property="note"/>
    </association>
    </resultMap>

    <select id="selectUserPosition1" resultMap="userAndPosition1">
    select
    a.id,
    user_name,
    real_name,
    sex,
    mobile,
    email,
    a.note,
    b.id post_id,
    b.post_name,
    b.note post_note
    from t_user a,
    t_position b
    where a.position_id = b.id

    </select>

     一对一  嵌套结果 

      

    <resultMap id="userAndPosition2" extends="BaseResultMap" type="TUser">
            <association property="position" fetchType="lazy"  column="position_id" select="cn.qin.mybatis.mapper.TPositionMapper.selectByPrimaryKey" />
    </resultMap>

    <select id="selectUserPosition2" resultMap="userAndPosition2">
    select
    a.id,
    a.user_name,
    a.real_name,
    a.sex,
    a.mobile,
    a.position_id
    from t_user a
    </select>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">

    select
    <include refid="Base_Column_List" />
    from t_position
    where id = #{id,jdbcType=INTEGER}
    </select>

    “N+1 查询问题” 概括地讲,N+1 查询问题可以是这样引起的:
    你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
    对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。 这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。

    解决办法:使用“fetchType=lazy”并且全局setting进行改善: <setting name="aggressiveLazyLoading" value="false"/>   

    按需加载  需要返回值的时候再去查询 

    一对多  

    <resultMap id="userAndJobs1" extends="BaseResultMap" type="TUser">
            <collection property="jobs"
                ofType="cn.qin.mybatis.entity.TJobHistory" >
                <result column="comp_name" property="compName" jdbcType="VARCHAR" />
                <result column="years" property="years" jdbcType="INTEGER" />
                <result column="title" property="title" jdbcType="VARCHAR" />
            </collection>
    </resultMap>
    <select id="selectUserJobs1" resultMap="userAndJobs1">
    select
    a.id,
    a.user_name,
    a.real_name,
    a.sex,
    a.mobile,
    b.comp_name,
    b.years,
    b.title
    from t_user a,
    t_job_history b
    where a.id = b.user_id

    </select>


      延迟查询 

    <resultMap id="userAndJobs2" extends="BaseResultMap" type="TUser">
    <collection property="jobs" fetchType="lazy" column="id"
    select="cn.qin.mybatis.mapper.TJobHistoryMapper.selectByUserId" />
    </resultMap>

    <select id="selectUserJobs2" resultMap="userAndJobs2"> select a.id, a.user_name, a.real_name, a.sex, a.mobile from t_user a </select>
    <select id="selectByUserId" resultMap="BaseResultMap"  parameterType="java.lang.Integer">
    select
    <include refid="Base_Column_List" />
    from t_job_history
    where user_id = #{userId,jdbcType=INTEGER}
    </select>
    <sql id="Base_Column_List">
    id, user_id, comp_name, years, title
    </sql>


  • 相关阅读:
    [转]MVP+WCF+三层结构搭建项目框架
    PowerDesigner之PDM(物理概念模型)
    atoi 和 itoa
    最短作业优先(SJF)
    TCP 三次握手过程详解
    TCP/UDP,SOCKET,HTTP,FTP 简析
    计算机的OSI和TCP/IP网络模型
    理解数据成员指针、函数成员指针
    虚函数底层原理
    C++多态有哪几种方式?
  • 原文地址:https://www.cnblogs.com/qin1993/p/12017144.html
Copyright © 2020-2023  润新知