controller层做的就是对请求的直接处理
先来看看如何对 Test 表进行增删改查操作
前面我们已经编写好了 Dao 和 Service 层
接下来我们只需要调用Service层封装好的函数即可
首先使用 @Autowired 实例化一个接口对象
一般来说,接口是不能用来实例化对象的 该注解就是帮我们完成这项工作
之后去对应的函数中,调用对应的Service中的函数即可
package com.example.demo2.springbootmybatis.controller;import com.example.demo2.springbootmybatis.entiy.Test;import com.example.demo2.springbootmybatis.service.TestService;import com.example.demo2.springbootmybatis.utils.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/*** @author 小喻同学*/@RestController@RequestMapping("/test")public class TestController {@Autowiredprivate TestService testService;/*** 查询所有* */@GetMapping("/all")public Result<List<Test>> getAll(){List<Test> list = testService.getAll();return new Result<>(5000,"success","ok",list);}/*** 查询单个* */@GetMapping("/{name}")public Result<Test> getOne(@PathVariable String name){Test test = testService.getOne(name);return new Result<>(5000,"success","ok",test);}/*** 增加* */@PostMapping("")public Result<Object> insertOne(@RequestBody Test test){int affectedRows = testService.insertOne(test);System.out.println("affectedRows:" + affectedRows);return new Result<>(5000, "success", "ok", test);}/*** 删除* */@DeleteMapping("")public Result<Object> deleteOne(@RequestParam String name){int affectedRows = testService.deleteOne(name);System.out.println("affectedRows:" + affectedRows);return new Result<>(5000, "success", "ok");}/*** 修改* */@PutMapping("")public Result<Object> updateOneNumber(@RequestParam String name,@RequestParam String number){int affectedRows = testService.updateOneNumber(name, number);System.out.println("affectedRows:" + affectedRows);return new Result<>(5000, "success", "ok");}}

在Postman中测试
成功返回了对应的结果
