SpringBoot整合swagger+MP+PageHelper

发布时间 2023-08-21 11:19:13作者: 九极致之术

1.SpringBoot整swagger接口文档

【接口文档的作用: 就是为了方便前后端的交互】

1.1swagger依赖

   <!--引入swagger2依赖-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <!--图形化依赖-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

1.2配置swagger

新建config包并创建SwaggerConfig配置类

@Configuration
public class DocConfig {

    @Bean
    public Docket docket(){
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aaa.controller"))
                .build();
        return docket;
    }

    private ApiInfo getInfo(){
        Contact DEFAULT_CONTACT = new Contact("百晓生", "http://www.wjy.com", "110@qq.com");
        ApiInfo info = new ApiInfo("AAA", "地府管理系统", "v1.0", "http://www.jd.com",
        DEFAULT_CONTACT, "Apache 2.0", "http://www.baidu.com", new ArrayList<VendorExtension>());
        return info;
    }


}

1.3开启swagger2注解驱动

在主启动类上增加注解@EnableSwagger2

1.4测试运行

右键运行

打开浏览器输入http://localhost:8080/doc.html

网址即可进入Swagger文档页面

你们进去的页面与看到我的页面肯定不一样

因为参数的说明我都已经设置过

下面告诉你们参数的说明怎么设置

1.5swagger注解说明

@Api:接口类的说明 加在controller类上     【@Api(tages = "")】

@ApiOperation: 接口方法的说明。 加在controller方法上   (value = "")

@ApiImplicitParams: 接口方法的所有参数的说明.  (value = { 

@ApiImplicitParam(name = "",value = "",require = "",dataType = "",)

})

@ApiImplicitParam:单个参数的说明 

--name: 参数名

--value: 参数的说明

--require: 是否为必须的

--dataType: 参数类型说明 int string

 

@ApiModel: 实体类的说明 

@ApiModelProperty: 单个参数的说明

1.6 注解参数演示:

1.6.1 controller层接口说明

@RestController
@Api(tags = "学生管理")
public class StudentController {
    @Autowired
    private StudentService service;

    //    添加
    @ApiOperation(value = "对象添加学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "student", value = "学生对象", required = true, dataType = "Object")})
    @PostMapping("/insert")
    public Result insert(@RequestBody student student) {
        return service.insert(student);
    }

    //    删除
    @ApiOperation(value = "根据id删除学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "学生编号", required = true, dataType = "int")})
    @DeleteMapping("/del")
    public Result del(Integer id) {
        return service.del(id);
    }

    //修改
    @ApiOperation(value = "更改学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "student", value = "学生对象", required = true, dataType = "Object")})
    @PostMapping("/update")
    public Result update(@RequestBody student student) {
        return service.update(student);
    }

    //查询
    @ApiOperation(value = "根据id查询学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "学生编号", required = true, dataType = "int")})
    @GetMapping("/sele")
    public Result sele(Integer id) {
        return service.sele(id);
    }

    //分页查询
    @ApiOperation(value = "查询学生全部信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "page", value = "分页当前页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "分页当前条数", required = true, dataType = "int")})
    @GetMapping("/seleall")
    public Result seleAll(Integer page, Integer size) {
        return service.seleAll(page, size);
    }
}

 1.6.2 实体类参数说明:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("学生实体类")
public class student {
    @ApiModelProperty(value = "学生编号")
    private int id;
    @ApiModelProperty(value = "学生姓名")
    private String name;
    @ApiModelProperty(value = "学生手机号")
    private String phone;
    @ApiModelProperty(value = "学生邮箱")
    private String email;
    @ApiModelProperty(value = "学生专业")
    private String profession;
    @ApiModelProperty(value = "学生年龄")
    private int age;
    @ApiModelProperty(value = "学生性别")
    private int gender;
    @ApiModelProperty(value = "学生状态")
    private int status;
    @ApiModelProperty(value = "操作时间")
    private Date createtime;

}

1.6.3 返回值参数说明:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "统一返回类型对象")
public class Result {
    @ApiModelProperty(value = "状态码")
    private int code;
    @ApiModelProperty(value = "状态信息")
    private String msg;
    @ApiModelProperty(value = "返回数据")
    private Object data;
}

测试运行:

2.springboot整合MP+PageHelper

【mp: mybatis-plus】

2.1 依赖准备

MP依赖:

<!--mp的依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

PageHelper依赖:

<!--pageHelper的依赖-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.5</version>
</dependency>

2.2 配置分页插件拦截器

在已有的config包中创建PageConfig类

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

【注:】

 

2.3 利用MP写CRUD

2.3.0 返回类型实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "统一返回类型对象")
public class Result {
    @ApiModelProperty(value = "状态码")
    private int code;
    @ApiModelProperty(value = "状态信息")
    private String msg;
    @ApiModelProperty(value = "返回数据")
    private Object data;
}

