controller与service调用的语句
时间: 2023-09-30 20:04:49 浏览: 77
在Java语言中,controller 与 service 调用的语句通常如下所示:
1. Controller 调用 Service:
```
@Autowired
private UserService userService;
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
User user = userService.getUserById(id);
if (user == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(user, HttpStatus.OK);
}
```
2. Service 调用 Dao:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserById(Long id) {
return userDao.getUserById(id);
}
}
```
其中,`@Autowired` 注解用于自动注入依赖的实例,`@RequestMapping` 注解用于映射请求和处理方法,`@PathVariable` 注解用于获取路径中的参数,`ResponseEntity` 则用于封装 HTTP 响应。
阅读全文