To address the needs of three linkage

To address the needs of three linkage

What are the three linkage

The so-called three, even three levels, in conjunction with representatives of the three levels of nested interdependence, the only way to achieve three linkage;

Since in normal life, when the network will be expected to fill out some forms to, for example: a certain area (a) certain city a certain province (two) (three);

See here you can quickly understand it, this is called id three linkage;

How to achieve

In fact, this requirement is not difficult to solve, first of all, three representatives of the three properties, which means that you need three property sheets, and now we can project an example of the type of electricity supplier;

For example, attributes of goods inside the table id Product name Product type linkage of this type is three last level inside, can be understood as the type of final table id table so that item type associated with a third level of the table;

Implementation steps

1. The first three levels, respectively, to create three tables, the first built out provincial table:

Province provincial table id name `

To build out municipal, and now we must pay attention to you, because I also mentioned above, if you want the linkage, then be sure interdependent, so there are three fields in the city, namely:

Id id provincial and municipal urban name   

Following on and so on:

District-level table:

Id id district municipal area name

 

Summary: apparent from the above, three tables are interdependent with each other in a nesting relationship, the only way to achieve three linkage;

 

Use Rules

Because the three linkage need to link three tables, so we need to build a custom return type (DTO), or business development classes, where it is recommended to use DTO;

Establish a dto package directly in the dao layer, this package are needed link multiple tables of queries;

It is noteworthy that, if two tables are linked, then you can not use the DTO way to return results, as long as you can in the corresponding mapping relation Mapper layer, but if more than two tables, you need to build additional class of;

Figure:

 

 

 In this class is no doubt that the inquiry required fields, all fill in it, the premise is that we must add a comment, otherwise the class will mess;

E.g:

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;
    / ** 
     * third level table foreign key object properties 
     * / 
    Private OpgcSonarticletype sonarticletype; 

    Private String articletypeCover; 

    Private Integer articeCommentnum; 

    Private String articeResource; 

    Private Double articePrice; 

    Private Integer articeRecommend;
     // Article type properties end end. . . 

    // an attribute start start. . . 
    Private Integer articletypeId; 

    Private String articletypeName;
     // an attribute end end. . . 

    // two properties start start. . . 
    Private Integer childtypeId; 

    Private Integer parentArticletypeId; 

    PrivateChildtypeName String;
     // two attributes end end. . . 

    // three attributes start start. . . 
    Private Integer sonarticletypeId; 

    Private String sonarticletypeName; 

    Private Integer parentChildtypeId;
     // three properties end end. . . 


    public String getSonarticletypeNname () {
         return sonarticletypeNname; 
    } 

    public  void setSonarticletypeNname (String sonarticletypeNname) {
         the 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 +
                '}';
    }
}

In Mapper.xml file, you need to return the interfaces directly back to the DTO class:

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

Interface to write

In the interface side, because it is three linkage, so, we need to provide three arguments, adding dynamic sql to use in order to achieve the effect linkage!

Look dao layer interface:

/ ** 
     * three linkage query 
     * @param articletypeId belonging to one type of ID 
     * @param childtypeId belong to two types of ID 
     * @param sonarticletypeId belong to three types of ID 
     * @return Mapper XML layer established dto object returned, Bowen contained [product] data, each of the hierarchical data
      * / 
    List <OpgcArticleQueryByTypeDTO> SelectAllByType (@Param ( "articletypeId") Integer articletypeId, @Param ( "childtypeId") Integer childtypeId, @Param ( "sonarticletypeId") Integer sonarticletypeId);

Service:

/ ** 
     * three linkage query 
     * @param articletypeId belonging to one type of ID 
     * @param childtypeId belong to two types of ID 
     * @param sonarticletypeId belong to three types of ID 
     * @return 
     * / 
    the Map <String, Object> SelectAllByType ( int articletypeId , int childtypeId, int sonarticletypeId);

impl:

/ ** 
     * three linkage query 
     * @param articletypeId belonging to one type of ID 
     * @param childtypeId belong to two types of ID 
     * @param sonarticletypeId belong to three types of ID 
     * @return 
     * / 
    @Override 
    public the Map <String, Object> SelectAllByType ( int articletypeId, int childtypeId, int sonarticletypeId) { 
        the Map <String, Object> = Map new new of ConcurrentHashMap <> ();
         int code = 200 is ; 
        String MSG = "query is successful" ;
         the 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:

/ ** 
     * now three basic query linkage end, only create a test line 
     * @param articletypeId an ID 
     * @param childtypeId two ID 
     * @param sonarticletypeId three ID 
     * @return return the data results
      * / 
    @ApiOperation (value = "three linkage, to establish a return value class dto, extracted shared attributes" ) 
    @GetMapping ( "/ SelectAllByType" )
     public the Map <String, Object> SelectAllByType (@RequestParam ( "articletypeId") Integer articletypeId, @RequestParam ( "childtypeId") Integer childtypeId, @RequestParam ( "sonarticletypeId" ) Integer sonarticletypeId) {
         return opgcArticleService.SelectAllByType(articletypeId,childtypeId,sonarticletypeId);
    };

Mapper.xml:

<! - to establish a return dto class values extracted common property, the return value is set up in a consistent column alias, and dto object properties, then return directly resultType -> 
    <the SELECT the above mentioned id = "SelectAllByType" = the resultType "com.qyzn.ogpc.dao.dto.OpgcArticleQueryByTypeDTO"> 
        the 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>

According to conditions of dynamic sql, passing in three different parameters, follow the changes and change this to achieve the purpose of three linkage

Guess you like

Origin www.cnblogs.com/StanleyBlogs/p/11442679.html