Spring框架中常用的注解有:
- @RestController
- @RequestMapping
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @Controller
- @RequestParam
- @PathVariable
- @ResponseBody
- @ModelAttribute
- @SessionAttributes
- @Autowired
- @Qualifier
- @Component
- @Service
- @Repository
- @Transactional
示例代码:
@RestController
public class UserController {
// Controller methods
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable int id) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable int id) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@PostMapping("/")
public User createUser(@RequestBody User user) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@PutMapping("/{id}")
public User updateUser(@PathVariable int id, @RequestBody User user) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable int id) {
// Method implementation
}
}
示例代码:
@Controller
public class HomeController {
// Controller methods
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/")
public List<User> getUsers(@RequestParam("page") int page, @RequestParam("size") int size) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable int id) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
@ResponseBody
public User getUser(@PathVariable int id) {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
public class UserController {
@ModelAttribute("countries")
public List<String> getCountries() {
// Method implementation
}
}
示例代码:
@Controller
@RequestMapping("/users")
@SessionAttributes("user")
public class UserController {
// Controller methods
}
示例代码:
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
示例代码:
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public UserService(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
this.userRepository = userRepository;
}
}
示例代码:
@Component
public class UserRepositoryImpl implements UserRepository {
// Repository methods
}
示例代码:
@Service
public class UserService {
// Service methods
}
示例代码:
@Repository
public class UserRepositoryImpl implements UserRepository {
// Repository methods
}
示例代码:
@Service
public class UserService {
@Transactional
public void createUser(User user) {
// Method implementation
}
}