MyBatis关系映射

MyBatis关系映射


1.一对一

①嵌套查询

<!--IdCardMapper.xml-->
<mapper namespace="com.itheima.mapper.IdCardMapper">
    <select id="findCodeById" parameterType="Integer" resultType="idCard">
        SELECT * FROM tb_idcard where id=#{id}
    </select>
</mapper>

<!--PersonMapper.xml-->
<mapper namespace="com.itheima.mapper.PersonMapper">
    <select id="findPersonById" parameterType="Integer"
            resultMap="IdCardWithPersonResult">
        SELECT * FROM tb_person where id=#{id}
    </select>
    <resultMap id="IdCardWithPersonResult" type="person">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <association property="card" column="card_id" javaType="IdCard"
                     select="com.itheima.mapper.IdCardMapper.findCodeById"/>
    </resultMap>
</mapper>
  • column:嵌套查询用到的在第一次查询中的字段(传参)
  • select:嵌套执行的SQL语句id
  • javaType:嵌套查询返回对象的类型
  • property:查询后映射的属性
  • fecheType:是否延迟加载 lazy 或者eager(默认不延迟)

column → select → property

注意:主配置文件中要使用 typeAliases 设置类别名


②嵌套结果

只执行一条语句减少消耗数据库性能

<mapper namespace="com.itheima.mapper.PersonMapper">
    <select id="findPersonById2" parameterType="Integer"
            resultMap="IdCardWithPersonResult2">
        SELECT p.*,idcard.code
        from tb_person p, tb_idcard idcard
        where p.card_id=idcard.id
        and p.id=#{id}
    </select>
    <resultMap id="IdCardWithPersonResult2" type="person">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <association property="card" javaType="IdCard">
            <id property="id" column="card_id"/>
            <result property="code" column="code"/>
        </association>
    </resultMap>
</mapper>

③关于延时加载

在主配置文件中进行配置

<settings>
    <!--打开延迟加载开关-->
    <setting name="lazyLoadingEnabled" value="true"/>
    <!--将积极加载改为延迟加载,按需加载-->
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

在映射文件中的<association>和<collection>中设置fetchType="lazy",这样可以一定程度上降低消耗提高查询效率


2.一对多

<mapper namespace="com.itheima.mapper.UserMapper">
    <select id="findUserWithOrders" parameterType="Integer"
            resultMap="UserWithOrdersResult">
        SELECT u.*,o.id as orders_id,o.number
        from tb_user u,tb_orders o
        where u.id=o.user_id
        and u.id=#{id}
    </select>
    <resultMap id="UserWithOrdersResult" type="User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="address" column="address"/>
        <collection property="ordersList" ofType="orders">
            <id property="id" column="orders_id"/>
            <result property="number" column="number"/>
        </collection>
    </resultMap>
</mapper>

ofType为集合中包含类型


3.多对多

①嵌套查询

<!--OrdersMapper.xml-->
<mapper namespace="com.itheima.mapper.OrdersMapper">
    <select id="findOrdersWithProduct" parameterType="Integer"
            resultMap="OrdersWithProductResult">
        select * from tb_orders where id=#{id}
    </select>
    <!--嵌套查询-->
    <resultMap id="OrdersWithProductResult" type="Orders">
        <id property="id" column="id"/>
        <result property="number" column="number"/>
        <collection property="productList" column="id" ofType="product"
                    select="com.itheima.mapper.ProductMapper.findProductById">
        </collection>
    </resultMap>
</mapper>

<!--ProductMapper.xml-->
<mapper namespace="com.itheima.mapper.ProductMapper">
    <select id="findProductById" parameterType="Integer"
            resultType="Product">
        Select * from tb_product where id IN(
            SELECT product_id FROM tb_ordersitem WHERE orders_id=#{id}
        )
    </select>
</mapper>

②嵌套结果

<mapper namespace="com.itheima.mapper.OrdersMapper">
    <select id="findOrdersWithProduct2" parameterType="Integer"
            resultMap="OrdersWithProductResult2">
        select o.*,p.id as pid,p.name,p.price
        from tb_orders o,tb_product p,tb_ordersitem oi
        where oi.orders_id=o.id
        and oi.product_id=p.id
        and o.id=#{id}
    </select>
    <!--嵌套结果-->
    <resultMap id="OrdersWithProductResult2" type="Orders">
        <id property="id" column="id"/>
        <result property="number" column="number"/>
        <collection property="productList" ofType="Product">
            <id property="id" column="pid"/>
            <result property="name" column="name"/>
            <result property="price" column="price"/>
        </collection>
    </resultMap>
</mapper>
posted @ 2021-05-17 19:41  王子饼干  阅读(64)  评论(0编辑  收藏  举报