MyBatis中的collection兩種使用方法


表關系: 問題表 1==>n 問題選項表,
需求: 查詢問題時候,聯查出來問題選項

主要解決:代碼復用性高, 主表分頁查詢正確,只需要執行一次sql查詢, 主表分頁查詢不正確

//問題 實體類

public class AnotherGoodsInfo {
    private Integer id;
    private String goods_name;
    private Integer goods_type;
    private String brand_name;
    private String category_name;
    private Double special_price;
    private String city_name;
    private String short_name;
    private Integer city_id;

    private List<GoodsImage> imageList;  // 問題選項 *** 問題表里不需要有這個屬性對應的字段

    private String image;
}


//問題選項 實體類
public class GoodsImage {
    private Integer id;
    private String image_url;
    private String small_image_url;

}

方式一:

QuestionMapper.xml

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionMapper">
   <resultMap id="goodsInfoMapper" type="com.huamo.thirdparty.entity.AnotherGoodsInfo">
    <result property="id" column="id"/>
    <result property="goods_name" column="goods_name"/>
    <result property="goods_type" column="goods_type"/>
    <result property="brand_name" column="brand_name"/>
    <result property="category_name" column="category_name"/>
    <result property="special_price" column="special_price"/>
    <result property="city_name" column="city_name"/>
    <result property="city_id" column="city_id"/>
    <result property="short_name" column="short_name"/>
    <result property="image" column="image"/>
    <collection property="imageList" select="getGoodsImages" ofType="com.huamo.thirdparty.entity.GoodsImage" javaType="ArrayList"
                column="{series_id=id,goods_type=goods_type}">
    </collection>

      <!-- qid/sort是定義的變量名, id/sort是主表的字段id/sort,
        先查出主表的結果, 然后主表記錄數是幾 就執行幾次 collection 的select,
        javaType和ofType 寫不寫都行,
        select的值: 對應xml的namespace + 對應xml中的代碼片段的id,
        column作為select語句的參數傳入,如果只傳一個參數id可以簡寫: column="id" -->
</resultMap>   

<!-- 查詢列表主查詢(先查出主表的結果, 然后主表記錄數是幾 就執行幾次 collection 的select,其實就相當於在java代碼中寫個循環判斷在查詢,只不過這里交給sql執行不用java代碼執行 其實是一樣得) -->
   <select id="getAllGoodsInfo" resultMap="goodsInfoMapper">
    select t1.id,substring(concat(t1.goods_name,ifnull(t6.goods_title,"") ),1,100) as goods_name,t1.goods_type,t4.name as brand_name,t5.category_name,t6.special_price, t7.city_name,t1.venue_cityid as city_id,t7.short_name
    from (
        select id,supplier_id,venue_cityid,goods_name,goods_type
        from hxjb_goods_series
        where
        (goods_type = 11 OR (goods_type in (9,12) and audit_status = 2))
        and line_flag = 1 AND del_flag = 1
      ) t1
        LEFT join `hxjb_supplier_brand` t3 on t1.supplier_id = t3.supplier_id
        LEFT join hxjb_brand t4 on t3.brand_id = t4.id
        LEFT join hxjb_supplier t2 on t1.supplier_id = t2.id
        LEFT join `hxjb_supplier_category` t5 on  t2.supplier_category_id = t5.id
        LEFT join hxjb_goods t6 on t1.id = t6.`goods_series_id`
        left join hxjb_venue_city t7 on t1.venue_cityid = t7.id
    ORDER BY t1.id DESC
</select>

<!--子查詢-->

<select id="getGoodsImages" parameterType="java.util.Map" resultType="com.huamo.thirdparty.entity.GoodsImage">
    select image_url from hxjb_goods_image
    where goods_series_id = #{series_id} and del_flag = 1
     <choose>
         <when test="goods_type == 12">
             and image_type_id = 4
         </when>
         <otherwise> and image_type_id = 2

        <!--    pqo.qid = #{qid} <!-- 變量名 qid 對應上文的 qid -->
         <!-- 如果上文中 collection只傳一個參數column="id",只要類型匹配,在這里隨便寫個變量名就可以取到值 #{xyz} -->  -->

       </otherwise>
     </choose>
</select>

