新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章给大家分享的是有关使用Spring Boot 怎么对mybatis与swagger2进行整合,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
十多年专注成都网站制作,企业网站建设,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站建设,高端网页制作,对成都三维植被网等多个领域,拥有丰富的网站营销经验。
首先是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/*.xml")); sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
最后就是swagger的配置了,
package com.example; 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 springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Created by lhm on 2015/8/27. */ @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(); } }
以上就是使用Spring Boot 怎么对mybatis与swagger2进行整合,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。