controller的增删改功能代码
时间: 2023-08-06 21:20:31 浏览: 50
好的,以下是一个controller中增删改的示例代码:
```java
@RestController
@RequestMapping("/example")
public class ExampleController {
@Autowired
private ExampleService exampleService;
// 查询
@GetMapping("/{id}")
public Example getById(@PathVariable Long id) {
return exampleService.getById(id);
}
// 新增
@PostMapping("")
public Example addExample(@RequestBody Example example) {
return exampleService.addExample(example);
}
// 修改
@PutMapping("/{id}")
public Example updateExample(@PathVariable Long id, @RequestBody Example example) {
Example oldExample = exampleService.getById(id);
oldExample.setName(example.getName());
oldExample.setAge(example.getAge());
return exampleService.updateExample(oldExample);
}
// 删除
@DeleteMapping("/{id}")
public void deleteExample(@PathVariable Long id) {
exampleService.deleteExample(id);
}
}
```
其中,`ExampleService`为一个服务层的接口,具体实现可以根据具体需求编写。`Example`为一个实体类,包含了需要增删改的字段。这个示例代码中,我们使用了`@Autowired`注解自动注入了`ExampleService`,在各个方法中调用服务层的接口实现相应的增删改功能。