Spring Boot 整合mybatis 与 swagger2

作者:也许明天1 时间:2022-05-01 06:32:17 

之前使用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文件的一些依赖


<?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.example</groupId>
 <artifactId>demo 2</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>demo 2</name>
 <description>Demo project for Spring Boot</description>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </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-aop</artifactId>
  </dependency>
  <!-- 移除内置的log的依赖使用log4j进行日志输出-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
     <exclusion>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-logging</artifactId>
     </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.1.0</version>
  </dependency>
  <!-- redis -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <!--导入mybatis -->
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.2</version>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.8</version>
  </dependency>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.5</version>
  </dependency>
  <!--mybatis的分页插件 很好用的一个 具体使用方法可以去下面的网站 -->
  <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
  <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
  </dependency>
  <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
  </dependency>
  <!--swagger2 生成api文档 -->
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
  </dependency>
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.2.2</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
  </dependency>
  </dependencies>
 <build>
  <plugins>
    <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
 </build>
 <repositories>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
     <enabled>true</enabled>
    </snapshots>
  </repository>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
     <enabled>false</enabled>
    </snapshots>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
     <enabled>true</enabled>
    </snapshots>
  </pluginRepository>
  <pluginRepository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
     <enabled>false</enabled>
    </snapshots>
  </pluginRepository>
 </pluginRepositories>
</project>

接着是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();
 }
}

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

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

总结

以上所述是小编给大家介绍的Spring Boot 整合mybatis 与 swagger2网站的支持!

来源:http://www.cnblogs.com/yihec/archive/2017/08/01/7268792.html

标签:spring,boot,swagger2,mybatis
0
投稿

猜你喜欢

  • Java系统变量参数获取设置System.getProperties()的方法

    2021-07-22 04:52:47
  • Spring Security 中细化权限粒度的方法

    2022-08-30 14:26:40
  • Dwr3.0纯注解(纯Java Code配置)配置与应用浅析一之零配置文件化

    2022-06-05 05:20:17
  • SpringCloud迈向云原生的步骤

    2023-08-21 22:59:46
  • Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法

    2022-09-18 11:18:22
  • 使用Java和ffmpeg把音频和视频合成视频的操作方法

    2022-08-22 07:30:20
  • C#使用protobuf-net进行序列化的详细操作

    2021-11-17 02:32:41
  • java使用jaxb操作xml示例

    2023-11-05 15:02:18
  • java 实现将一个string保存到txt文档中

    2022-07-30 05:32:38
  • 浅谈java项目与javaweb项目导入jar包的区别

    2023-11-11 11:06:19
  • SpringBoot、mybatis返回树结构的数据实现

    2022-05-12 18:56:08
  • Android基于AlarmManager实现用户在线心跳功能示例

    2021-12-02 20:26:46
  • jvm垃圾回收之GC调优工具分析详解

    2023-08-17 12:40:20
  • IDEA连接Mysql数据库的详细图文教程

    2023-10-09 09:51:24
  • Java Socket使用加密协议进行传输对象的方法

    2023-11-28 12:47:44
  • C# DataTable 转换为 实体类对象实例

    2021-12-25 02:14:51
  • AndroidStudio实现能在图片上涂鸦程序

    2023-09-14 06:57:02
  • spring定义和装配bean详解

    2023-08-23 00:33:18
  • C# 如何调用SAP RFC

    2023-07-02 18:11:42
  • Java BigDecimal除法精度和格式化输出方式

    2023-01-06 01:03:42
  • asp之家 软件编程 m.aspxhome.com