2.3.1 实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("学生实体类")
public class student {
    @ApiModelProperty(value = "学生编号")
    private int id;
    @ApiModelProperty(value = "学生姓名")
    private String name;
    @ApiModelProperty(value = "学生手机号")
    private String phone;
    @ApiModelProperty(value = "学生邮箱")
    private String email;
    @ApiModelProperty(value = "学生专业")
    private String profession;
    @ApiModelProperty(value = "学生年龄")
    private int age;
    @ApiModelProperty(value = "学生性别")
    private int gender;
    @ApiModelProperty(value = "学生状态")
    private int status;
    @ApiModelProperty(value = "操作时间")
    private Date createtime;

}

2.3.2 dao层

public interface StudentDao extends BaseMapper<student> {
}

【注:这里只需要继承BaseMapper<实体类对象>父类就行了】

2.3.3 service层

2.3.3.1 service接口:

public interface StudentService {
    //添加
    Result insert(student student);
    //删除
    Result del(int id);
    //更改
    Result update(student student);
    //查询
    Result sele(int id);
    //分页查询
    Result seleAll(Integer page,Integer size);
}

2.3.3.2 service实现类:

@Service
public class StudentServiceim implements StudentService {
    @Autowired
    private StudentDao studentDao;

    //添加
    @Override
    public Result insert(student student) {
        int insert = studentDao.insert(student);
        return insert == 1 ? new Result(200,"添加成功",insert): new Result(500,"添加失败",null);
    }
    //删除
    @Override
    public Result del(int id) {
        int i = studentDao.deleteById(id);
        return i == 1 ? new Result(200,"删除成功",null): new Result(500,"删除失败",null);
    }
    //更改
    @Override
    public Result update(student student) {
        int update = studentDao.updateById(student);
        return update == 1 ? new Result(200,"修改成功",null): new Result(500,"修改失败",null);
    }
    //查询
    @Override
    public Result sele(int id) {
        student studentDao1 = studentDao.selectById(id);
        return studentDao1 != null ? new Result(200,"查询成功",studentDao1): new Result(500,"查询失败",null);
    }
    //分页查询
    @Override
    public Result seleAll(Integer page,Integer size) {
        IPage<student> ipage = new Page<>(page,size);
        IPage<student> studentIPage = studentDao.selectPage(ipage, null);
        return studentIPage != null ? new Result(200,"查询成功",studentIPage): new Result(500,"查询失败",null);
    }
}

2.3.4 controller层

@RestController
@Api(tags = "学生管理")
public class StudentController {
    @Autowired
    private StudentService service;

    //    添加
    @ApiOperation(value = "对象添加学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "student", value = "学生对象", required = true, dataType = "Object")})
    @PostMapping("/insert")
    public Result insert(@RequestBody student student) {
        return service.insert(student);
    }

    //    删除
    @ApiOperation(value = "根据id删除学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "学生编号", required = true, dataType = "int")})
    @DeleteMapping("/del")
    public Result del(Integer id) {
        return service.del(id);
    }

    //修改
    @ApiOperation(value = "更改学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "student", value = "学生对象", required = true, dataType = "Object")})
    @PostMapping("/update")
    public Result update(@RequestBody student student) {
        return service.update(student);
    }

    //查询
    @ApiOperation(value = "根据id查询学生信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "学生编号", required = true, dataType = "int")})
    @GetMapping("/sele")
    public Result sele(Integer id) {
        return service.sele(id);
    }

    //分页查询
    @ApiOperation(value = "查询学生全部信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "page", value = "分页当前页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "分页当前条数", required = true, dataType = "int")})
    @GetMapping("/seleall")
    public Result seleAll(Integer page, Integer size) {
        return service.seleAll(page, size);
    }
}

3.MP方法说明

//查询全部
        studentDao.selectList(null);

        //根据id查询
       studentDao.selectById(24);

        //条件查询
        QueryWrapper<student> studentQueryWrapper = new QueryWrapper<>();

        //根据列名条件查询
        studentQueryWrapper.eq("name","妲己");

        //根据范围条件查询(大于某个值)
        studentQueryWrapper.ge("age",40);

        //根据范围条件查询(小于某个值)
        studentQueryWrapper.lt("age",20);

        //模糊查询
       studentQueryWrapper.like("name","白");

        //范围条件查询--不包括某值
        studentQueryWrapper.ne("gender",1);

        //根据某列降序排序
       studentQueryWrapper.orderByDesc("id");

        //多条件查询
        studentQueryWrapper.like("name","花");
        studentQueryWrapper.ge("age",20);
        studentDao.selectList(studentQueryWrapper);
//批量删除
        //根据主键删除
        studentDao.deleteById(24);
        //根据id批量删除
        ArrayList<Integer> students = new ArrayList<>();
        students.add(1);
        students.add(2);
        students.add(3);
        studentDao.deleteBatchIds(students);
//分页与条件混合
IPage<student> iPage = new Page<>(1,5);
QueryWrapper<student> studentQueryWrapper = new QueryWrapper<>();
studentQueryWrapper.eq("gender",1);
studentDao.selectPage(iPage,studentQueryWrapper);

 

以上便是SpringBoot整合swagger+MP+PageHelper中的内容,如有漏缺请在下方留言告知,我会及时补充