Xiaotang began to learn Spring Boot - (5) Front-end display integration


In the previous section, we said that we can already map the content in our database to our front-end through our mybatis. At
insert image description here
this time, careful students will find out, and we will display all the information to our front-end. Users, this is definitely not acceptable! But I can't modify our class. In this way, the statement we receive from Mysql is missing, so we have our port integration.
The basic idea is to recreate a class we want to show others, and then copy our values ​​to him

First, create a front-end display class

In this case, the continuation class of our previous database is the same, but this time, we only write the class content we want others to see. In order to better distinguish the content obtained from the front and back ends, we create our front-end display class under the domain.
insert image description here
You can see that we have less password than before.

package com.example.test.domain.dto;

public class Test1dto {
    
    
    private Integer id;

    private String name;


    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer 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. Judgment

Because we re-given our value to our front-end display class, at this time we will worry that sometimes it will make an error, so we also need a judgment class to ensure that we can return
normally. The general construction is
insert image description here
Then there are the rules, tostring, get method, set method, there is another place to pay attention to, because we may display different information according to different people, so we need to add a paradigm before this function
insert image description here

package com.example.test.resp;

public class CommonResp<T> {
    
    
    private boolean success=true;
    private String message;
    private String codo;
    private T content;

    public boolean isSuccess() {
    
    
        return success;
    }

    public void setSuccess(boolean success) {
    
    
        this.success = success;
    }

    public String getMessage() {
    
    
        return message;
    }

    public void setMessage(String message) {
    
    
        this.message = message;
    }

    public String getCodo() {
    
    
        return codo;
    }

    public void setCodo(String codo) {
    
    
        this.codo = codo;
    }

    public T getContent() {
    
    
        return content;
    }

    public void setContent(T content) {
    
    
        this.content = content;
    }

    @Override
    public String toString() {
    
    
        return "CommonResp{" +
                "success=" + success +
                ", message='" + message + '\'' +
                ", codo='" + codo + '\'' +
                ", content=" + content +
                '}';
    }
}

3. Rewrite the Service layer

Here we take list() as an example, because we want to encapsulate the list we wrote into the judgment class we just made, so first rewrite our return type to CommonResp and then transfer the data obtained by our backend in it. Front-end conversion, let's look at the specific code

public CommonResp list()//改写获取判断类
    {
    
    
        CommonResp commonResp=new CommonResp();
        List<Test1> test1s= test1Mapper.selectByExample(new Test1Example());//获取到我们后台数据
        ArrayList<Test1dto> test1dtos=new ArrayList<>();//我们前台显示类链表
        for(int i=0,l=test1s.size();i<l;i++)
        {
    
    
            Test1 test1=test1s.get(i);
            Test1dto test1dto=new Test1dto();//单个前台显示类
            BeanUtils.copyProperties(test1,test1dto);//复制给我们的类
            test1dtos.add(test1dto);//加进去
        }
        commonResp.setContent(test1dtos);//返还给我们的判断类
        return commonResp;//返回
    }

Since ours is in our control layer, remember to modify the function type
insert image description here

Fourth, the effect

insert image description here
insert image description here
This is the judgment class we wrote at the beginning

Guess you like

Origin blog.csdn.net/weixin_52521533/article/details/124081649