To obtain from the data controller

controller code

@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 layer

@Service
public class PmsBrandService {
	@Autowired
	private PmsBrandMapper brandMapper;
	
	public int createBrand(PmsBrand brand){
		return brandMapper.insertSelective(brand);
	}
}

dao layer

public interface PmsBrandMapper{
		int insertSelective(PmsBrand record);
}

Data to be returned

/**
 *通用返回对象
 */
 @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);
	}
	.......
}

status code

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;
	}
}
Published 17 original articles · won praise 0 · Views 503

Guess you like

Origin blog.csdn.net/HexString/article/details/104652674