Spring Boot 整合mybatis 与 swagger2
程序员文章站
2024-02-15 09:12:46
之前使用springmvc+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到了spring boot 后发现...
之前使用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地址:)
首先是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地址:
总结
以上所述是小编给大家介绍的spring boot 整合mybatis 与 swagger2,希望对大家有所帮助
上一篇: C#不可变类型深入解析