SpringBoot常用注解

Controller层

注释含义
@Controller让 URL 请求返回具体的页面,如返回 html、jsp 页面等,这个注解在类头上添加 配合@RequestMapping 进行具体页面跳转控制 返回的是页面
@RequestMapping@Controller + @RequestMapping 页面跳转
@RequestParam带参数 http://localhost:8080/param?id=10&age=33
@PathVariable带参数 http://localhost:8080/path/10/33
@RestController@Controller + @ResponseBody = @RestController 返回的是结果:如Json 结合@GetMapping @PostMapping @PutMapping @DeleteMapping 使用
@GetMapping
@PostMapping@RequestBody
@PutMapping
@DeleteMapping
@ResponseBody可以用于@PostMapping @PutMapping 将 Json 转换对象

 

 

配置 Html 前缀后缀

上面的可以简写未 return "view";

 

引入 freemark 模板

在 index.html 页面获取 data 数据

        <!-- freemarker模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
freemarker:
  suffix: .html
  template-loader-path: classpath:/static/

返回页面(不带数据,带数据)

带数据需引入 freemark 模板

@Controller
public class ViewController {
    // 返回页面
    @RequestMapping("view")
    public String view (){
        return "view";
    }
    // 返回页面数据
    @RequestMapping("data")
    public ModelAndView data (){
        ModelAndView view = new ModelAndView("view");
        view.addObject("str1","Hello");
        view.addObject("str2","World");
​
        return view;
    }

view.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图页面</title>
</head>
<body>
    <div>我的页面</div>
    <hr>
    <div>${str1}</div>
    <hr>
    <div>${str2}</div>
</body>
</html>

URL带参数

http://localhost:8080/param?id=10&age=33
@Controller
public class ParamController {
    // 返回页面 xx/param?id=10&age=33
    @RequestMapping("param")
    public ModelAndView param (@RequestParam(value = "id", defaultValue = "0") int id,
                               @RequestParam(value = "age", defaultValue = "22") int age){
        ModelAndView view = new ModelAndView("param");
        view.addObject("id", id);
        view.addObject("age", age);
        return view;
    }
    // 返回页面 xx/param/10/33
    @RequestMapping("path/{id}/{age}")
    public ModelAndView path (@PathVariable(value = "id") int id,
                               @PathVariable(value = "age") int age){
        ModelAndView view = new ModelAndView("param");
        view.addObject("id", id);
        view.addObject("age", age);
        return view;
    }
}

Get Post Put Delete

@RestController
@RequestMapping("user")
public class UserController {
    @GetMapping
    public User getUser() {
        User user = new User(10, "get", "M", 10);
        return user;
    }
    @GetMapping("{id}")
    public User getUserById(@PathVariable("id") int id) {
        User user = new User(id, "getById", "M", 10);
        return user;
    }
    @PostMapping
    public User addUser(@RequestBody User user) {
        return user;
    }
    @PutMapping
    public User updateUser(@RequestBody User user) {
        return user;
    }
    @DeleteMapping("{id}")
    public boolean deleteUserById(@PathVariable("id") int id) {
        System.out.println(id);
        return true;
    }
}

其他

注释含义
@Servicebean注册
@Componentbean注册
@Autowired获取bean
@Resource获取bean
@Autowired + @Qualifier获取bean
@Configuration + @Beanbean注册(人为)
@Values配置文件中取参数

@Service + @Autowired

Service 实现类

// @Component 一般不用在业务层
@Service
public class UserServiceImpl implements UserService {
    @Override
    public User getUser() {
        User user = new User(10, "get", "M", 10);
        return user;
    }
​
    @Override
    public User getUserById(int id) {
        User user = new User(id, "getById", "M", 10);
        return user;
    }
}

Controller类

使用@Service + @Autowired 替代了 下面的方式 不用new对象了

userService = new UserServiceImpl();
return userService.getUser();

@RestController
@RequestMapping("action")
public class UserActionController {
    @Autowired
    private UserService userService;
​
    @GetMapping
    public User getUser() {
        return userService.getUser();
    }
    @GetMapping("{id}")
    public User getUserById(@PathVariable("id") int id) {
        return userService.getUserById(id);
    }
}

接口多个实现

@Resource = @Autowired + @Qualifier 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值