在很早之前,我發過javaweb系列教程,探索了從spring框架的配置整合到springboot,中間還講解了一個博客小項目。具體信息請訪問:https://acheng1314.cncss
本項目的GitHub:https://github.com/pc859107393/Go2SpringBoot.githtml
有興趣交流springboot進行快速開發的同窗能夠加一下下面的企鵝羣。java
在很早之前,個人項目中探索Spring+SpringMvc+Druid+Mybatis框架整合的時候,咱們但是花了兩個章節詳細的討論spring框架的整合,一樣的哪怕是一個熟練的Java程序員要搭建一個完整穩定的框架也要很長的時間。 可是springboot解決了這個問題,筆者如今搭建一個springboot項目僅僅須要幾分鐘時間。下面咱們一塊兒一點點的看下去。mysql
按照前面項目的國際慣例,使用IntelliJ IDEA構建項目,這一次仍是像之前同樣貼出圖片。react
①. 在idea的歡迎界面,咱們選擇Create New Project
,如圖1.1所示。git
圖1.1 歡迎界面程序員
②. 接着咱們選擇左側的Spring Initializr
而後點擊next
,進入Project Metadata
界面,如圖1.2所示。github
圖1.2 選擇Spring項目初始化web
③. 緊接着開始配置項目的基本參數,咱們在這裏一點點的實現,如圖1.3所示。spring
圖1.3 配置項目基本參數
在圖1.3中,咱們須要着重注意的是: 項目類型設置爲Gradle project,開發語言我選擇的是kotlin,打包方式jar,java語言版本是8,其餘的參數你們自行百度
④. 完成了上面的項目參數配置後,咱們接着選擇項目資源依賴。項目資源依賴也就是咱們須要使用哪些jar包擴展。界面如圖1.4所示。
圖1.4 選擇項目依賴資源
在上面的圖1.4中,左邊是資源的父類別,中間是具體的資源詳細名稱,右邊是咱們選中的依賴,一樣的已經把目錄層級展現出來了,你們請根據我選中的作出相應的選擇。注意:SpringBoot版本咱們默認就行。
⑤. 當咱們把圖1.4點擊next
後,進入了一個項目目錄命名環節,咱們能夠取本身心儀的名字。注意:寫完名字不要當即點擊finish!寫完名字不要當即點擊finish!寫完名字不要當即點擊finish!
在這裏咱們有一個很值得注意的小細節,在這裏咱們能夠加快項目構建速度。
1. gradle項目的構建是須要gradle環境的。
2. gradle和maven同樣是進行遠程資源依賴的,因此合理的遠程資源倉庫能夠加快構建速度(資源下載速度快了,減小大部分等待時間)。
3. 修改文件內容 項目目錄->gradle->wrapper->gradle-wrapper.properties 中的distributionUrl的值爲:http\://7xlmzq.com1.z0.glb.clouddn.com/gradle-4.5.1-bin.zip
4. 在項目中的build.gradle文件中修改repositories字段相關的內容。添加以下內容:
maven { url "http://maven.aliyun.com/nexus/content/repositories/central" }
複製代碼
⑥. 作完這兩步後,咱們就能夠選擇finish進入項目中。 大概模樣如圖1.5所示。
圖1.5 項目構建完成
在傳統的JavaWeb應用中,咱們通常採用經典三層來解決問題,經典三層指: dao->service->web,一樣的咱們這裏也先來整合這三層。
①. 打開build.gradle
,查看底部的dependencies
包含哪些依賴。這個時候項目的依賴應該以下所示:
dependencies {
//aop支持
compile('org.springframework.boot:spring-boot-starter-aop')
//緩存
compile('org.springframework.boot:spring-boot-starter-cache')
//快捷生成RESTFul文檔
compile('org.springframework.boot:spring-boot-starter-data-rest')
//模板引擎
compile('org.springframework.boot:spring-boot-starter-freemarker')
//數據校驗
compile('org.springframework.boot:spring-boot-starter-validation')
//傳統的web
compile('org.springframework.boot:spring-boot-starter-web')
//新的webflux
compile('org.springframework.boot:spring-boot-starter-webflux')
//mybatis的支持
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
//Kotlin支持
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
//springweb項目快速重啓
runtime('org.springframework.boot:spring-boot-devtools')
//mysql連接
runtime('mysql:mysql-connector-java')
//測試支持
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.projectreactor:reactor-test')
//在上面的依賴中 compile是任什麼時候候都須要的依賴,runtime是牢牢在運行時須要依賴,testCompile是在測試的編譯和運行時候均須要。
}
複製代碼
在之前個人系列教程中,咱們主要採用了哪些框架呢?主要採用了:Spring+SpringMvc+Mybatis+Druid+SpringFox,因此這些組成一個完整web應用的框架集合是不能少的,因此咱們須要加入下面的一些依賴。
compile 'com.alibaba:druid-spring-boot-starter:1.1.9'
compile 'com.google.code.gson:gson:2.7'
//mybatis-plus插件支持
compile 'com.baomidou:mybatis-plus:2.3'
compile 'com.baomidou:mybatis-plus-boot-starter:2.3'
compile "io.springfox:springfox-swagger2:${springfoxVersion}"
compile "io.springfox:springfox-staticdocs:2.6.1"
compile "io.springfox:springfox-swagger-ui:${springfoxVersion}"
compile 'com.github.xiaoymin:swagger-bootstrap-ui:1.7.2'
複製代碼
我的緣由常用gson,上面的springfoxVersion='2.8.0'
接着咱們刷新gradle或者選擇右下角提示的 Import Changes,導入咱們加入的依賴資源。到這一步咱們的依賴添加完畢。
其實框架整合無外乎就是這些基於Spring的bean模式構建的各個bean協調工做,這一點在傳統手動配置的Spring應用中是一個很難很繁瑣的過程。可是咱們springboot時代,一切都簡化了,咱們只須要找到相應資源的官方文檔,參照文檔進行整合就好了。
我整合的配置文件application.properties
以下:
debug=false
trace=false
# Druid鏈接池配置,官方配置參考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.druid.url=jdbc:mysql://localhost:3306/cc_db?useUnicode=true&characterEncoding=utf8&autoReconnect=true
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=laopo5201314
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
# 配置StatFilter
spring.datasource.druid.filter.stat.db-type=mysql
spring.datasource.druid.aop-patterns= -cn.acheng1314.*
# 配置WallFilter
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=mysql
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false
## Druid WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.filter.stat.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=
#事物提交失敗回滾和aop
spring.transaction.rollback-on-commit-failure=true
spring.aop.auto=true
spring.aop.proxy-target-class=true
spring.http.encoding.force=true
spring.http.encoding.force-request=true
spring.http.encoding.charset=utf-8
spring.http.converters.preferred-json-mapper=jackson
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=1024mb
spring.servlet.multipart.max-request-size=1024mb
#freemarker配置
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=true
spring.freemarker.content-type=text/html
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.enabled=true
spring.freemarker.suffix=.ftl
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=req
server.port=8181
spring.resources.static-locations=classpath:/static/
#Mybatis配置,官方參考:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
mybatis.mapper-locations=classpath:/mapper/*.xml
mybatis.type-aliases-package=cn.acheng1314.base.domain
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.configuration.use-column-label=true
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.type-aliases-package=cn.acheng1314.base.domain
#主鍵類型 0:"數據庫ID自增", 1:"用戶輸入ID",2:"全局惟一ID (數字類型惟一ID)", 3:"全局惟一ID UUID";
mybatis-plus.global-config.id-type=0
#字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
mybatis-plus.global-config.field-strategy=0
#駝峯下劃線轉換
mybatis-plus.global-config.db-column-underline=true
#刷新mapper 調試神器
mybatis-plus.global-config.refresh-mapper=true
mybatis-plus.global-config.capital-mode=true
mybatis-plus.configuration.cache-enabled=true
mybatis-plus.configuration.map-underscore-to-camel-case=true
#配置文件設置請參考官網文檔:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#production-ready-endpoints
複製代碼
在上面的配置文件中,咱們在項目開發目錄中resources
對應編譯後的目錄爲classes
。
至此爲止,咱們的框架整合差很少完成,具體的細節,我建議你們仍是去官網文檔查看一下。固然最重要的仍是框架整合的基礎思想,有興趣的童鞋能夠查看我之前的相關教程。
①. 測試json輸出,代碼以下:
@GetMapping(value = ["/"], produces = [MediaType.APPLICATION_JSON_UTF8_VALUE])
@ResponseBody
fun MainLocal(): Any = User("程", "18976962315", "123456", "吹牛", Date())
複製代碼
②. 測試freemarker頁面渲染
@GetMapping(value = ["/test"], produces = [MediaType.TEXT_HTML_VALUE])
fun getTest(map: ModelMap): String {
map["test"] = MainLocal()
return "test1"
}
複製代碼
具體的測試頁面不用再放出來了,一樣這種相似的測試也能夠考慮使用Spring的相關測試框架來檢查。