尋找寫代碼感覺(七)之封裝請求參數和返回參數

一、目的

封裝請求參數的目的,是爲了統一和方便。直白點說,如果只查詢一個屬性,傳一個入參,這是沒問題的,要是一個對象有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 10: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完成對象的拷貝
            BeanUtils.copyProperties(eBook, eBookResp);
            eBookResp.setId(12345L);
            eBookRespList.add(eBookResp);
        }
        return eBookRespList;
    }
}

5、接口改造後測試

6、Service層再改造

CopyUtil工具類,示例代碼如下:

package com.rongrong.wiki.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

    /**
     * 單體複製
     */
    public static <T> T copy(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BeanUtils.copyProperties(source, obj);
        return obj;
    }

    /**
     * 列表複製
     */
    public static <T> List<T> copyList(List source, Class<T> clazz) {
        List<T> target = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)){
            for (Object c: source) {
                T obj = copy(c, clazz);
                target.add(obj);
            }
        }
        return target;
    }
}

對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.stereotype.Service;

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

import static com.rongrong.wiki.util.CopyUtil.copyList;

/**
 * @author rongrong
 * @version 1.0
 * @description
 * @date 2021/10/13 10: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完成對象的拷貝
        //    //BeanUtils.copyProperties(eBook, eBookResp);
        //    //eBookResp.setId(12345L);
        //    //單體複製
        //    EBookResp copy = copy(eBook, EBookResp.class);
        //    eBookRespList.add(copy);
        //}
        //列表複製
        List<EBookResp> respList = copyList(eBookList, EBookResp.class);
        return respList;
    }
}

7、接口改造後測試

四、最後

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章