springboot完整项目(mybatis+swagger+Generator插件)

后续持续更新...

1.poi.xml配置


    org.springframework.boot
    spring-boot-starter-parent
    2.1.9.RELEASE
 
版本不宜超过2.2.0.RELEASE,这个问题折磨的好几天

        
            mysql
            mysql-connector-java
            5.1.44
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
        
            org.springframework.boot
            spring-boot-starter-validation
        

        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            io.springfox
            springfox-swagger2
            2.2.2
        
        
            io.springfox
            springfox-swagger-ui
            2.2.2
        

        
            com.alibaba
            fastjson
            1.2.60
        
         
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
    


        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    
                        mysql
                        mysql-connector-java
                        5.1.44
                    
                
                
                    src/main/generated/generator.xml
                    true
                    false
                
            
        

        
            
                src/main/resources
                
                    **/*.p12
                    **/*.crt
                    **/*.html
                    **/*.cer
                    **/*.pfx
                
                false
            
            
                src/main/java
                
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.yml
                    **/*.yaml
                    **/*.properties
                    **/*.xml
                    **/*.txt
                    **/*.ini
                
                true
            
        
    

2.Applivation.yml配置

server:
  servlet:
    context-path: /${spring.application.name}
spring:
  application:
    name: test
  http:
    encoding:
      force: true
      charset: UTF-8
      enabled: true
  # 同一容器部署多springboot实例
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: root

mybatis:
  mapper-locations: classpath*:com/example/test/dao/mapping/**/*.xml
  configuration:
    # 启动驼峰命名映射数据库表,例userId对应user_id
    map-underscore-to-camel-case: true
    # insert返回主键
    use-generated-keys: true
    ## swagger配置
    swagger:
      enabled: true

3.创建generator.xml

新建目录src/main/下文件名为 generated




    
        
        
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        

4.configuration文件夹下创建Swagger

@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", name = "enabled", havingValue = "true", matchIfMissing = true)
public class Swagger {
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {

//登录验证暂时用不到
// List pars = new ArrayList<>();
// ParameterBuilder tokenPar = new ParameterBuilder();
// tokenPar.name("token")
// .description("用户token")
// .modelRef(new ModelRef("string"))
// .parameterType("header")
// .required(false)
// .build();
// pars.add(tokenPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//修正Byte转string的Bug
                .directModelSubstitute(Byte.class, Integer.class)
// .globalOperationParameters(pars)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.test.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("Swagger API 接口文档")
                .contact("Yaoyao.Mei")
                .version("1.0")
                .build();
    }
}

备注:

@ConditionalOnProperty(prefix = "swagger", name = "enabled", havingValue = "true", matchIfMissing = true) 

作用:在application.yml 可以设置是否开启

参考链接:https://blog.csdn.net/dyc87112/article/details/73739411

@RequiresRoles(value = {"leader", "user"}, logical = Logical.OR)   //角色权限设置

关于Swagger使用的注解及其说明,请参考https://blog.csdn.net/sanyaoxu_2/article/details/80555328

 

5.创建controller,service;  mybatis通过Generator 插件自动生成

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    TestService testService;

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public GetUserVoRespVo getUserName(@RequestBody GetUserVoReqVo req) {
        return testService.query(req);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get() {
        return testService.query();
    }
}
public interface TestService {
     GetUserVoRespVo query(GetUserVoReqVo req);
     String query();
}

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private UserMapperExt userMapperExt;

    @Override
    public GetUserVoRespVo query(GetUserVoReqVo req) {
        User user = userMapperExt.selectAll();
        GetUserVoRespVo userVoRespVo = new GetUserVoRespVo();
        BeanUtils.copyProperties(user, userVoRespVo);
        return userVoRespVo;
    }

    @Override
    public String query() {
        User user = userMapperExt.selectAll();
        System.out.println(user);
        return user.getName();
    }
}

7.启动类:

@SpringBootApplication
@MapperScan("com.example.test.dao.mapper")
public class YaoApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(YaoApplication.class);
}
    public static void main(String[] args) {
        SpringApplication.run(YaoApplication.class, args);
    }

启动类继承SpringBootServletInitializer,并复写configure()方法,可以避免jetty容器启动报错.

@MapperScan("com.example.test.dao.mapper"),启动时扫描

项目结构如图:

springboot完整项目(mybatis+swagger+Generator插件)_第1张图片

springboot完整项目(mybatis+swagger+Generator插件)_第2张图片

解释:

@MapperScan("com.example.test.dao.mapper")

mybatis: mapper-locations: classpath*:com/example/test/dao/mapping/**/*.xml

poi build resoucese: 我们用到的资源文件(各种xml,properties,xsd文件)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里。

 

servlet-api  

HttpServletRequest和HttpServletResponse 等对象的,但是tomcat里也有,运行时要用tomcat自己的,所以引用maven包          

 

       org.apache.tomcat

           tomcat-jsp-api

        3.0

             provided

            

编写servlet时用我们自己引用的包,而运行时用tomcat自己的包,这样就不用冲突了,又能正常开发代码.

看完记得点赞哦!!!

 

ion.class, args);}}

你可能感兴趣的:(搭建springboot)