mybatis高级映射


高级映射本质来说是多个表的联合查询的过程。

订单数据模型分析思路

数据表:
用户表user:记录了购买商品的用户信息。
订单表orders:记录了用户所创建的订单(购买商品的订单)
订单明细表orderdetail:记录了订单的详细信息即购买商品的信息。
商品表items:记录了商品信息。

数据模型分析:
在这里插入图片描述
表与表之间的业务关系
user和orders:(一对多关系)
user->orders:一个用户可以创建多个订单,一对多。
orders->user:一个订单只能由一个用户创建,一对一。

orders和orderdetail:(一对多关系)
orders->orderdetail:一个订单可以包括多个订单明细 一对多。
orderdetail->orders:一个订单明细只能包括在一个订单中 一对一。

一对一映射

需求:通过订单号来查询订单及用户信息

select * from orders o,user u where o.user_id = u.id and u.number=?

分析:通过分析,orders表示主表,user表是辅助表
在Order的pojo类上添加上User属性

public class Orders213 {
    private Integer id;
    private Integer userId;
    private String number;
    private Date createTime;
    private String note;
    
    //新增属性
    private User213 user;
  //省略了getter和setter方法
}

接口方法:

Orders213 selectOrdersByNumber(String number);

返回结果通过resultType返回:

    <select id="selectOrdersByNumber" parameterType="string" resultType="com.tulun.Mybatis.pojo.Orders213">
        select o.*,u.id "user.id",u.username "user.username",u.sex "user.sex",u.address "user.address" from orders o,user u where o.user_id = u.id and o.number= #{number}
    </select>

使用resultMap配置一对一映射:

    <resultMap id="orderUserMap" type="com.tulun.Mybatis.pojo.Orders213">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="number" column="number"/>
        <result property="createTime" column="createtime"/>
        <result property="note" column="note"/>
        
        <!--user属性配置-->
        <result property="user.id" column="u_id"/>
        <result property="user.username" column="u_username"/>
        <result property="user.sex" column="u_sex"/>
        <result property="user.address" column="u_address"/>
    </resultMap>


    <select id="selectOrdersByNumber" parameterType="string" resultMap="orderUserMap">
        select o.*,u.id u_id,u.username u_username,u.sex u_sex,u.address u_address from orders o,user u where o.user_id = u.id and o.number= #{number}
    </select>

使用resultMap中提供的association配置一对一关系。
association:用于配置关联查询的单个对象。
property属性:要映射到pojo类中的对象的属性。
javaType:要映射的pojo属性的全限定名。
columnPrefix:针对数据库相同前缀的别名。

    <resultMap id="orderUserMap1" type="com.tulun.Mybatis.pojo.Orders213">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="number" column="number"/>
        <result property="createTime" column="createtime"/>
        <result property="note" column="note"/>
        <association property="user" columnPrefix="u_" javaType="com.tulun.Mybatis.pojo.User213">
            <result property="id" column="id"/>
            <result property="username" column="username"/>
            <result property="sex" column="sex"/>
            <result property="address" column="address"/>
        </association> 
    </resultMap>

优化2:

extends继承自主类,resultMap使用一个已经存在的map类
<resultMap id="orderUserMap2" extends="orderMap" type="com.tulun.Mybatis.pojo.Orders213">
        <association property="user" columnPrefix="u_" resultMap="com.tulun.Mybatis.mapper.User213Mapper.userMap"/>
    </resultMap>

注:一对一映射使用resultMap下的association来配置映射关系。

一对多映射

将关联的列映射为list。

需求:查询用户的订单信息

select * from user u,orders o where u.id=o.user_id and u.id=?

分析:通过用户id查询用户订单信息,用户在user表,称为主表,关联的是orders订单表,订单表是辅助表。

使用resultMap下提供的Collection配置来配置一对多关系。
映射pojo类配置:

public class User213 {
    private Integer id;
    private String username;
    private String sex;
    private String  address;

    //添加orders属性
    private List<Orders213> orders;
   //省略getter和setter方法
}

配置文件:

<resultMap id="userMap" type="com.tulun.Mybatis.pojo.User213">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>
        <collection property="orders" ofType="com.tulun.Mybatis.pojo.Orders213" columnPrefix="o_" >
            <id property="id" column="id"/>
            <result property="userId" column="user_id"/>
            <result property="number" column="number"/>
            <result property="createTime" column="createtime"/>
            <result property="note" column="note"/>
        </collection>
    </resultMap>

    <select id="selectUserById" parameterType="int" resultMap="userMap">
        select u.*,o.id o_id,o.user_id o_user_id,o.number o_number
         ,o.createtime o_createtime,o.note o_note
         from user u,orders o where u.id=o.user_id and u.id = #{id}
    </select>

使用resultMap标签下提供的Collection配置一对一关系。
collection:对关联查询到的多条记录映射到集合对象中。
property属性:将关联查询到的多条记录映射到orders属性中。
ofType属性:指定映射的集合属性中的类型全限定名。

多对多映射

需求:查询用户及用户的商品信息。

分析:查询用户是主表:user表,关联表,订单orders表,订单明细orderdetails表和商品表items。
将结果映射到user属性中,在user类上添加订单列表属性List ,将用户创建的额订单映射到orderlist中。
在orders添加订单明细列表属性List,将订单明细映射到OrderDetails中。
在OrderDetails类中添加商品属性Item类型,将商品映射到item上。

resultMap和ResultType

resultType

将数据库中的数据映射到pojo类上,自动将字段完成映射,减少映射编码,对于数据库字段和pojo属性不一致时是无法完成字段映射,如果所有字段都不匹配,则无法映射pojo对象出来。
1、映射自动化,如果字段较少且保保持一致优先选择resultType。
2、对于多表映射,只能进行一对一映射。

resultMap

resultmap是手动显性实现映射,是可以处理数据库字段和pojo类字段不一致的问题。
还可以使用association和Collection完成一对一和一对多高级映射。
association:
作用:将关联的查询信息映射到一个pojo类中。
Collection:
将关联的查询信息映射到一个list集合中。

延时加载

延时加载的配置
默认为开启延时加载,需要在配置文件中给定延时加载配置setting。
在这里插入图片描述

    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

延时加载主要针对的是关联表的查询,属于高级映射(使用association和Collection来具备延时加载功能)。

延时加载在association和Collection都有select属性:指定延时加载需要执行的StatementID

    <resultMap id="orderUserMap3" extends="orderMap" type="com.tulun.Mybatis.pojo.Orders213">
        <association property="user" column="id={user_id}" select="com.tulun.Mybatis.mapper.User213Mapper.selectUserById"/>
    </resultMap>
    <select id="selectOrdersByNumber" parameterType="string" resultMap="orderUserMap3">
              select * from orders where number = #{number}
    </select>
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值