尋找寫程式碼感覺(七)之封裝請求引數和返回引數

久曲健發表於2021-10-13

一、目的

封裝請求引數的目的,是為了統一和方便。直白點說,如果只查詢一個屬性,傳一個入參,這是沒問題的,要是一個物件有100個屬性,查詢需要多個引數關聯,就需要統一方便管理了,簡單說傳物件就完了,哈哈哈。

二、實際案例

先舉個例子,大家一看便知,比如我現在想搞個模糊查詢,按照名稱查詢。

1、介面改造

還是那個查詢介面開始改造,示例程式碼如下:

package com.rongrong.wiki.controller;

import com.rongrong.wiki.domain.EBook;
import com.rongrong.wiki.resp.CommonResp;
import com.rongrong.wiki.service.EBookService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author longrong.lang
 * @version 1.0
 * @description
 */
@RestController
@RequestMapping("/ebook")
public class EBookController {

    @Resource
    private EBookService eBookService;

    @GetMapping("/list")
    public CommonResp list(String name) {
        CommonResp<List<EBook>> resp = new CommonResp<>();
        List<EBook> list = eBookService.list(name);
        resp.setMessage("執行查詢成功!");
        resp.setContent(list);
        return resp;
    }
}

2、從Service層改造

那麼我先從Service加個入參,比如Name,這就是我們常說的按照關鍵字查詢,Sql來看的話就是like一下,這塊我就不廢話了,有點墨跡了,示例程式碼如下:

package com.rongrong.wiki.service;

import com.rongrong.wiki.domain.EBook;
import com.rongrong.wiki.domain.EBookExample;
import com.rongrong.wiki.mapper.EBookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author rongrong
 * @version 1.0
 * @description
 * @date 2021/10/13 10:09
 */
@Service
public class EBookService {

    @Resource
    private EBookMapper eBookMapper;

    public List<EBook> list(String name) {
        EBookExample eBookExample = new EBookExample();
        //此處程式碼的意思相當於,搞了一個Sql的where條件
        EBookExample.Criteria criteria = eBookExample.createCriteria();
        criteria.andNameLike("%"+name+"%");
        return eBookMapper.selectByExample(eBookExample);
    }
}

3、介面改造測試

查詢結果:

三、需求變更

現在我想通過IDName來查詢,或者多個引數來查詢,該怎麼辦?

1、構造統一入參

很簡單,直接傳個類(物件即可),那麼我們先來個統一的入參構造吧,先以兩個引數作為入參為例,示例程式碼如下:

package com.rongrong.wiki.req;

public class EBookReq {
    private Long id;

    private String name;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", name=").append(name);
        sb.append("]");
        return sb.toString();
    }
}

2、構造統一返回

為什麼構造統一返回資訊?

比如我們登入成功,不能把密碼欄位,也返回給使用者吧,感覺好不專業是不是,正常只返回幾個欄位即可,所以才有了統一返回資訊一說。
示例程式碼如下:

package com.rongrong.wiki.resp;

public class EBookResp {
    private Long id;

    private String name;

    private Long category1Id;

    private Long category2Id;

    private String description;

    private String cover;

    private Integer docCount;

    private Integer viewCount;

    private Integer voteCount;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getCategory1Id() {
        return category1Id;
    }

    public void setCategory1Id(Long category1Id) {
        this.category1Id = category1Id;
    }

    public Long getCategory2Id() {
        return category2Id;
    }

    public void setCategory2Id(Long category2Id) {
        this.category2Id = category2Id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public Integer getDocCount() {
        return docCount;
    }

    public void setDocCount(Integer docCount) {
        this.docCount = docCount;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    public Integer getVoteCount() {
        return voteCount;
    }

    public void setVoteCount(Integer voteCount) {
        this.voteCount = voteCount;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", name=").append(name);
        sb.append(", category1Id=").append(category1Id);
        sb.append(", category2Id=").append(category2Id);
        sb.append(", description=").append(description);
        sb.append(", cover=").append(cover);
        sb.append(", docCount=").append(docCount);
        sb.append(", viewCount=").append(viewCount);
        sb.append(", voteCount=").append(voteCount);
        sb.append("]");
        return sb.toString();
    }
}

3、介面改造

示例程式碼如下:

package com.rongrong.wiki.controller;

import com.rongrong.wiki.req.EBookReq;
import com.rongrong.wiki.resp.CommonResp;
import com.rongrong.wiki.resp.EBookResp;
import com.rongrong.wiki.service.EBookService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author longrong.lang
 * @version 1.0
 * @description
 */
@RestController
@RequestMapping("/ebook")
public class EBookController {

    @Resource
    private EBookService eBookService;

    @GetMapping("/list")
    public CommonResp list(EBookReq eBookReq) {
        CommonResp<List<EBookResp>> resp = new CommonResp<>();
        List<EBookResp> list = eBookService.list(eBookReq);
        resp.setMessage("執行查詢成功!");
        resp.setContent(list);
        return resp;
    }
}

4、Service層改造

示例程式碼如下:

package com.rongrong.wiki.service;

import com.rongrong.wiki.domain.EBook;
import com.rongrong.wiki.domain.EBookExample;
import com.rongrong.wiki.mapper.EBookMapper;
import com.rongrong.wiki.req.EBookReq;
import com.rongrong.wiki.resp.EBookResp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author rongrong
 * @version 1.0
 * @description
 * @date 2021/10/13 23:09
 */
@Service
public class EBookService {

    @Resource
    private EBookMapper eBookMapper;

    public List<EBookResp> list(EBookReq eBookReq) {
        EBookExample eBookExample = new EBookExample();
        //此處程式碼的意思相當於,搞了一個Sql的where條件
        EBookExample.Criteria criteria = eBookExample.createCriteria();
        criteria.andNameLike("%"+eBookReq.getName()+"%");
        List<EBook> eBookList = eBookMapper.selectByExample(eBookExample);
        List<EBookResp> eBookRespList = new ArrayList<>();
        for (EBook eBook: eBookList) {
            EBookResp eBookResp = new EBookResp();
            //spring boot 自帶的BeanUtils完成物件的拷貝
            eBookResp.setId(123L);
            BeanUtils.copyProperties(eBook, eBookResp);
            eBookRespList.add(eBookResp);
        }
        return eBookRespList;
    }
}

5、介面改造後測試

四、最後

統一是為了更好的管理和維護,寫程式碼確實會讓人很興奮,好在吃了止痛藥,要不我這老腰肯定又廢了,感謝閱讀,覺得好給我點個贊,謝謝。

相關文章