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

Spring Boot 整合mybatis 与 swagger2

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

Spring Boot 整合mybatis 与 swagger2

之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到了spring boot 后发现搭建一个web项目真的是1分钟的事情,再也不用去管那么多xml配置,简直神清气爽,不用看那么多一坨xml,下面是我把以前的一些ssm项目改成了spring boot + mybatis,相对于来说优点太明显了

1. 创建独立的Spring应用程序

2. 嵌入的Tomcat,无需部署WAR文件

3. 简化Maven配置

4. 自动配置Spring

5. 提供生产就绪型功能,如指标,健康检查和外部配置

6. 绝对没有代码生成和对XML没有要求配置

这个是百度百科上面对spring boot 优点的描述,其实我感觉也是这样,大家可以试一下。

以下是具体的代码实现(github地址:https://github.com/yihec/spring-boot-mybatis) 

首先是pom文件的一些依赖



  4.0.0
  com.example
  demo 2
  0.0.1-SNAPSHOT
  jar
  demo 2
  Demo project for Spring Boot
  
   org.springframework.boot
   spring-boot-starter-parent
   1.5.1.RELEASE
    
  
  
   UTF-8
   UTF-8
   1.7
  
  
   
     org.springframework.boot
     spring-boot-starter-aop
   
   
   
     org.springframework.boot
     spring-boot-starter-web
     
      
 org.springframework.boot
 spring-boot-starter-logging
      
     
   
   
     org.springframework.boot
     spring-boot-starter-jdbc
   
   
     com.oracle
     ojdbc14
     10.2.0.1.0
   
   
   
     org.springframework.boot
     spring-boot-starter-redis
   
   
     org.springframework.boot
     spring-boot-starter-test
     test
   
   
   
     org.mybatis
     mybatis-spring
     1.2.2
   
   
     org.mybatis
     mybatis
     3.2.8
   
   
     org.apache.commons
     commons-lang3
     3.5
   
   
   
   
     com.github.pagehelper
     pagehelper
     4.1.6
   
   
     org.codehaus.jackson
     jackson-mapper-asl
     1.9.13
   
   
   
     io.springfox
     springfox-swagger2
     2.2.2
   
   
     io.springfox
     springfox-swagger-ui
     2.2.2
   
   
     org.springframework.boot
     spring-boot-starter-thymeleaf
   
   
     org.springframework.boot
     spring-boot-starter-log4j
   
   
  
   
     
      org.springframework.boot
      spring-boot-maven-plugin
     
   
  
  
   
     spring-snapshots
     Spring Snapshots
     https://repo.spring.io/snapshot
     
      true
     
   
   
     spring-milestones
     Spring Milestones
     https://repo.spring.io/milestone
     
      false
     
   
  
  
   
     spring-snapshots
     Spring Snapshots
     https://repo.spring.io/snapshot
     
      true
     
   
   
     spring-milestones
     Spring Milestones
     https://repo.spring.io/milestone
     
      false
     
   
  

接着是application.yml数据库配置

spring:
 datasource:
   driver-class-name: oracle.jdbc.OracleDriver 
   url: jdbc:oracle:thin:localhost:1521:orcl
   username: orcl
   password: orcl

然后是application.java

package com.example;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@SpringBootApplication
//@ComponentScan 注解自动收集所有的Spring组件,包括 @Configuration 类。
@ComponentScan
@MapperScan("com.example.mapper")
public class DemoApplication {
  @Bean
  @ConfigurationProperties(prefix="spring.datasource")
  public DataSource dataSource() {
    return new org.apache.tomcat.jdbc.pool.DataSource();
  }
  @Bean
  public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //这里的话如果不用mybatis-config.xml 可以把下面那句给注释掉
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
  @Bean
  public Docket createRestApi() {
    return new Docket(documentationType.SWAGGER_2)
 .apiInfo(apiInfo())
 .select()
 .apis(RequestHandlerSelectors.basePackage("com.example.web"))
 .paths(PathSelectors.any())
 .build();
  }
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
 .title("API")
 .description("")
 .termsOfServiceUrl("")
 .contact("yihec")
 .version("1.0")
 .build();
  }
}

最主要的代码就是这些了,然后大功告成,后面的都是个人的业务逻辑了。

github地址:https://github.com/yihec/spring-boot-mybatis

总结

以上所述是小编给大家介绍的Spring Boot 整合mybatis 与 swagger2,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

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

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

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