栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合swagger2接口文档和mybatis-plus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合swagger2接口文档和mybatis-plus

springboot整合swagger2接口文档和mybatis-plus

使用mybatis-plus对员工表的增删改查
项目结构

jar包

  
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.13
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.spring4all
            swagger-spring-boot-starter
            1.9.1.RELEASE
        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.6
        
        
            com.alibaba
            druid-spring-boot-starter
            1.2.1
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            org.projectlombok
            lombok
        
    

注意:
springboot的版本号最新版本swagger会出现问题

 
        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
         
    

mabatis最新版本加上

@Configuration
public class MybatisPlusConfig {


    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

swagger

@Configuration
public class Swagger {
    //获取Swagger2的实例对象Docket。
    @Bean
    public Docket getDocket(){
        Docket docket = new Docket(documentationType.SWAGGER_2)
                .groupName("143").apiInfo(apiInfo())
                .select()
                //为指定的包生成api文档
                .apis(RequestHandlerSelectors.basePackage("com.ccr.controller"))
                //为指定的路径生成api文档
                //.paths(PathSelectors.any())
                .build();
        return docket;
    }

    private ApiInfo apiInfo(){
        Contact DEFAULT_ConTACT = new Contact("程哈哈", "http://www.test.com", "2665488005@qq.com");
        ApiInfo apiInfo = new ApiInfo("员工管理", "员工管理", "2.0",
                "http://www.test.com",
                DEFAULT_CONTACT, "test", "http://www.test.com", new ArrayList());
        return apiInfo;
    }
}


启动类

@SpringBootApplication
@MapperScan(basePackages = "com.ccr.dao")
@EnableSwagger2 //开启swagger
public class SpringbootMybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
    }

}

CommonResult

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "状态码")
public class CommonResult {
    @ApiModelProperty(value = "返回的状态码200成功")
    private Integer code;
    @ApiModelProperty(value = "提示信息")
    private String msg;
    @ApiModelProperty(value = "返回的数据")
    private Object data;
}

EmpService

public interface EmpService {
    //    根据id查询信息
    public CommonResult selectById(Long empId );
    //    查询所有分页
    public CommonResult selectByPage(Integer page,Integer limit);
    //    添加信息
    public CommonResult addEmp(Emp emp);
    //    修改
    public CommonResult upEmp(Emp emp);
    //    删除
    public CommonResult delEmp(Long empId);
}

EmpServiceImpl

@Service(value = "empService")
public class EmpServiceImpl implements EmpService {
    @Resource
    public EmpDao empDao;
    @Override
    public CommonResult selectById(Long empId) {
        Emp emp = empDao.selectById(empId);
        return new CommonResult(200,"查询成功",emp);
    }

    @Override
    public CommonResult selectByPage(Integer page, Integer limit) {
        final Page empPage = new Page<>(page,limit);
        final Page empPage1 = empDao.selectPage(empPage, null);
        return  new CommonResult(200,"查询成功",empPage1);
    }
//  添加
    @Override
    public CommonResult addEmp(Emp emp) {
        int insert = empDao.insert(emp);
        return  new CommonResult(200,"添加成功",insert);
    }

    @Override
    public CommonResult upEmp(Emp emp) {

        final int i = empDao.updateById(emp);
        return  new CommonResult(200,"更新",i);
    }

    @Override
    public CommonResult delEmp(Long empId) {
        final int i = empDao.deleteById(empId);
        return new CommonResult(200,"删除成功",i);

    }
}

Emp

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tbl_emp") //表名和实体类不同时
@ApiModel(value = "员工表")//实体类说明
public class Emp {
    //    实体类中的属性
    @ApiModelProperty(value = "员工id编号")
    @TableId(value = "empId", type = IdType.ASSIGN_ID)
    private Long empId;

    @ApiModelProperty(value = "员工姓名")
    @TableField(value = "empName")
    private String empName;

    @ApiModelProperty(value = "员工性别")
    private char gender;

    @ApiModelProperty("员工邮箱")
    private String email;

    @ApiModelProperty("员工所在的部门编号")
    @TableField(value = "d_Id")
    private Integer dId;
}

EmpDao

public interface EmpDao extends baseMapper {
}

EmpController

@RestController
@RequestMapping("emp")
@Api(tags = "员工表的增删改")
public class EmpController {
    @Autowired
    private EmpService empService;
//    根据id查询
    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询员工信息")
    @ApiImplicitParams(
            @ApiImplicitParam (name = "empId",value = "员工id号",dataType = "Integer" ,required = true)
    )
    public CommonResult getById(Long empId){
        return empService.selectById(empId);
    }

    //    查询所有信息分页
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "一页条数", dataType = "Integer", required = true)
    })
    public CommonResult getAll(Integer page,Integer limit){
        return empService.selectByPage(page,limit);
    }
    //    添加
    @PostMapping("/addEmp")
    @ApiOperation(value = "添加员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "empId", value = "员工id",required = false,paramType = "添加",dataType="Long"),
            @ApiImplicitParam(name = "empName", value = "员工姓名",required = true,paramType = "添加",dataType="String"),
            @ApiImplicitParam(name = "gender", value = "员工性别",required = true,paramType = "添加",dataType = "char"),
            @ApiImplicitParam(name = "email", value = "邮箱",required = true,paramType = "添加",dataType = "String"),
            @ApiImplicitParam(name = "dId", value = "部门",required = true,paramType = "添加",dataType = "Integer"),
    })
    public CommonResult addEmp(Long empId,String empName,char gender,String email,Integer dId){
        Emp emp = new Emp(empId,empName,gender,email,dId);
//        System.out.println(emp);
        return empService.addEmp(emp);
    }
    //    修改
    @PutMapping("upEmp")
    @ApiOperation(value = "修改员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "empId", value = "员工id",required = true,paramType = "修改",dataType="Long"),
            @ApiImplicitParam(name = "empName", value = "员工姓名",required = true,paramType = "修改",dataType="String"),
            @ApiImplicitParam(name = "gender", value = "员工性别",required = true,paramType = "修改",dataType = "char"),
            @ApiImplicitParam(name = "email", value = "邮箱",required = true,paramType = "修改",dataType = "String"),
            @ApiImplicitParam(name = "dId", value = "部门",required = true,paramType = "修改",dataType = "Integer"),
    })
    public CommonResult upEmp(Long empId,String empName,char gender,String email,Integer dId){
        Emp emp = new Emp(empId, empName, gender, email, dId);
        return empService.upEmp(emp);
    }
    //    删除
    @DeleteMapping("delEmp")
    @ApiOperation(value = "删除员工")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "empId", value = "员工id",required = true,paramType = "删除",dataType = "Long"))
    public CommonResult delEmp(Long empId){
        return empService.delEmp(empId);
    }

}

applicatication.yml

#配置端口号
server:
  port: 8080
# 配置druid的信息
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/emp?serverTimezone=Asia/Shanghai
      username: root
      password: root
      max-active: 20
      initial-size: 5
      min-idle: 5
      max-wait: 3000

logging:
  level:
    com.ccr.dao: debug
pagehelper:
  reasonable: true

运行结果






转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684131.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号