• mybatis3.2.7应用_高级映射(一对一、一对多、多对多)


    1. 一对一查询

    需求:查询订单信息,关联查询创建订单的用户信息

    1.1 使用resultType实现

    1.1.1 sql语句

       确定查询的主表:订单表
           确定查询的关联表:用户表
           关联查询:使用外连接,还是内连接??

            --由于orders表中有一个外键(user_id),通过外键关联查询用户表只能查询出一条记录,可以使用内连接

    select t.*, 
             u.username,  u.sex,  u.address
      from orders t, user u
     where t.user_id = u.id

    1.1.2 创建pojo

         将上边sql查询结果映射到pojo中,pojo中必须包含所有查询列名。
             原始的Orders不能映射全部字段,需要新创建pojo。创建一个pojo继承包括查询字段较多的po类。

    //通过此类映射订单和用户和用户查询的结果,让此类继承包括字段较多的pojo类
    public class OrdersCustom extends Orders{
         //添加用户属性
         //u.username, u.sex, u.address
         private String username;
         private String sex;
         private String address;
    }

    1.1.3 mapper.xml

    <!-- 查询订单,关联查询用户信息 -->
    <select id="findOrderUser" resultType="cn.itcast.mybatis.po.OrdersCustom">
            select t.*, u.username, u.sex, u.address
             from orders t, user u
            where t.user_id = u.id
    </select>

    1.1.4 mapper.java

    public List<OrdersCustom> findOrderUser() throws Exception;

    1.2 使用resultMap实现
    1.2.1 sql语句
          同resultType实现的sql
    1.2.2 使用resultType映射的思路
        使用resultType将查询结果中的订单信息映射到Orders对象中,在Orders对象中添加user属性,将关联查询出来的用户信息映射到Orders对象中user属性中。
        需要Orders类中添加user属性。

    public class Orders{
         
         private Integer id;
         private Integer userId;
         private String number;
         private Date createTime;
         private String note;
         
         //用户信息
        private User user;
    }

    1.2.3 mapper.xml   定义resultMap(一对一使用association来映射用户信息)

        <!-- 订单查询关联查询用户信息 的 resultMap
               将整个查询的结果映射到cn.itcast.mybatis.po.Orders中
        -->
        <resultMap type="cn.itcast.mybatis.po.Orders" id="ordersUserResultMap">
             <!-- 配置映射的订单信息-->
             <!-- id: 指定查询列中的唯一标识,订单信息中的唯一标识,如果有多个列组成唯一标识,配置多个id
                  column: 订单信息中的唯一标识列
                  property:订单信息中的唯一标识列所映射到Orders中的哪个属性-->
             <id column="id" property="id"/>
             <result column="user_id" property="userId"/>
             <result column="number" property="number"/>
             <result column="createTime" property="createTime"/>
             <result column="note" property="note"/>
         
             <!-- 配置映射的关联的用户信息
                  association: 用于映射关联查询单个对象的信息
                  property: 要将关联查询的用户信息映射到Orders中哪个属性
                  javaType: 指定映射到关联用户的pojo类型中-->
             <association property="user" javaType="cn.itcast.mybatis.po.User">
                  <!-- id: 关联查询用户的唯一标识
                       column: 指定唯一标识用户信息的列
                       property:映射到user的哪个属性-->
                  <id column="user_id" property="id"/>
                  <result column="username" property="username"/>
                 <result column="sex" property="sex"/>
                 <result column="address" property="address"/>
              </association>
        </resultMap>        

    1.2.4 mapper.xml   statement

    <!-- 查询订单,关联查询用户信息 -->
    <select id="findOrdersUserResultMap" resultMap="ordersUserResultMap">
         select t.*, u.username, u.sex, u.address
          from orders t, user u
        where t.user_id = u.id
    </select>

    1.2.5 mapper.java

    public List<Orders> findOrdersUserResultMap() throws Exception;

    1.3 resultType 和 resultMap 实现一对一查询小结
          resultType: 使用resultType较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
                     如果没有查询结果的特殊要求,建议使用resultType。
          resultMap:   需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成关联查询映射到pojo属性中。
                     resultMap可以实现延时加载。而resultType无法实现延时加载。

      

        

        

  • 相关阅读:
    CentOS的SSH,Putty配置说明
    关于QString::toWCharArray 无法解析的外部符号
    CentOS最常用命令及快捷键整理
    Ali相关面试题
    C#几个例子[静态构造函数,继承,虚方法]
    SQL 2005 中查询或执行另外的数据库操作的方法
    DataTable Select Top
    SQL中行列转换 Pivot UnPivot
    ASP.NET页面生命周期描述
    Jquery checkbox, select 取值
  • 原文地址:https://www.cnblogs.com/qianbing/p/6978035.html
Copyright © 2020-2023  润新知