第一章-系统设计与搭建

理解Restful

REST是Representational State Transfer的缩写,翻译是”表现层状态转化”。 可以 总结为一句话:REST是所有Web应用都应该遵守的架构设计指导原则。
7个HTTP方法:GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS。

测试工具Postman

发送HTTP请求的工具。

公共子模块

1.返回结果实体类Result

public class Result{
  private boolean flag;  //是否成功
  private Integer code;  //返回码
  private String message;  //返回信息
  private Object data; //返回数据
  
  //成功并且携带参数的构造器
  public Result(boolean flag,Integer code,String message,Object data){
    super();
    this.flag = flag;
    this,code = code;
    this.message = message;
    this.data = data;
  }
  //失败的构造器
  public Result(boolean flag,Integer code,String message){
    super();
    this.flag = flag;
    this.code = code;
    this.message = message;
  }
  //setter/getter...
}

2.返回分页结果的PageResult

public class PageResult{
    private Long total;
    private List rows;
    
    public PageResult(Long total,List rows){
      super();
      this.total = total;
      this.rows = rows;
    }
    //setter/getter
}

3.分布式ID生成器

@SpringBootApplication
public class BaseApplication{
  public static void main(String[] args){
    SpringBootApplication.run(BaseApplication.class);
  }
  
  @Bean
  public IdWorker idWorker(){
    return new IdWorker(1,1);
  }
}

CRUD的实现

1.实体类

@Entity
@Table(name = "tb_label")
public class Label{
  @Id
  private String id;  //
  private String labelName;  //标签名称
  private String state;  //状态
  private Long count;  //使用数量
  
  //setters/getters
}

2.Jpa数据访问接口

public interface LabelDao extends JpaRespository,JpaSpecificationExecutor

3.业务逻辑类

@Service
public class LabelService{
  @Autowired
  private LabelDao labelDao;
  
  @Autowired 
  private IdWorker idWorker;

  public List

4.控制器类

@RestController
@RequestMapping("/label")
public class LabelController{

@Autowired
  private LabelService labelService;
  
      /*查询全部列表
       * @return
       */
  @RequestMapping(method = RequestMethod.GET)
  public Result findAll(){
    return new Result<>(true,StatusCode.OK,"查询成功",labelService.findAll());
  }
  
      /**     
      *  根据ID查询标签     
      * @param id      
      * @return      
      */
      @RequestMapping(value = /{id},method = RequestMethod.GET)
      public Result

5.接口测试

第一章-系统设计与搭建_第1张图片
image.png

你可能感兴趣的:(第一章-系统设计与搭建)