其他分享
首页 > 其他分享> > 第二天----后端C端接口的编写

第二天----后端C端接口的编写

作者:互联网

第二天----后端C端接口的编写

查询接口

查询接口现在其实挺简单的,就是将前端发送的数据在数据库中查询一下

这里来说说表结构吧

挺简单的不够后面在进行补充吧

image-20220101090004540

@RestController
@RequestMapping("//directory")
@Api(value = "查询模块",tags = "C端图书查询接口")
public class DirectoryController {
    @Autowired
    private IDirectoryService directoryService;

    @GetMapping("/getList")
    @ApiOperation(value = "获得官网")
    public RespBean getList(){
        List<Directory> list = directoryService.list();
        return RespBean.success("success",list);
    }
    @PostMapping("/selectName")
    @ApiOperation(value = "查询官网")
    public RespBean selectName(@RequestBody SelectDTO selectDTO){
        System.out.println(selectDTO);

        QueryWrapper<Directory> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",selectDTO.getNa());
        List<Directory> list =directoryService.list(queryWrapper);
        return RespBean.success("success",list);
    }
}

第一个getList接口其实可以不用,这只是我一开始测试整体时写的

首先的事先@Autowired自动注册iservice层

应为用到了Mybatis所以可以直接写个warpper

在service.list时用wrapper里面的内容进行筛选

然后直接返回数据,然后数据的RespBean是统一返回的类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespBean {
    private long code;
    private String message;
    private Object data;
    
    /**
     * 成功返回结果
     */
    public static RespBean success(String message){
        return new RespBean(200,message,null);
    }
    /**
     * 成功返回结果
     */
    public static RespBean success(String message,Object obj){
        return new RespBean(200,message,obj);
    }
    /**
     * 失败返回结果
     */
    public static RespBean error(String message,Object obj){
        return new RespBean(500,message,obj);
    }
    /**
     * 失败返回结果
     */
    public static RespBean error(String message){
        return new RespBean(500,message,null);
    }
}
@Data
public class SelectDTO {
    String na;
    String ne;
}

然后是申请相关的接口

都差不多

 */
@RestController
@RequestMapping("//application")
@Api(value = "申请模块",tags = "C端申请端口")
public class ApplicationController {
    @Autowired
    private IApplicationService applicationService;


    @PostMapping("/addApplication")
    @ApiOperation(value = "增加申请")
    public RespBean addApplication(@RequestBody Application application){
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        application.setApplication_id(snowflake.nextId());
        System.out.println(application);
        boolean save = applicationService.save(application);
        if (save){
            return RespBean.success("1");
        }
        return RespBean.error("2");
    }
    @GetMapping("/selectList")
    @ApiOperation(value = "所有的申请列表")
    public RespBean selectList(){
        List<Application> list = applicationService.list();
        return RespBean.success("成功",list);
    }


}

标签:RespBean,success,后端,list,接口,----,return,message,public
来源: https://blog.csdn.net/qq_45671761/article/details/122268178