• mybatis中实现一对一,一对多查询


    在实际的开发中我们经常用到的是一对一查询和一对多查询。而多对多的实现是通过中间来实现,这里就没有给出来了微笑

    比如:

    订单和用户是一对一的关系(一个订单只能对应一个用户)

    订单和订单明细是一对多的关系(一个订单可以对应多个订单明细)

    程序结构图:


    所采用的mybatis版本3.2.3

    对应的jar包如下图所示


    对应库表的信息

    用户表:

    订单表:

    订单明细表:


    对应的实体类的信息

    用户实体类;

    用户扩展实体类:

    订单实体类:

    订单明细实体类:


    ==============================【一对一(resultType方式) begin】==============================

    OrderContent.xml

    <!-- 一对一: 通过查询商品,查询出对应的用户的信息 -->
    <!-- 方式1:resultType -->
    <select id="findOrderAndRelateUserByResultType" resultType="com.ithema.mybatis.po.OrderVo">
    SELECT
    orders.id,
    orders.order_number,
    orders.user_id,
    user.username,
    user.address
    FROM orders,
    USER
    WHERE orders.user_id = user.id
    </select>

    OrderContent.java

    /**
    * resultType的方式实现通过订单查询用户(一对一)
    * @return
    */
    List<OrderVo> findOrderAndRelateUserByResultType();

    查询结果:

    数据库结果:

    ==============================【一对一(resultType方式) end】==============================


    ==============================【一对一(resultMap方式) begin】==============================

    OrderContent.xml

    <!-- 方式2:resultMap -->
    <!-- 定义一个resultMap,用于映射订单信息和用户信息 id:在同一个命名空间下唯一 
    type:要映射到的java类型,在Orders实体中定义一个User属性,用于映射User的信息 -->
    <resultMap type="com.ithema.mybatis.po.Orders" id="orderAndRelateUserMap">
    <id column="id" property="id" />
    <result column="order_number" property="order_number" />
    <result column="user_id" property="user_id" />


    <!-- association标签用于实现一对一映射 property:将关联的信息映射到Orders对象中的哪个属性中 javaType:映射属性的java类型 -->
    <association property="user" javaType="com.ithema.mybatis.po.User">
    <!-- id:关联的用户信息的唯一约束 property:id指定的映射关联的对象的那个属性 -->
    <id column="user_id" property="id" />
    <result column="username" property="username" />
    <result column="address" property="address" />
    </association>
    </resultMap>


    <select id="findOrderAndRelateUserByResultMap" resultMap="orderAndRelateUserMap">
    SELECT
    orders.id,
    orders.order_number,
    orders.user_id,
    user.username,
    user.address
    FROM orders,
    USER
    WHERE orders.user_id = user.id
    </select>

    OrderContent.java

    /**
    * resultMap的方式实现通过订单查询用户(一对一)
    * @return
    */
    List<Orders> findOrderAndRelateUserByResultMap();


    测试结果:




    ==============================【一对一(resultMap方式) end】==============================


    ==============================【一对多(resultMap方式) begin】==============================


    OrderContent.xml

    <!-- 一对多: 通过查询订单查询对应的订单明细信息 
    一对多的查询只能通过resultMap的方式实现
    在Orders对象中加入一个list集合表示订单明细的信息
    -->
    <!-- 定义一个订单和订单明细的一对多的映射Map -->
    <resultMap type="com.ithema.mybatis.po.Orders" id="orderAndOrderDetailMap">
    <!-- 订单对象的唯一标识 -->
    <id column="id" property="id"/>
    <result column="order_number" property="order_number"/>

    <!-- 一对多的映射要使用collection标签 
    property:将明细信息映射到哪个集合对象中
    ofType:所要映射的集合对象的类型
    -->
    <collection property="orderDetails" ofType="com.ithema.mybatis.po.OrderDetail">
    <id column="orderdetail_id" property="id"/>
    <result column="item_num" property="item_num"/>
    <result column="item_price" property="item_price"/>
    </collection>
    </resultMap>

    <select id="findOrderAndOrderDetail" resultMap="orderAndOrderDetailMap">
    SELECT
    orders.id,
    orders.order_number,
    orderdetail.id orderdetail_id,
    orderdetail.item_num,
    orderdetail.item_price
    FROM orders,
    orderdetail
    WHERE orders.id = orderdetail.orders_id
    </select>


    OrderContent.java

    /**
    * resultMap的方式实现通过订单查询对应的订单明细(一对多)
    * @return
    */
    List<Orders> findOrderAndOrderDetail();


    测试结果:



    数据库结果:

    ==============================【一对多(resultMap方式) end】=============================







  • 相关阅读:
    夜游遂宁滨江路
    易中天讲座十句人生感悟(发人深省,耐人寻味)
    遥望死海
    一直被忽略的成功之道:勤快并非优点,成功需要懒惰
    给别人以宽容,给自己以信心
    合理支配“财富”:经理人运用时间的12种典型模式
    三月的清晨
    学习的三重境界(想成功的人都不可错过)
    上班
    持续开发你的事业智慧:企业家保持冲锋势头的路径
  • 原文地址:https://www.cnblogs.com/chunguang-yao/p/10666441.html
Copyright © 2020-2023  润新知