美文网首页
MyBatis7-一对多查询&对多对查询

MyBatis7-一对多查询&对多对查询

作者: 我相信你爱过gg | 来源:发表于2017-03-19 18:59 被阅读33次

一对一查询

    <resultMap id="asdas" type="cc.ibadboy.mybatis.entity.Teacher">
        <!-- 关联查询出来的信息到集合中
             property: 将关联出来的信息映射到那个属性.
             ofType: 指定集合的泛型.
         -->
        <collection property="orders" ofType="cc.ibadboy.mybatis.entity.TeacherClass">
            <!--
                 column: Teacher表的外键
                 property: TeacherClass表的主键.
             -->
            <id column="tc_id" property="tcid"/>

            <!-- 设置其它属性与表字段进行关联 -->
            <result column="" property=""/>
        </collection>
    </resultMap>

多对多查询

<resultMap type="mybatis.po.User" id="UserAndItemsResultMap">
    <!-- 用户信息 -->
    <id column="user_id" property="id"/>
    <result column="username" property="username"/>
    <result column="sex" property="sex"/>
    <result column="address" property="address"/>
    <!-- 订单信息 -->
    <!-- 一个用户对应多个订单,使用collection -->
    <collection property="ordersList" ofType="mybatis.po.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"/>
        <!-- 订单明细信息 -->
        <!-- 一个订单包括多个明细,使用collection -->
        <collection property="orderdetails" ofType="mybatis.po.Orderdetail">
            <id column="orderdetail_id" property="id"/>
            <result column="items_id" property="itemsId"/>
            <result column="items_num" property="itemsNum"/>
            <result column="orders_id" property="ordersId"/>
            <!-- 商品信息 -->
            <!-- 一个明细对应一个商品信息,使用association -->
            <association property="items" javaType="mybatis.po.Items">
                <id column="items_id" property="id"/>
                <result column="items_name" property="name"/>
                <result column="items_detail" property="detail"/>
                <result column="items_price" property="price"/>
            </association>
        </collection>
    </collection>
</resultMap>

是不是看起来有点复杂了,但是很有条理,一步步的深入即可,仔细看看,其实不是很复杂,就是有点多而已,一个个套进去呗~

如果出现json序列化异常时候我们可以这样操作

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"handler"})
public class Student extends BaseModel {

相关文章

网友评论

      本文标题:MyBatis7-一对多查询&对多对查询

      本文链接:https://www.haomeiwen.com/subject/uyipnttx.html