SSM项目封装结果集:

封装结果集

1.封装Code类和Result类和修改Controller类:

需要在controller的类中封装一个Code类和Result类且再修改一下Controller类:


package com.tang.controller;
​
public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;
​
    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;
}
​

package com.tang.controller;
​
public class Result {
    private Object data;
    private int code;
    private String msg;
​
    public Result(){
    }
​
    public Result(int code , Object data) {
        this.data = data;
        this.code = code;
    }
​
​
    public Result(int code , Object data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }
​
    public Object getData() {
        return data;
    }
​
    public void setData(Object data) {
        this.data = data;
    }
​
    public int getCode() {
        return code;
    }
​
    public void setCode(int code) {
        this.code = code;
    }
​
    public String getMsg() {
        return msg;
    }
​
    public void setMsg(String msg) {
        this.msg = msg;
    }
​
    @Override
    public String toString() {
        return "Result{" +
                "data=" + data +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
}

package com.tang.controller;
​
import com.tang.pojo.Book;
import com.tang.pojo.User;
import com.tang.service.MapperService;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
​
import java.util.ArrayList;
import java.util.List;
​
@RestController
@RequestMapping("/users")
public class SpringMVCConfig {
    @Autowired
    private MapperService mapperService;
​
    @PostMapping
    public Result save(User user){
        boolean flag = mapperService.save(user);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
​
​
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag = mapperService.delete(id);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
​
    @PutMapping
    public Result update(User user){
        boolean flag = mapperService.update(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR , flag);
    }
​
    @GetMapping("/{id}")
    public Result getUser(@PathVariable Integer id){
        User user =  mapperService.getById(id);
        if(user == null){
            return new Result(Code.GET_ERR,null);
        }
        return new Result(Code.GET_OK,user);
    }
​
    @GetMapping
    public Result getAll(){
        List<User>  users =  mapperService.getAll();
        if(users == null){
            return new Result(Code.GET_ERR,null);
        }
        return new Result(Code.GET_OK,users);
    }
}
​

2.测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值