完成User后端增删改查的操作
实体类
@Data @AllArgsConstructor @NoArgsConstructor public class User extends Model<User> { @TableId(type = IdType.AUTO) private Integer username; private String password; private String role; }
像前端传递消息的实体类
@NoArgsConstructor @AllArgsConstructor @Data public class Result { private Integer code;//响应码,1 代表成功; 0 代表失败 private String msg; //响应信息 描述字符串 private Object data; //返回的数据 //增删改 成功响应 public static Result success(){ return new Result(1,"success",null); } //查询 成功响应 public static Result success(Object data){ return new Result(1,"success",data); } //失败响应 public static Result error(String msg){ return new Result(0,msg,null); } }
UserController.java
@RestController @RequestMapping("/user") public class UserController { @Autowired UserService service; @GetMapping("{username}") public Result findById(@PathVariable int username){ return Result.success(service.getById(username)); } @PostMapping public Result add(@RequestBody User user){ service.save(user); return Result.success(); } @DeleteMapping("{username}") public Result delById(@PathVariable int username){ service.removeById(username); return Result.success(); } @PutMapping public Result update(@RequestBody User user){ service.updateById(user); return Result.success(); } }
UserService.java
public interface UserService extends IService<User> { }
UserServiceImpl.java
@Service public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService { }
UserDao.java
@Mapper public interface UserDao extends BaseMapper<User> { }
后端就此完成
项目结构如下
标签:username,return,success,家庭,private,记账,Result,public From: https://www.cnblogs.com/daniel350-wang/p/18036164