5. Mybatis入門,ResultMap級聯,association,collection,discriminator,resultMap繼承,lazyLoadingEnabled

Mybatis級聯分爲三種:association,collection,discriminator
association:代表一對一關係
collection:代表一對多關係
discriminator:鑑別器,根據實際選擇採用哪個類作爲實例。
我們以如下例子進行說明:

<?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.leo.test.mybatis.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.leo.test.mybatis.entity.User" >
        <id property="id" column="id"/>
        <result property="name" column="name" />
        <result property="age" column="age" />
        <result property="gender" column="gender" />
        <result property="addressId" column="address_id" />
        <association property="address" column="address_id" select="com.leo.test.mybatis.mapper.AddressMapper.selectAddressById" />
        <collection property="userOrder" column="id" select="com.leo.test.mybatis.mapper.UserOrderMapper.getOrderListByUserId" />
    </resultMap>
    <select id="selectUserById" parameterType="int" resultMap="userResultMap" >
        select * from user where id = #{id}
    </select>
</mapper>

association:代表一對一關係,這裏每個用戶對應一個住址
collection:代表一對多關係,每個用戶有多個訂單
discriminator,實現的類似switch case邏輯,比如我們這裏的user有性別,可以根據性別返回不同的結果:

<?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.leo.test.mybatis.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.leo.test.mybatis.entity.User" >
        <id property="id" column="id"/>
        <result property="name" column="name" />
        <result property="age" column="age" />
        <result property="gender" column="gender" />
        <result property="addressId" column="address_id" />
        <association property="address" column="address_id" select="com.leo.test.mybatis.mapper.AddressMapper.selectAddressById" />
        <collection property="userOrder" column="id" select="com.leo.test.mybatis.mapper.UserOrderMapper.getOrderListByUserId" fetchType="lazy" />
        <discriminator javaType="string" column="gender">
            <case value="M" > 
                <collection property="xxx" select="" column="grade" />
            </case>
            <case value="F" resultMap="aaaa" />
            <case value="OTHER" resultType="com.leo.test.mybatis.entity.User">
                <result property="xxx" />
            </case>
        </discriminator>
    </resultMap>
    <select id="selectUserById" parameterType="int" resultMap="userResultMap" >
        select * from user where id = #{id}
    </select>
</mapper>

當我們使用collection的時候,有時候我們可能只需要一個屬性,但是卻會查詢多條結果,這種就比較浪費,這就是N+1的問題,爲此,Mybatis中提供了兩個配置:

  • lazyLoadingEnabled:爲true,開啓延遲加載
  • aggressiveLazyLoading:屬性按需加載,沒有用到不會查詢加載,只有用到屬性的時候纔會查詢加載,爲true的時候,只要調用任意具有懶加載特性的對象的任意一個屬性將完整加載整個對象,即觸發級聯效果。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章