1
This commit is contained in:
parent
32ceb09cb5
commit
805716cf5f
60
src/main/java/com/example/demo/common/ResponseBean.java
Normal file
60
src/main/java/com/example/demo/common/ResponseBean.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.example.demo.common;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.example.demo.entity.constant.RestCode;
|
||||
|
||||
public record ResponseBean<T>(int code, T data, String msg) {
|
||||
public static <T> ResponseBean<T> success() {
|
||||
return new ResponseBean<>(RestCode.SUCCESS.getCode(), null, RestCode.SUCCESS.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> success(T data) {
|
||||
return new ResponseBean<>(RestCode.SUCCESS.getCode(), data, RestCode.SUCCESS.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> success(T data, String msg) {
|
||||
return new ResponseBean<>(RestCode.SUCCESS.getCode(), data, msg);
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> success(String msg) {
|
||||
return new ResponseBean<>(RestCode.SUCCESS.getCode(), null, msg);
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure() {
|
||||
return new ResponseBean<>(RestCode.ERROR.getCode(), null, RestCode.ERROR.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(T data) {
|
||||
return new ResponseBean<>(RestCode.ERROR.getCode(), null, RestCode.ERROR.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(String msg) {
|
||||
return new ResponseBean<>(RestCode.ERROR.getCode(), null, msg);
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(T data, String msg) {
|
||||
return new ResponseBean<>(RestCode.ERROR.getCode(), data, msg);
|
||||
}
|
||||
|
||||
|
||||
public static <T> ResponseBean<T> failure(RestCode code) {
|
||||
return new ResponseBean<>(code.getCode(), null, code.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(RestCode code, String msg) {
|
||||
return new ResponseBean<>(code.getCode(), null, msg);
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(RestCode code, T data) {
|
||||
return new ResponseBean<>(code.getCode(), data, code.getMsg());
|
||||
}
|
||||
|
||||
public static <T> ResponseBean<T> failure(RestCode code, T data, String msg) {
|
||||
return new ResponseBean<>(code.getCode(), data, msg);
|
||||
}
|
||||
|
||||
public String toJsonString() {
|
||||
return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.common.ResponseBean;
|
||||
import com.example.demo.mapper.ArticleMapper;
|
||||
import com.example.demo.model.Article;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/article")
|
||||
public class ArticleController {
|
||||
@Resource
|
||||
private ArticleMapper articleMapper;
|
||||
|
||||
|
||||
|
||||
@PostMapping
|
||||
public ResponseBean<Object> addUser(@RequestBody Article article) {
|
||||
int insert = articleMapper.insert(article);
|
||||
return ResponseBean.success(insert);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Article get(@RequestParam Integer id) {
|
||||
return articleMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping
|
||||
public ResponseBean<Object> delete(@RequestParam Integer id) {
|
||||
int insert = articleMapper.deleteById(id);
|
||||
return ResponseBean.success(insert);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ResponseBean<Object> updateUser(@RequestBody Article user) {
|
||||
|
||||
int update = articleMapper.updateById(user);
|
||||
return ResponseBean.success(update);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.mapper.UserMapper;
|
||||
import com.example.demo.model.User;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
|
||||
|
||||
@PostMapping
|
||||
public String addUser(@RequestBody User user) {
|
||||
int insert = userMapper.insert(user);
|
||||
return String.valueOf(insert);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public User get(@RequestParam Integer id) {
|
||||
return userMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping
|
||||
public String delete(@RequestParam Integer id) {
|
||||
int insert = userMapper.deleteById(id);
|
||||
return String.valueOf(insert);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public String updateUser(@RequestBody User user) {
|
||||
|
||||
int update = userMapper.updateById(user);
|
||||
return String.valueOf(update);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
20
src/main/java/com/example/demo/entity/ArticleEntity.java
Normal file
20
src/main/java/com/example/demo/entity/ArticleEntity.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.example.demo.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("article")
|
||||
public class ArticleEntity {
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
@TableField("title")
|
||||
private String title;
|
||||
@TableField("content")
|
||||
private String content;
|
||||
@TableField("author")
|
||||
private String author;
|
||||
}
|
13
src/main/java/com/example/demo/entity/constant/RestCode.java
Normal file
13
src/main/java/com/example/demo/entity/constant/RestCode.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.demo.entity.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum RestCode {
|
||||
SUCCESS(0, "拉取成功"), ERROR(1, "拉取失败"), UNAUTHORIZED(401, "需要登陆");
|
||||
private final int code;
|
||||
private final String msg;
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.example.demo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.demo.model.User;
|
||||
import com.example.demo.model.Article;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
public interface ArticleMapper extends BaseMapper<Article> {
|
||||
|
||||
}
|
@ -4,15 +4,14 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@TableName("wenzhang")
|
||||
@TableName("article")
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
public class Article {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
private String title;
|
Loading…
Reference in New Issue
Block a user