SpringBoot2.0—使用Druid、Mybatis和Redis(一)

本篇只簡單講述簡的環(huán)境搭建,如題,在SpringBoot2.0中集成Druid(阿里的那個(gè)數(shù)據(jù)源)、Mybatis(sql寫在xml中的配置)和redis(最常用的緩存數(shù)據(jù)庫)。本項(xiàng)目只限提供RestAPI服務(wù),不涉及網(wǎng)站頁面文件(靜態(tài)和動態(tài))的支持。


  1. 先看pom.xml
   <!-- SpringBoot版本 -->
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <!-- 主要的幾個(gè)依賴,現(xiàn)在主流的框架都支持springboot了,省去了那一大推依賴 -->
        <!-- web服務(wù)starter --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- redis的starter --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!-- druid的starter --> 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <!-- mybatis的starter --> 
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 其他輔助依賴,對,總共就這么幾個(gè),沒有了,是不是很清爽 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

2.項(xiàng)目結(jié)構(gòu)


一個(gè)完整的RestAPI服務(wù)架構(gòu),就這點(diǎn)東西

3.springBoot主要配置-application.properties,幾乎所有的配置都在這里了,已附上官網(wǎng)配置參考鏈接,想深入的同學(xué)可以自行閱讀

debug=false
trace=false

# Druid連接池配置,官方配置參考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.druid.url=jdbc:mysql://10.168.0.24:3306/dmp_db?useUnicode=true&characterEncoding=utf8
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=10
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# Druid WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*

# Druid StatViewServlet配置,說明請參考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

#Mybatis配置,官方參考:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml

# Redis配置,官方參考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#common-application-properties
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=5000

4.啟動類Application.java的代碼,也是少的可憐;這里唯一需要注意的就是Mybatis的配置這里需要指定一下掃描的mapper包路徑(其實(shí)覺得完全可以放在application.properties中指定,但本人親測,只有放在這里管用,具體原理還沒細(xì)糾)

@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5.其他廢話
除了上述幾個(gè)配置之外,其他的代碼跟普通的spring項(xiàng)目一樣,沒有任何區(qū)別。雖說springboot雖然還需要一些配置,但整體來說已經(jīng)比傳統(tǒng)的項(xiàng)目配置要少的多了,主要在于一些大廠都已經(jīng)開始支持springboot了,于是就有了各種各樣的starter。需要哪個(gè)服務(wù)的支持,可以直接去github上搜對應(yīng)的starter就可以了。最后附上springboot的官方配置連接,還是很詳細(xì)的。
SpringBoot官方配置參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容