基于前后端分离技术做增删改查操作(SpringBoot+Mybatis Plus+Vue)
以下是一个简化的示例,展示了如何在前后端分离的项目中使用Spring Boot和MyBatis Plus进行增删改查操作。
后端代码(Spring Boot):
// UserController.java
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
� private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.list();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") Long id) {
return userService.getById(id);
}
@PostMapping
public boolean createUser(User user) {
return userService.save(user);
}
@PutMapping("/{id}")
public boolean updateUser(@PathVariable("id") Long id, User user) {
user.setId(id);
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public boolean deleteUser(@PathVariable("id") Long id) {
return userService.removeById(id);
}
}
// UserService.java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> list() {
return userMapper.selectList(null);
}
public User getById(Long id) {
return userMapper.selectById(id);
}
public boolean save(User user) {
return userMapper.insert(user) > 0;
}
public boolean updateById(User user) {
return userMapper.updateById(user) > 0;
}
public boolean removeById(Long id) {
return userMapper.deleteById(id) > 0;
}
}
// UserMapper.java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
前端代码(Vue.js):
// UserService.js
import axios from 'axios';
export default {
getAllUsers() {
return axios.get('/api/users');
},
getUserById(id) {
return axios.get('/api/users/' + id);
},
createUser(user) {
return axios.post('/api/users', user);
},
updateUser(id, user) {
return axios.put('/
评论已关闭