SpringBoot中的注解
文章目录
@RequestMapping
作用:将请求映射到类定义或方法定义处。
末尾不要写斜线!
前面可写斜线,也可不写斜线。建议前面写上斜线。
@Controller
public class LoginController {
// 访问login路径
@RequestMapping("/login")
public String func() {
// 跳转到src/resources/static目录
return "login.html";
}
}
spring.mvc.view.prefix:给@RequestMapping下面的函数的返回值前面拼接一串字符。
spring.mvc.view.suffix:给@RequestMapping下面的函数的返回值后面拼接一串字符。
@GetMapping
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping
@GetMapping = @RequestMapping(method = RequestMethod.POST)
@Mapper
写在每个Mapper接口上面,或者在spring boot启动处一次性使用MapperScan(mapper文件集合所在位置)。
@MapperScan
写在SpringBoot的启动程序上面。例如:@MapperScan("com.lee.project.mapper")
@Repository
@Repository和@Controller、@Service、@[Component的作用差不多,都是把对象交给spring管理。@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理。
@Controller
@Controller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。
@Controller
public class ViewController {
@RequestMapping("/login")
public String login() {
// 跳转到static目录下的loginPage.html文件
return "loginPage.html";
}
}
@responseBody
@Controller
@ResponseBody
public class ViewController {
@RequestMapping("/login")
public String login() {
// 直接在当前界面打印json字符串"loginPage.html"
return "loginPage.html";
}
}
@RestController
@RestController = @Controller + @ResponseBody
@RequestBody
@RequestBody用来接收前端传递给后端请求。支持Post请求,不支持Get请求。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
@RequestParam
接收前端传递给后端的请求。既支持Post请求,也支持Get请求。@RequestParam()可以有多个。
// 访问http://localhost:8080/param?id=11&type=apple
// 在界面上输出 "id=11 type=apple"
@RestController
public class ViewController {
@RequestMapping("/param")
public String Params(@RequestParam(value = "id") int id, @RequestParam(value = "type")String type) {
String info = "id=" + id + " type=" + type;
return info;
}
}
value = “type”)String type) {
String info = “id=” + id + " type=" + type;
return info;
}
}

596





