MyBatis關聯查詢,一對一關聯查詢


數據庫E-R關系

實體類

public class City {
    Long id;
    String name;
    Long countryId;
    Date lastUpdate;
}
public class Country {
    Long id;
    String name;
    Date lastUpdate;
}
public class CityPlus {
    Long id;
    String name;
    Long countryId;
    Date lastUpdate;
    Country country;
}
public class CountryPlus {
    Long id;
    String name;
    Date lastUpdate;
    List<City> cityList;
}

一對一關聯查詢

  一對一關聯查詢可采用的方式有:

  1. 單步查詢,通過級聯屬性賦值
    • result標簽級聯屬性賦值
    • association標簽級聯屬性賦值
  2. 分步查詢

單步查詢

  • 數據模型:一個實體Bean中包含另外一個實體Bean
  • SQL查詢:關聯SQL 查詢語句,如inner join、left join、right join
  • 具體實現方式:
    • 為級聯屬性賦值
    • association標簽

采用相同的select標簽

    <select id="selectCityPlusById" resultMap="cityPlusResultMap">
        select city_id,city,city.country_id as country_id,city.last_update as last_update,
                country.country_id as country_country_id,country,country.last_update as country_last_update
        from city,country
        where city.country_id = country.country_id and  city_id=#{id}
    </select>

result標簽級聯屬性賦值

        <id column="city_id" property="id"/>
        <result column="city" property="name"/>
        <result column="country_id" property="countryId"/>
        <result column="last_update" property="lastUpdate"/>
        <result column="country_country_id" property="country.id"/>
        <result column="country" property="country.name"/>
        <result column="country_last_update" property="country.lastUpdate"/>
    </resultMap>

 association標簽級聯屬性賦值

  •  需要指定級聯實體Bean在上級Bean中的屬性名稱,即association標簽的property屬性
  • 需要指定級聯實體Bean的類型,即association標簽的javaType屬性
  • association標簽的內部和resultMap標簽內部具有相同的結構;
  • association標簽也可以嵌套association標簽;
    <resultMap id="cityPlusResultMap" type="canger.study.chapter04.bean.CityPlus">
        <id column="city_id" property="id"/>
        <result column="city" property="name"/>
        <result column="country_id" property="countryId"/>
        <result column="last_update" property="lastUpdate"/>
        <association property="country" javaType="canger.study.chapter04.bean.Country">
            <id column="country_country_id" property="id"/>
            <result column="country" property="name"/>
            <result column="country_last_update" property="lastUpdate"/>
        </association>
    </resultMap>

分步查詢

  分步查詢是指通過兩次(或更多次)的查詢,來為一個一對一關系的實體Bean賦值。

  • 數據模型:一個實體Bean中包含另外一個實體Bean
  • SQL查詢:簡單SQL語句,不存在關聯查詢
  • 只能通過association標簽實現;

第一步查詢

    <select id="selectCityPlusByIdUnderStep" resultMap="cityPlusResultMapStep">
        select city_id,city,country_id
        from city
        where city_id=#{id}
    </select>

 association標簽:

  • 需要指定級聯實體Bean在上級Bean中的屬性名稱,即association標簽的property屬性;
  • 需要指定下一步查詢需要使用的select語句,即association標簽的select屬性,該屬性值為Mapper接口查詢方法的全限定名
  • 需要使用column屬性,用於指定第二步查詢的輸入參數,第二步查詢只有一個輸入參數時,使用第一步查詢結果的column名稱即可;
    <resultMap id="cityPlusResultMapStep" type="canger.study.chapter04.bean.CityPlus">
        <id column="city_id" property="id"/>
        <result column="city" property="name"/>
        <result column="country_id" property="countryId"/>
        <result column="last_update" property="lastUpdate"/>
        <association property="country"
                     select="canger.study.chapter04.mapper.CountryMapper.selectCountryById"
                     column="country_id">
        </association>
    </resultMap>

第二步查詢

    <select id="selectCountryById" resultMap="countryResultMap">
        select *
        from country
        where country_id=#{id}
    </select>

 

    <resultMap id="countryResultMap" type="canger.study.chapter04.bean.Country">
        <id column="country_id" property="id"/>
        <result column="country" property="name"/>
        <result column="last_update" property="lastUpdate"/>
    </resultMap>

分步查詢中,第二步查詢需要多個參數

當第二步查詢需要多個參數時,column屬性的設置方式為  {param1=column1,param2=column2},其中param1,param2...為第二步查詢的mapper映射文件的SQL語句中所使用的引用參數名稱,column1,column2...為第一步查詢返回的列名稱,示例如下:

第一步

 

    <select id="selectCityPlusByIdUnderStep" resultMap="cityPlusResultMapStep">
        select city_id,city,country_id, "Spain" as countryName
        from city
        where city_id=#{id}
    </select>

 

    <resultMap id="cityPlusResultMapStep" type="canger.study.chapter04.bean.CityPlus">
        <id column="city_id" property="id"/>
        <result column="city" property="name"/>
        <result column="country_id" property="countryId"/>
        <result column="last_update" property="lastUpdate"/>
        <association property="country"
                     select="canger.study.chapter04.mapper.CountryMapper.selectCountryByIdAndName"
                     column="{id=country_id,name=countryName}">
        </association>
    </resultMap>

第二步

    <select id="selectCountryByIdAndName" resultMap="countryResultMap">
        select *
        from country
        where country_id=#{id} and country=#{name} </select>

 

    <resultMap id="countryResultMap" type="canger.study.chapter04.bean.Country">
        <id column="country_id" property="id"/>
        <result column="country" property="name"/>
        <result column="last_update" property="lastUpdate"/>
    </resultMap>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM