mybatis关联映射的使用!一对一,一对多,多对一,多对多

前言:本篇小刘讲述mybatis关联语句的使用

1. 创建表结构如下:

2. 实体类部分代码:

  • 2.1 Wife类:

public class Wife {
    private Integer id;
    private String name;
    private Integer userId;
。。。。。。。。。。。。。。。
}
  • 2.2 User类:

public class User {
    private Integer id;
    private String username;
    private String hobby;
    private Date birthday;

    /**
     * 一对一,一个user只有一个妻子
     */
    private Wife wife;

    /**
     * 一对多:一个user有多个订单
     */
    private List<Order>orders;
...............................
}
  • 2.3 Order类:

public class Order {
    private Integer id;
    private String orderName;
    private Integer userId;
    private Date orderTime;
    private User user;
.......................
}

3. 接口类

  • 3.1 UserDao

  /**
     * 找到所有的用户即包含一对一,也包含一对多
     * @return
     */
    List<User>findAll();

    /***
     * 找出一对一的妻子
     * @return
     */
    List<User>findAllOne();
  • 3.2 UserMapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjitc.dao.UserDao">
    <resultMap id="users" type="com.zjitc.pojo.entity.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="hobby" column="hobby"/>
        <result property="birthday" column="birthday"/>
        <!--
        一对一:一个用户只有一个妻子,关联查询
        多对一:也采用关联查询
        -->
        <association property="wife" javaType="com.zjitc.pojo.entity.Wife">
            <id property="id" column="wife_id"/>
            <result property="name" column="name"/>
            <result property="userId" column="user_id"/>
        </association>
        <!--
        一对多采用collection left join:一个用户含有多个订单
        多对一:采用关联association
        -->
        <collection property="orders" column="user_id" ofType="com.zjitc.pojo.entity.Order">
            <id column="order_id" property="id"/><!--一对多,主键相同必须用别名-->
            <result property="orderName" column="order_name"/>
            <result property="orderTime" column="order_time"/>
            <result property="userId" column="user_id"/>
        </collection>
    </resultMap>



    <resultMap id="xx" type="com.retailo2o.smc.Do.xx" extends="BaseResultMap">
        <result column="cust_name" jdbcType="VARCHAR" property="customerName"></result>
        <collection property="detailList"
                    select="com.retailo2o.smc.mapper.WsPreDetailMapper.xx"
                    column="{billNumber=bill_number,platformNum=platform_num}"></collection>
    </resultMap>

    <!--
    一对多,left join
    -->
    <select id="findAll" resultMap="users">
        select u.*,o.id order_id,o.order_name,o.order_time,o.user_id
         ,
         w.* from user as u
         left join  `order` as o
         on o.user_id=u.id left join wife w on u.id = w.user_id
    </select>

    <select id="findAllOne" resultMap="users">
        select s.*,w.* from user as s left join wife as w on s.id=w.user_id;
    </select>
</mapper>

3.3 多对一:

public interface OrderDao {
    /**
     * 找到所有的订单
     * @return
     */
    List<Order>findAll();
}

3.4 OrderDaoMapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjitc.dao.OrderDao">
    <resultMap id="order" type="com.zjitc.pojo.entity.Order">
        <id property="id" column="id"/>
        <result property="orderName" column="order_name"/>
        <result property="orderTime" column="order_time"/>
        <result property="userId" column="user_id"/>
        <!--
        多对一和一对一没啥区别,看查询的数据差别
        :association关联查询,多个订单有同一个一个user
        -->
        <association property="user" javaType="com.zjitc.pojo.entity.User">
            <id column="id" property="id"/>
            <result property="username" column="username"/>
            <result property="hobby" column="hobby"/>
            <result property="birthday" column="birthday"/>
        </association>
    </resultMap>
    <select id="findAll" resultMap="order">
      select o.*,u.id,u.username,u.hobby,u.birthday from `order` as o left join user as u on o.user_id=u.id;
    </select>
</mapper>

<resultMap type="com.qiruo.yuejuan.entity.ExamDTO" id="uniteSingleExamResultMap">
       <association property="twoExam" column="{examId=id}" javaType="com.qiruo.yuejuan.entity.ExamDTO" select="queryTwoExamInfo" />
   <collection property="questionNumber" column="{examId=id}" ofType="int" select="queryQuestionNumber"></collection>
   <collection property="succeeNumber" column="{examId=id}" ofType="int" select="querysubSucceeNumber"></collection>
   <!--联考单科时查询所有参与学校考试信息-->
   <collection property="uniteExamList" column="{examId=id,singleSubjectExamIds=singleSubjectExamIds,examNum=examNum}"
            ofType="int" select="queryUniteExamList"></collection>
   <!--联考单科时查询参与学校信息列表-->
   <collection property="schoolList" column="{examId=id,examNum=examNum}"
            ofType="com.qiruo.yuejuan.entity.SchoolVO" select="querySchoolListOfUniteSingleSubject"></collection>
</resultMap>

多对多:一般转换为一对多,多对一的处理方式:

特别注意:

mysql连接的方式:

三种基本表连接方式:

  1. 外连接
  2. 内连接
  3. 交叉连接(笛卡尔积)

外连接:包含左外连接(left join或者left out join),右外连接(right join或者right out join),全外连接(full join或者full out join)

 左外连接包括left join左表所有行。假设左表中某行在右表没有匹配。则结果中相应行右表的部分所有为空(NULL).
注:此时我们不能说结果的行数等于左表数据的行数。

 右外连接包括right join右表所有行,假设左表中某行在右表没有匹配,则结果中相应左表的部分所有为空(NULL)。
注:相同此时我们不能说结果的行数等于右表的行数。

全然外连接包括full join左右两表中所有的行,假设右表中某行在左表中没有匹配,则结果中相应行右表的部分所有为空(NULL),假设左表中某行在右表中没有匹配,则结果中相应行左表的部分所有为空(NULL)。

内连接:join或者inner join

inner join 是比較运算符,仅仅返回符合条件的行。

交叉连接:cross join

概念:没有 WHERE 子句的交叉联接将产生连接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。

结果和inner join所看到的运行结果一样。

如有不解,请加java爱好群大家交流:852665736;群里都是热心好客的小伙伴,大家一同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mybatis是一款优秀的ORM框架,支持一对一一对多、多对一、多对多关联映射。其中,一对多关联映射是指一个实体类中包含多个另一个实体类的对象。下面是一对多关联映射的实现方法: 1.在主实体类中定义一个包含从实体类对象的List集合属性。 2.在主实体类对应的Mapper.xml文件中,使用<collection>标签来映射从实体类对象的List集合属性。 3.在从实体类对应的Mapper.xml文件中,使用<association>标签来映射主实体类对象。 具体实现可以参考以下代码: 主实体类User: ``` public class User { private Integer id; private String username; private List<Order> orders; //getter和setter方法 } ``` 从实体类Order: ``` public class Order { private Integer id; private String orderNo; private User user; //getter和setter方法 } ``` 主实体类User对应的Mapper.xml文件: ``` <mapper namespace="com.biem.mapper.UsersMapper"> <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> </collection> </resultMap> <select id="getUserById" resultMap="userMap"> select * from user where id=#{id} </select> </mapper> ``` 从实体类Order对应的Mapper.xml文件: ``` <mapper namespace="com.biem.mapper.OrdersMapper"> <resultMap id="orderMap" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <association property="user" javaType="User"> <id property="id" column="user_id"/> <result property="username" column="username"/> </association> </resultMap> <select id="getOrderById" resultMap="orderMap"> select * from order where id=#{id} </select> </mapper> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值