controller中的代码
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
@Autowired
private PmsBrandService demoService;
@RequestMapping(value="/create",method=RequestMethod.POST)
@ResponseBody
public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {
CommonResult commonResult;
int count = demoService.createBrand(pmsBrand);
if(count == 1) {
commonResult = CommonResult.success(pmsBrand);
}else{
commonResult = CommonResult.failed("操作失败");
}
return commonResult;
}
}
service层
@Service
public class PmsBrandService {
@Autowired
private PmsBrandMapper brandMapper;
public int createBrand(PmsBrand brand){
return brandMapper.insertSelective(brand);
}
}
dao层
public interface PmsBrandMapper{
int insertSelective(PmsBrand record);
}
要返回的数据
/**
*通用返回对象
*/
@Setter
@Getter
public class CommonResult<T>{
private long code;
private String message;
private T data;
protected CommonResult() {
}
protected CommonResult(long code,String message,T data){
this.code = code;
this.message = message;
this.data = data;
}
//返回成功结果
public static <T> CommonResult<T> success(T data){
return new CommonResult<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMethod,data);
}
//成功返回结果
public static <T> CommonResult<T> success(T data,String message){
return new CommonResult<T>(ResultCode.SUCCESS.getCode(),message,data);
}
//返回失败结果
public static <T> CommonResult<T> failed(String message){
return new CommonResult<T>(ResultCode.FAILED.getCode(),message,null);
}
.......
}
状态码
public enum ResultCode{
SUCCESS(200,"操作成功"),
FAILE(500,"操作失败");
private long code;
private String message;
private ResultCode(long code,String message) {
this.code = code;
this.message = message;
}
public long getCode(){
return code;
}
public String getMessage(){
return message;
}
}