----------------------------------------------------------------------------------------------------------------

方式二:

/問題 實體類
public class Question {
    private    String id; //ID
    private    String content; //問題
    private    String type; //問題類型 1:單選,2:多選,3:問答
    private    Integer sort; //排序
    private List<QuestionOption> options; //問題選項 *** 問題表里不需要有這個屬性對應的字段
    //...
}

//問題選項 實體類
public class QuestionOption{
    private    String id; //ID
    private    String qid; //問題ID *** 問題選項表里需要有這個屬性對應的字段
    private    String content; //選項
    private    Integer sort; //排序
    //...
}

方式一:

QuestionMapper.xml

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionMapper">
只需要執行一次sql查詢, 主表分頁查詢不正確
<resultMap id="BaseResultMap" type="com.xxx.modules.xxx.entity.Question" >
        <id column="id"          property="id"      jdbcType="VARCHAR" />
        <result column="content" property="content" jdbcType="VARCHAR" />
        <result column="type"    property="type"    jdbcType="VARCHAR" />
        <result column="sort"    property="sort"    jdbcType="INTEGER" />
        <collection property="options" javaType="java.util.ArrayList" ofType="com.xxx.modules.data.entity.QuestionOption">
            <id column="o_id" property="id" jdbcType="VARCHAR" />
            <result column="o_content" property="content" jdbcType="VARCHAR" />
            <result column="o_sort" property="sort" jdbcType="INTEGER" />
        </collection>
        <!-- 列的別名 o_id,o_content,o_sort , 起別名是因為主子表都有這幾個字段
             這里要寫 ofType, javaType還是可以不寫 -->
    </resultMap>
    <!-- 查詢列表 -->
    <select id="selectList" resultMap="BaseResultMap">
        SELECT
            pq.id, pq.content, pq.type, pq.sort
            ,pqo.id AS oid ,pqo.content AS ocontent ,pqo.sort AS osort <!-- 聯查子表字段,起別名 -->
        FROM
            question AS pq
            LEFT JOIN question_option pqo ON pq.id = pqo.qid <!-- 聯查子表 -->
        <where>  
        </where>
    </select>

注意: 主子表要查詢出來的字段名重復,要起別名

方式二:

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionMapper">

<resultMap id="BaseResultMap" type="com.xxx.modules.xxx.entity.Question" >
        <id column="id"          property="id"      jdbcType="VARCHAR" />
        <result column="content" property="content" jdbcType="VARCHAR" />
        <result column="type"    property="type"    jdbcType="VARCHAR" />
        <result column="sort"    property="sort"    jdbcType="INTEGER" />
        <collection property="options" javaType="java.util.ArrayList" ofType="com.xxx.modules.xxx.entity.QuestionOption"
                    select="com.xxx.modules.xxx.mapper.QuestionOptionMapper.selectList" column="{qid=id,sort=sort}" />
        <!-- qid/sort是定義的變量名, id/sort是主表的字段id/sort,
        先查出主表的結果, 然后主表記錄數是幾 就執行幾次 collection 的select,
        javaType和ofType 寫不寫都行,
        select的值: 對應xml的namespace + 對應xml中的代碼片段的id,
        column作為select語句的參數傳入,如果只傳一個參數id可以簡寫: column="id" -->
    </resultMap>

    <!-- 查詢列表  注意: 主子表要查詢出來的字段名重復,要起別名 -->
    <select id="selectList" resultMap="BaseResultMap">
        SELECT
            pq.id, pq.content, pq.type, pq.sort
        FROM
            question AS pq
        <where>  
        </where>
    </select>

 

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionOptionMapper">
 <!-- 查詢列表 -->
 <select id="selectList" resultType="QuestionOption">
     SELECT
         pqo.id, pqo.content, pqo.sort
     FROM
         question_option AS pqo
     <where>
         pqo.qid = #{qid} <!-- 變量名 qid 對應上文的 qid -->
         <!-- 如果上文中 collection只傳一個參數column="id",只要類型匹配,在這里隨便寫個變量名就可以取到值 #{xyz} -->
     </where>
 </select>


————————————————————————————————————————————————
詳細請看:https://blog.csdn.net/lzxomg/article/details/89739651

 

 

 


免責聲明!

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



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