解決需求中的 三級聯動


解決需求中的 三級聯動

什么是三級聯動

所謂三級,即使三個級別,聯動代表這三個級別相互依賴與嵌套,唯有這樣才能實現三級聯動;

在平時生活中,網上填一些表格時就可以預見到,例如:某某省(一級)某某市(二級)某某區(三級);

看到這里就可以快速理解了吧,這就是所謂id三級聯動;

如何實現

其實,解決這個需求並不難,首先,三級代表三個屬性,也就意味着,你需要三個屬性表,現在我們可以舉例電商項目的類型;

比如,商品表里面的屬性有 商品id 商品名稱 商品類型 這個類型就是三級聯動里面的最后一個級別,也可以理解成最終類型表,所以商品類型表關聯的就是第三級別表的id;

實現步驟

1.先將三個級別分別創建出三張表,先建出來省級表:

省級表id 省名稱`

再建出來市級,現在就要注意啦,因為上面我也提到了,如果想聯動,那就務必相互依賴,所以,在市級中有三個字段,分別是:

市級id 省級id 市區名稱   

以下就以此類推:

區級表:

區級id 市級id 區域名稱

 

小結:從上可見,三張表均有相互嵌套相相互依賴的關系,唯有這樣才能實現三級聯動;

 

使用規則

因為,三級聯動需要鏈接三張表,所以我們需要建立一個自定義返回類(DTO),或者業務拓展類,在這里還是推薦使用DTO;

直接在dao層下建立一個dto包,這個包均是需要多張表鏈接查詢用的;

值得注意的是,如果是兩張表鏈接,那么就可以不使用DTO的方式去返回結果,只要在Mapper層中對應映射關系即可,但是如果超過兩張表,那么就需要額外建類了;

如圖:

 

 

 在這個類中無疑就是將查詢所需字段,全部填寫在里面,前提是一定要添加注釋,否則這個類將會很亂;

例如:

package com.qyzn.ogpc.dao.dto; import com.qyzn.ogpc.dao.entity.OpgcSonarticletype; import java.util.Date; public class OpgcArticleQueryByTypeDTO { //文章類型屬性開始 start 。。。 //文章類型 private String sonarticletypeNname; private Integer articleId; private String articleHeadline; private String articeRichtext; private Integer articeRead; private Integer articeMode; private Integer articeEndorse; private Integer userId; private Date articePublishtime; /** * 第三級表外鍵對象屬性 */ private OpgcSonarticletype sonarticletype; private String articletypeCover; private Integer articeCommentnum; private String articeResource; private Double articePrice; private Integer articeRecommend; //文章類型屬性結束 end 。。。 //一級屬性開始 start。。。 private Integer articletypeId; private String articletypeName; //一級屬性結束 end。。。 //二級屬性開始 start。。。 private Integer childtypeId; private Integer parentArticletypeId; private String childtypeName; //二級屬性結束 end。。。 //三級屬性開始 start。。。 private Integer sonarticletypeId; private String sonarticletypeName; private Integer parentChildtypeId; //三級屬性結束 end。。。 public String getSonarticletypeNname() { return sonarticletypeNname; } public void setSonarticletypeNname(String sonarticletypeNname) { this.sonarticletypeNname = sonarticletypeNname; } public Integer getArticleId() { return articleId; } public void setArticleId(Integer articleId) { this.articleId = articleId; } public String getArticleHeadline() { return articleHeadline; } public void setArticleHeadline(String articleHeadline) { this.articleHeadline = articleHeadline; } public String getArticeRichtext() { return articeRichtext; } public void setArticeRichtext(String articeRichtext) { this.articeRichtext = articeRichtext; } public Integer getArticeRead() { return articeRead; } public void setArticeRead(Integer articeRead) { this.articeRead = articeRead; } public Integer getArticeMode() { return articeMode; } public void setArticeMode(Integer articeMode) { this.articeMode = articeMode; } public Integer getArticeEndorse() { return articeEndorse; } public void setArticeEndorse(Integer articeEndorse) { this.articeEndorse = articeEndorse; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Date getArticePublishtime() { return articePublishtime; } public void setArticePublishtime(Date articePublishtime) { this.articePublishtime = articePublishtime; } public OpgcSonarticletype getSonarticletype() { return sonarticletype; } public void setSonarticletype(OpgcSonarticletype sonarticletype) { this.sonarticletype = sonarticletype; } public String getArticletypeCover() { return articletypeCover; } public void setArticletypeCover(String articletypeCover) { this.articletypeCover = articletypeCover; } public Integer getArticeCommentnum() { return articeCommentnum; } public void setArticeCommentnum(Integer articeCommentnum) { this.articeCommentnum = articeCommentnum; } public String getArticeResource() { return articeResource; } public void setArticeResource(String articeResource) { this.articeResource = articeResource; } public Double getArticePrice() { return articePrice; } public void setArticePrice(Double articePrice) { this.articePrice = articePrice; } public Integer getArticeRecommend() { return articeRecommend; } public void setArticeRecommend(Integer articeRecommend) { this.articeRecommend = articeRecommend; } public Integer getArticletypeId() { return articletypeId; } public void setArticletypeId(Integer articletypeId) { this.articletypeId = articletypeId; } public String getArticletypeName() { return articletypeName; } public void setArticletypeName(String articletypeName) { this.articletypeName = articletypeName; } public Integer getChildtypeId() { return childtypeId; } public void setChildtypeId(Integer childtypeId) { this.childtypeId = childtypeId; } public Integer getParentArticletypeId() { return parentArticletypeId; } public void setParentArticletypeId(Integer parentArticletypeId) { this.parentArticletypeId = parentArticletypeId; } public String getChildtypeName() { return childtypeName; } public void setChildtypeName(String childtypeName) { this.childtypeName = childtypeName; } public Integer getSonarticletypeId() { return sonarticletypeId; } public void setSonarticletypeId(Integer sonarticletypeId) { this.sonarticletypeId = sonarticletypeId; } public String getSonarticletypeName() { return sonarticletypeName; } public void setSonarticletypeName(String sonarticletypeName) { this.sonarticletypeName = sonarticletypeName; } public Integer getParentChildtypeId() { return parentChildtypeId; } public void setParentChildtypeId(Integer parentChildtypeId) { this.parentChildtypeId = parentChildtypeId; } @Override public String toString() { return "OpgcArticleQueryByTypeDTO{" + "sonarticletypeNname='" + sonarticletypeNname + '\'' + ", articleId=" + articleId + ", articleHeadline='" + articleHeadline + '\'' + ", articeRichtext='" + articeRichtext + '\'' + ", articeRead=" + articeRead + ", articeMode=" + articeMode + ", articeEndorse=" + articeEndorse + ", userId=" + userId + ", articePublishtime=" + articePublishtime + ", sonarticletype=" + sonarticletype + ", articletypeCover='" + articletypeCover + '\'' + ", articeCommentnum=" + articeCommentnum + ", articeResource='" + articeResource + '\'' + ", articePrice=" + articePrice + ", articeRecommend=" + articeRecommend + ", articletypeId=" + articletypeId + ", articletypeName='" + articletypeName + '\'' + ", childtypeId=" + childtypeId + ", parentArticletypeId=" + parentArticletypeId + ", childtypeName='" + childtypeName + '\'' + ", sonarticletypeId=" + sonarticletypeId + ", sonarticletypeName='" + sonarticletypeName + '\'' + ", parentChildtypeId=" + parentChildtypeId + '}'; } }

而在Mapper.xml文件中,需要返回的接口,直接返回DTO的類即可:

    <select id="SelectAllByType" resultType="com.qyzn.ogpc.dao.dto.OpgcArticleQueryByTypeDTO">

接口編寫

在接口這邊,因為是三級聯動,所以呢,我們需要提供三個參數,再配上動態sql去使用,才能達到聯動的效果!

先看dao層接口:

/** * 三級聯動查詢 * @param articletypeId 屬於一級類型id * @param childtypeId 屬於二級類型id * @param sonarticletypeId 屬於 三級類型id * @return mapper xml層建立返回的 dto 對象,包含的博文[商品]數據、各層級數據 */ List<OpgcArticleQueryByTypeDTO> SelectAllByType(@Param("articletypeId")Integer articletypeId, @Param("childtypeId") Integer childtypeId, @Param("sonarticletypeId")Integer sonarticletypeId);

Service:

/** * 三級聯動查詢 * @param articletypeId 屬於一級類型id * @param childtypeId 屬於二級類型id * @param sonarticletypeId 屬於 三級類型id * @return */ Map<String , Object> SelectAllByType(int articletypeId, int childtypeId, int sonarticletypeId);

impl:

/** * 三級聯動查詢 * @param articletypeId 屬於一級類型id * @param childtypeId 屬於二級類型id * @param sonarticletypeId 屬於 三級類型id * @return */ @Override public Map<String, Object> SelectAllByType(int articletypeId, int childtypeId, int sonarticletypeId) { Map<String , Object> map = new ConcurrentHashMap<>(); int code = 200; String msg = "查詢成功"; try { List<OpgcArticleQueryByTypeDTO> dtos = opgcArticleMapper.SelectAllByType(articletypeId, childtypeId, sonarticletypeId); map.put("data",dtos); }catch (Exception e) { code = 500; msg = "查詢錯誤"; e.printStackTrace(); throw new RuntimeException(e); }finally { map.put("code",code); map.put("msg",msg); } return map; }

Controller:

/** * 現在三級聯動查詢基本結束,只用建立測試就行 * @param articletypeId 一級 ID * @param childtypeId 二級 ID * @param sonarticletypeId 三級 ID * @return 返回數據結果 */ @ApiOperation(value = "三級聯動,建立出返回值的dto類,抽取出共有的屬性") @GetMapping("/SelectAllByType") public Map<String , Object> SelectAllByType(@RequestParam("articletypeId")Integer articletypeId, @RequestParam("childtypeId") Integer childtypeId, @RequestParam("sonarticletypeId")Integer sonarticletypeId){ return opgcArticleService.SelectAllByType(articletypeId,childtypeId,sonarticletypeId); };

Mapper.xml:

 <!-- 建立出返回值的 dto 類,抽取出共有的屬性,返回值中建立好了列的別名,別名和 dto 的對象屬性一致,便直接返回 resultType -->
    <select id="SelectAllByType" resultType="com.qyzn.ogpc.dao.dto.OpgcArticleQueryByTypeDTO">
        SELECT
        q.artice_commentnum articeCommentnum,q.artice_endorse articeEndorse,q.artice_mode articeMode,q.artice_price articePrice,q.artice_publishtime articePublishtime,q.artice_read articeRead,q.artice_recommend articeRecommend,q.artice_resource articeResource,q.artice_richtext articeRichtext,q.article_headline articleHeadline,q.article_id articleId,q.articletype_cover articletypeCover,q.sonarticletype_id articeSonarticletypeId , q.user_id userId,
        a.articletype_id articletypeId,a.articletype_name articletypeName,
        b.childtype_id childtypeId,b.childtype_name childtypeName,b.articletype_id parentArticletypeId ,
        c.sonarticletype_id sonarticletypeId,c.sonarticletype_name sonarticletypeName,c.childtype_id parentChildtypeId
        FROM
        opgc_article q
        left join opgc_sonarticletype c on q.sonarticletype_id = c.sonarticletype_id left join opgc_childtype b on b.childtype_id = c.childtype_id left join opgc_articletype a on a.articletype_id = b.childtype_id where 1 = 1 <if test="articletypeId != null"> and a.articletype_id = #{articletypeId} </if> <if test="childtypeId != null"> and b.childtype_id = #{childtypeId} </if> <if test="sonarticletypeId != null"> and c.sonarticletype_id = #{sonarticletypeId} </if> </select>

根據動態sql的條件,傳入三個不同的參數,跟隨變化而變化,就此達到了三級聯動的目的


免責聲明!

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



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