03-Restful Web Service API 接口

Restful Web Service API 接口

Rest设计规范

GET (重点,对于所有的查询操作,都可以写GET)

– > /order/{id} – > /getOrder?id=1001

POST(重点,对于所有的保存删除更新操作,都可以写POST)

– > /order – > /saveOrder

PUT

– > /order/{id} – > /modifyOrder

DELETE

– > /order/{id} – >/deleteOrder?id=1001

保证自己写的接口是Restful风格的接口即可。

首先我们再service层定义方法:(注意在service层实现类加上@service)

public interface StuService {

    public Stu getStuInfo(int id);

    public void saveStu();

    public void updateStu(int id);

    public void deleteStu(int id);
}

service层接口实现类

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Stu getStuInfo(int id) {
        return stuMapper.selectByPrimaryKey(id);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveStu() {
        Stu stu = new Stu();
        stu.setName("jack");
        stu.setAge(19);
        stuMapper.insert(stu);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateStu(int id) {
        Stu stu = new Stu();
        stu.setId(id);
        stu.setName("lucy");
        stu.setAge(20);
        stuMapper.updateByPrimaryKey(stu);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteStu(int id) {
        stuMapper.deleteByPrimaryKey(id);
    }
}

然后我们在controller层写请求:(注意加上@RestController注释)

@RestController
public class StuFooController {

    @Resource
    private StuService stuService;

    @GetMapping("/getStuInfo/{id}")
    public Object getStuInfo(@PathVariable("id") int id){
        return stuService.getStuInfo(id);
    }

    @PostMapping("/saveStu")
    public Object saveStu(){
        stuService.saveStu();
        return "save,ok";
    }

    @PostMapping("/updateStu/{id}")
    public Object updateStu(@PathVariable("id") int id){
        stuService.updateStu(id);
        return "update,ok";
    }

    @PostMapping("/deleteStu/{id}")
    public Object deleteStu(@PathVariable("id") int id){
        stuService.deleteStu(id);
        return "delete,ok";
    }
}

注意在主程序中加,扫描到包

@MapperScan(basePackages = "com.imooc.mapper")

你可能感兴趣的:(天天吃货项目笔记,restful,spring,boot)