• Spring用于开发Web应用程序常用注解


    Spring框架中常用的注解有:

     - @RestController
     - @RequestMapping
     - @GetMapping
     - @PostMapping
     - @PutMapping
     - @DeleteMapping
     - @Controller
     - @RequestParam
     - @PathVariable
     - @ResponseBody
     - @ModelAttribute
     - @SessionAttributes
     - @Autowired
     - @Qualifier
     - @Component
     - @Service
     - @Repository
     - @Transactional
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. RestController注解:
      @RestController注解是Spring MVC中的一个特殊注解,它结合了@Controller和@ResponseBody注解的功能。它用于标识一个类是一个控制器,并且该类中的方法返回的结果直接作为HTTP响应的内容,而不是视图。

    示例代码:

    @RestController
    public class UserController {
        // Controller methods
    }
    
    • 1
    • 2
    • 3
    • 4
    1. RequestMapping注解:
      @RequestMapping注解用于将HTTP请求映射到控制器的方法上。它可以用于类级别和方法级别,用于指定URL路径和HTTP请求方法。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public User getUser(@PathVariable int id) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. GetMapping注解:
      @GetMapping注解是@RequestMapping注解的缩写形式,用于将HTTP GET请求映射到控制器的方法上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @GetMapping("/{id}")
        public User getUser(@PathVariable int id) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. PostMapping注解:
      @PostMapping注解是@RequestMapping注解的缩写形式,用于将HTTP POST请求映射到控制器的方法上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @PostMapping("/")
        public User createUser(@RequestBody User user) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. PutMapping注解:
      @PutMapping注解是@RequestMapping注解的缩写形式,用于将HTTP PUT请求映射到控制器的方法上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @PutMapping("/{id}")
        public User updateUser(@PathVariable int id, @RequestBody User user) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. DeleteMapping注解:
      @DeleteMapping注解是@RequestMapping注解的缩写形式,用于将HTTP DELETE请求映射到控制器的方法上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @DeleteMapping("/{id}")
        public void deleteUser(@PathVariable int id) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. Controller注解:
      @Controller注解用于标识一个类是一个控制器,它处理HTTP请求并返回视图。

    示例代码:

    @Controller
    public class HomeController {
        // Controller methods
    }
    
    • 1
    • 2
    • 3
    • 4
    1. RequestParam注解:
      @RequestParam注解用于从HTTP请求中获取参数的值,并将其绑定到方法的参数上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @GetMapping("/")
        public List<User> getUsers(@RequestParam("page") int page, @RequestParam("size") int size) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. PathVariable注解:
      @PathVariable注解用于从URL路径中获取参数的值,并将其绑定到方法的参数上。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @GetMapping("/{id}")
        public User getUser(@PathVariable int id) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. ResponseBody注解:
      @ResponseBody注解用于将方法的返回值直接作为HTTP响应的内容,而不是视图。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @GetMapping("/{id}")
        @ResponseBody
        public User getUser(@PathVariable int id) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. ModelAttribute注解:
      @ModelAttribute注解用于将方法的返回值添加到模型中,以便在视图中使用。

    示例代码:

    @Controller
    @RequestMapping("/users")
    public class UserController {
        @ModelAttribute("countries")
        public List<String> getCountries() {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. SessionAttributes注解:
      @SessionAttributes注解用于指定控制器类中的模型属性应该存储在会话中。

    示例代码:

    @Controller
    @RequestMapping("/users")
    @SessionAttributes("user")
    public class UserController {
        // Controller methods
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. Autowired注解:
      @Autowired注解用于自动装配依赖关系,它可以用于构造函数、属性和方法。

    示例代码:

    @Service
    public class UserService {
        private UserRepository userRepository;
    
        @Autowired
        public UserService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. Qualifier注解:
      @Qualifier注解用于指定自动装配的候选者的限定符,当有多个候选者时使用。

    示例代码:

    @Service
    public class UserService {
        private UserRepository userRepository;
    
        @Autowired
        public UserService(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. Component注解:
      @Component注解是一个通用的Spring注解,用于标识一个类是一个组件,它会被自动扫描并注册为Spring的Bean。

    示例代码:

    @Component
    public class UserRepositoryImpl implements UserRepository {
        // Repository methods
    }
    
    • 1
    • 2
    • 3
    • 4
    1. Service注解:
      @Service注解用于标识一个类是一个服务类,它通常用于业务逻辑的处理。

    示例代码:

    @Service
    public class UserService {
        // Service methods
    }
    
    • 1
    • 2
    • 3
    • 4
    1. Repository注解:
      @Repository注解用于标识一个类是一个数据访问对象(DAO),它通常用于数据库操作。

    示例代码:

    @Repository
    public class UserRepositoryImpl implements UserRepository {
        // Repository methods
    }
    
    • 1
    • 2
    • 3
    • 4
    1. Transactional注解:
      @Transactional注解用于标识一个方法或类应该在事务中执行,它可以应用于方法级别和类级别。

    示例代码:

    @Service
    public class UserService {
        @Transactional
        public void createUser(User user) {
            // Method implementation
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Chrome扩展的核心:manifest 文件(上)
    基于AVDTP信令分析蓝牙音频启动流程
    国庆作业day6
    android studio 字节码查看工具jclasslib bytecode viewer
    数据分享|R语言逻辑回归、线性判别分析LDA、GAM、MARS、KNN、QDA、决策树、随机森林、SVM分类葡萄酒交叉验证ROC...
    CSS简单的图片居中
    【MindSpore易点通】数据处理之Numpy数组的轴和矢量化
    自动直播软件开发方案:打造智能化、高效化的直播体验
    MSE = Bias² + Variance?什么是“好的”统计估计器
    MySQL高级语句(三)
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/133789803