Springboot整合mybatis及swagger2(实战版)

整合前说明

JDK1.7
使用springboot内置tomcat
整合mybatis xml版本
springboot1.5.3
小工具lombok
yml格式

导入pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dwc</groupId>
    <artifactId>rbac</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

    </dependencies>

    <build>
        <!--打包之后的名字-->
        <finalName>rbac</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

项目的整体结构

在这里插入图片描述

创建springboot运行类

package com.dcw;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@MapperScan(basePackages = {"com.dcw.mapper"})
@EnableSwagger2
public class Application {

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


}

@MapperScan(basePackages = {“com.dcw.mapper”}) :表示扫包,扫描mapper包
@EnableSwagger2 开启swagger2
@SpringBootApplication 启动类注解 === (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。

配置yml

server:
  port: 8081
spring:
  datasource:
    username: ivehicle
    password: ivehicle
    url: jdbc:mysql://127.0.0.1:3316/test?useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.dcw.entity
  

mybatis:
mapper-locations: classpath:mapping/*Mapper.xml //表示映射的mapper的xml
type-aliases-package: com.dcw.entity //配置mapper.xml中的实体别名

配置swagger2

package com.dcw;

import com.google.common.collect.Sets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Set;

/**
 * @ClsaaName: Swagger2
 * @auther: LT
 * @Date: 2018/11/13 15:52
 * @Description:
 * @version:
 */

@Configuration
public class Swagger2Config {


    @Bean
        public Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.dcw.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }

        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("swagger-api文档")
                    .description("tree权限点信息")
                    .termsOfServiceUrl("http://blog.csdn.net/saytime")
                    .version("1.0")
                    .build();
        }

}

@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置。

配置entity

package com.dcw.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "权限点信息")
public class SysRbacAclPo {

    @ApiModelProperty(value = "aclId", name = "aclId", required = false)
    @JsonProperty("id")
    private Integer aclId;
    @ApiModelProperty(value = "权限名称", name = "aclName", required = true)
    @JsonProperty("name")
    private String aclName;

    @ApiModelProperty(value = "权限名称缩写", name = "shortName", required = false)

    private String shortName;
    @ApiModelProperty(value = "请求的url", name = "url", required = true)
    @JsonProperty("xUrl")
    private String url;
    @ApiModelProperty(value = "client端请求的cmd(url)", name = "clientUrl", required = false)
    private String clientUrl;
    @ApiModelProperty(value = "权限点类型 1:菜单 2:按钮 3:其他", name = "aclType", required = true)

    private Integer aclType;
    @ApiModelProperty(value = "状态1:正常,0:冻结(默认为1)", name = "status", required = false)

    private Integer status;

    @ApiModelProperty(value = "默认资源:0:共享资源 1:非共享资源", name = "pubResource", required = true)
    private Integer pubResource;

    @ApiModelProperty(value = "排序字段", name = "sort", required = false)

    private Integer sort;
    @ApiModelProperty(value = "权限点父id,为null表示root权限点", name = "parentId", required = false)
    @JsonProperty("pId")
    private Integer parentId;
    @ApiModelProperty(value = "创建时间", name = "createTime", required = false)

    private Date createTime;
    @ApiModelProperty(value = "备注", name = "remark", required = false)
    private String remark;



}

@ApiModelProperty(value = “权限名称”, name = “aclName”, required = true)//用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
@JsonProperty(“name”) //返回json数据时别名为id,如果在添加的时候使用的字段名应该为name而不是aclName
private String aclName;
@ApiModel(value = “权限点信息”) //用户描述实体类。

配置接口(controller)

package com.dcw.controller;


import com.dcw.dto.SysRbacAclDto;
import com.dcw.entity.SysRbacAclPo;
import com.dcw.service.SysRbacAclService;
import com.dcw.utils.ResponseResultVo;
import com.dcw.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import java.util.List;


@Api(value = "acl",tags = "权限点控制类")
@Controller
@RequestMapping("sys/acl")
@ResponseBody
@CrossOrigin
public class SysRbacAclController {

    @Autowired
    private SysRbacAclService sysRbacAclService;


    @GetMapping("/test.json")
    @ApiIgnore
    public ResultVo test(){
        List<SysRbacAclPo> sysRbacAclServiceAll = sysRbacAclService.findAll();
        return ResponseResultVo.getSuccess(sysRbacAclServiceAll);

    }
    @ApiOperation(value="获取所有的树形权限点数据,返回json格式", notes = "获取所有的树形权限点数据,返回json格式")
    @GetMapping("/aclTree.json")
    public ResultVo aclTree(){
        List<SysRbacAclDto> allAclTree = sysRbacAclService.findAllAclTree();
        return ResponseResultVo.getSuccess(allAclTree);
    }

    @ApiOperation(value = "添加权限点信息,返回json格式", notes = "添加权限点信息,返回json格式")
//    @ApiImplicitParam(name="sysRbacAclPo", value="SysRbacAclPo", required = true, dataType = "SysRbacAclPo")
    @PostMapping("/addAcl.json")
    public ResultVo addAcl(@RequestBody SysRbacAclPo sysRbacAclPo) {
        System.out.println(sysRbacAclPo.toString());
        sysRbacAclService.saveAcl(sysRbacAclPo);
        return ResponseResultVo.getSuccess();

    }
    @ApiOperation(value = "编辑权限点信息,返回json格式", notes = "编辑权限点信息,返回json格式")
    @PostMapping("/updateAcl.json")
    public ResultVo updateAcl(@RequestBody SysRbacAclPo sysRbacAclPo) {
        System.out.println(sysRbacAclPo.toString());
        sysRbacAclService.updateAcl(sysRbacAclPo);
        return ResponseResultVo.getSuccess();

    }


    @ApiOperation(value = "删除权限点,返回json格式", notes = "删除权限点,返回json格式")
    @ApiImplicitParam(name = "aclId", value = "权限id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping(value = "/delAcl.json/{aclId}")
    public ResultVo delAcl(@PathVariable(value = "aclId") Integer aclId) {
        sysRbacAclService.deleteAcl(aclId);
        return ResponseResultVo.getSuccess();

    }
} 

@Api(value = “acl”,tags = “权限点控制类”) //用于controller类上

@ApiOperation(value = “删除权限点,返回json格式”, notes = “删除权限点,返回json格式”) //用在controller的方法上,是用来构建Api文档的是对接口说明
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value=“说明方法的用途、作用”
notes=“方法的备注说明”

@ApiImplicitParams:用在请求的方法上,包含一组参数说明
@ApiImplicitParam(name = “aclId”, value = “权限id”, required = true, dataType = “Integer”, paramType = “path”) //用在@ApiImplicitParams的方法里边
用在 @ApiImplicitParams 注解中,指定一个请求参数的配置信息

@ApiImplicitParams({
    @ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="form"),
    @ApiImplicitParam(name="password",value="密码",required=true,paramType="form"),
    @ApiImplicitParam(name="age",value="年龄",required=true,paramType="form",dataType="Integer")
})

@ApiResponses:用于请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
示例:

@ApiOperation(value = "select1请求",notes = "多个参数,多种的查询参数类型")
@ApiResponses({
    @ApiResponse(code=400,message="请求参数没填好"),
    @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})

@RequestBody //请求的是json字符串

@PathVariable

@CrossOrigin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值