欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 整合mybatis 與 swagger2

 更新時(shí)間:2017年08月01日 16:22:57   作者:也許明天1  
之前使用springMVC+spring+mybatis,總是被一些繁瑣的xml配置,還經(jīng)常出錯(cuò),下面把以前的一些ssm項(xiàng)目改成了spring boot + mybatis,相對(duì)于來(lái)說(shuō)優(yōu)點(diǎn)太明顯了,具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧

之前使用springMVC+spring+mybatis,總是被一些繁瑣的xml配置,有時(shí)候如果配置出錯(cuò),還要檢查各種xml配置,偶然接觸到了spring boot 后發(fā)現(xiàn)搭建一個(gè)web項(xiàng)目真的是1分鐘的事情,再也不用去管那么多xml配置,簡(jiǎn)直神清氣爽,不用看那么多一坨xml,下面是我把以前的一些ssm項(xiàng)目改成了spring boot + mybatis,相對(duì)于來(lái)說(shuō)優(yōu)點(diǎn)太明顯了

1. 創(chuàng)建獨(dú)立的Spring應(yīng)用程序

2. 嵌入的Tomcat,無(wú)需部署WAR文件

3. 簡(jiǎn)化Maven配置

4. 自動(dòng)配置Spring

5. 提供生產(chǎn)就緒型功能,如指標(biāo),健康檢查和外部配置

6. 絕對(duì)沒(méi)有代碼生成和對(duì)XML沒(méi)有要求配置

這個(gè)是百度百科上面對(duì)spring boot 優(yōu)點(diǎn)的描述,其實(shí)我感覺(jué)也是這樣,大家可以試一下。

以下是具體的代碼實(shí)現(xiàn)(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>
   <!-- 移除內(nèi)置的log的依賴使用log4j進(jìn)行日志輸出-->
   <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>
   <!--導(dǎo)入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的分頁(yè)插件 很好用的一個(gè) 具體使用方法可以去下面的網(wǎng)站 -->
   <!-- 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數(shù)據(jù)庫(kù)配置

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 注解自動(dòng)收集所有的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();
  }
}

最主要的代碼就是這些了,然后大功告成,后面的都是個(gè)人的業(yè)務(wù)邏輯了。

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

總結(jié)

以上所述是小編給大家介紹的Spring Boot 整合mybatis 與 swagger2,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理

    如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理

    這篇文章主要介紹了如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理。在 Java 中,錯(cuò)誤的處理在傳統(tǒng)上由異常以及創(chuàng)建和傳播異常的語(yǔ)言支持進(jìn)行。但是,如果不存在結(jié)構(gòu)化異常處理又如何呢?,需要的朋友可以參考下
    2019-06-06
  • Mybatis插件PageHelper的實(shí)現(xiàn)原理詳解

    Mybatis插件PageHelper的實(shí)現(xiàn)原理詳解

    PageHelper 是一款開(kāi)源的 MyBatis 分頁(yè)插件,可以在實(shí)際應(yīng)用中方便地實(shí)現(xiàn)分頁(yè)功能,這篇文章主要來(lái)和大家講講PageHelper的原理與使用,需要的可以參考下
    2023-06-06
  • springboot項(xiàng)目編寫(xiě)發(fā)送異常日志到企微工具包的操作方法

    springboot項(xiàng)目編寫(xiě)發(fā)送異常日志到企微工具包的操作方法

    本文介紹了Springboot項(xiàng)目如何編寫(xiě)發(fā)送異常日志到企業(yè)微信的工具包,內(nèi)容包括創(chuàng)建基礎(chǔ)Bean、配置類、pom依賴等步驟,并展示了如何通過(guò)nacos進(jìn)行配置,這為開(kāi)發(fā)者提供了一種有效的日志管理方案,方便快速定位和處理項(xiàng)目中的異常問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • springboot多數(shù)據(jù)源配置及切換的示例代碼詳解

    springboot多數(shù)據(jù)源配置及切換的示例代碼詳解

    這篇文章主要介紹了springboot多數(shù)據(jù)源配置及切換,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java四種常用線程池的詳細(xì)介紹

    Java四種常用線程池的詳細(xì)介紹

    今天小編就為大家分享一篇關(guān)于Java四種常用線程池的詳細(xì)介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • Java類的初始化順序知識(shí)點(diǎn)總結(jié)

    Java類的初始化順序知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于Java類的初始化順序知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟)

    IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟)

    這篇文章主要介紹了IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • java 中 zookeeper簡(jiǎn)單使用

    java 中 zookeeper簡(jiǎn)單使用

    ZooKeeper是一個(gè)分布式的,開(kāi)放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開(kāi)源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。下面通過(guò)本文給大家分享java 中 zookeeper簡(jiǎn)單使用,需要的朋友參考下吧
    2017-09-09
  • Mybatis plus實(shí)現(xiàn)Distinct去重功能

    Mybatis plus實(shí)現(xiàn)Distinct去重功能

    這篇文章主要介紹了Mybatis plus實(shí)現(xiàn)Distinct去重功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java Web學(xué)習(xí)之MySQL在項(xiàng)目中的使用方法

    Java Web學(xué)習(xí)之MySQL在項(xiàng)目中的使用方法

    mysql數(shù)據(jù)庫(kù)是我們?cè)谌粘i_(kāi)發(fā)中經(jīng)常會(huì)用到的,下面這篇文章主要給大家介紹了關(guān)于Java Web學(xué)習(xí)之MySQL在項(xiàng)目中的使用方法,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04

最新評(píng)論