1. 程式人生 > >mybatis中sql標籤的使用

mybatis中sql標籤的使用

mybatis中sql標籤重要是為了避免在專案開發的過程中重複編寫大量相同的sql語句,例如下面的查詢語句:

    <select id="selectCountryAndCity" parameterType="map" resultMap="countryAndCity1">
        SELECT country.Code as country_code,
        country.Name AS country_name,
        country.Continent AS country_contient,
        country.Region as
country_region, country.SurfaceArea as country_surfaceArea, country.IndepYear as country_indepYear, country.Population as country_population, country.LifeExpectancy as country_lifeExpectancy, country.GNP as country_gnp, country.GNPOld as country_gnpOld, country.LocalName as
country_localName, country.GovernmentForm as country_governmentForm, country.HeadOfState as country_headOfState, country.Capital as country_capital, country.Code2 as country_code2, city.ID as city_id, city.Name as city_name, city.CountryCode as
city_countryCode, city.District as city_district, city.Population as city_population From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code} AND city.name=#{name} </select> <select id="selectAllCityByCountry" parameterType="map" resultMap="countryAndCity2"> SELECT country.Code as country_code, country.Name AS country_name, country.Continent AS country_contient, country.Region as country_region, country.SurfaceArea as country_surfaceArea, country.IndepYear as country_indepYear, country.Population as country_population, country.LifeExpectancy as country_lifeExpectancy, country.GNP as country_gnp, country.GNPOld as country_gnpOld, country.LocalName as country_localName, country.GovernmentForm as country_governmentForm, country.HeadOfState as country_headOfState, country.Capital as country_capital, country.Code2 as country_code2, city.ID as city_id, city.Name as city_name, city.CountryCode as city_countryCode, city.District as city_district, city.Population as city_population From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code} </select> <select id="selectResultByIndepYear" parameterType="map" resultMap="countryAndCity3"> SELECT country.Code as country_code, country.Name AS country_name, country.Continent AS country_contient, country.Region as country_region, country.SurfaceArea as country_surfaceArea, country.IndepYear as country_indepYear, country.Population as country_population, country.LifeExpectancy as country_lifeExpectancy, country.GNP as country_gnp, country.GNPOld as country_gnpOld, country.LocalName as country_localName, country.GovernmentForm as country_governmentForm, country.HeadOfState as country_headOfState, country.Capital as country_capital, country.Code2 as country_code2, city.ID as city_id, city.Name as city_name, city.CountryCode as city_countryCode, city.District as city_district, city.Population as city_population From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code} </select>

除了最後的查詢條件或者返回的resultMap出現變化之外,需要查詢的欄位是完全相同的,通過使用sql標籤,可以將所有需要查詢的欄位封裝在一個sql語句中,語句如下:

    <sql id="all_columns">
        country.Code as country_code,
        country.Name AS country_name,
        country.Continent AS country_contient,
        country.Region as country_region,
        country.SurfaceArea as country_surfaceArea,
        country.IndepYear as country_indepYear,
        country.Population as country_population,
        country.LifeExpectancy as country_lifeExpectancy,
        country.GNP as country_gnp,
        country.GNPOld as country_gnpOld,
        country.LocalName as country_localName,
        country.GovernmentForm as country_governmentForm,
        country.HeadOfState as country_headOfState,
        country.Capital as country_capital,
        country.Code2 as country_code2,
        city.ID as city_id,
        city.Name as city_name,
        city.CountryCode as city_countryCode,
        city.District as city_district,
        city.Population as city_population
    </sql>

那麼通過使用sql將所有需要查詢的欄位封裝起來,在編寫select語句的時候就不需要每次都重複編寫需要查詢的欄位了,只需要在查詢語句中包含定義的sql語句即可,改造後的查詢語句如下:

    <select id="selectCountryAndCity" parameterType="map" resultMap="countryAndCity1">
        SELECT <include refid="all_columns" />
        From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code} AND city.name=#{name}
    </select>
    <select id="selectAllCityByCountry" parameterType="map" resultMap="countryAndCity2">
        SELECT <include refid="all_columns" />
        From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code}
    </select>
    <select id="selectResultByIndepYear" parameterType="map" resultMap="countryAndCity3">
        SELECT <include refid="all_columns" />
        From country LEFT JOIN city ON country.code=city.countrycode WHERE country.code=#{code}
    </select>

通過使用sql標籤減少重複的sql語句,不僅可以減少我們的程式碼量,還更加方便我們檢視!

測試中所使用的資料庫可以通過點選world.sql下載