//本文作者:cuifuan
Spring全家桶————[SpringBoot入門到跑路]
對於之前的Spring框架的使用,各種配置文件XML、properties一旦出錯之后錯誤難尋,這也是為什么SpringBoot被推上主流的原因,SpringBoot的配置簡單,說5分鍾能從框架的搭建到運行也不為過,現在更是微服務當道,所以在此總結下SpringBoot的一些知識,新手教程。
1.在官網快速創建SpringBoot項目
Gradle是一個基於Apache Ant和Apache Maven概念的項目自動化構建開源工具,它使用一種基於Groovy語言來聲明項目設置.也就是和Maven差不多的項目構建工具,為何要使用Gradle,舉例:
maven要引入依賴 pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.15.RELEASE</version> </dependency>
而Gradle引入 build.gradle
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.15.RELEASE'
很清晰明了,依賴管理比maven強,腳本編寫比Ant好用,Google都在使用了,趕緊上手吧!
Gradle本地安裝教程
windows :https://www.cnblogs.com/linkstar/p/7899191.html
Mac_OS :https://www.jianshu.com/p/e9d035f30876
下面開始進入正題:
進入 https://start.spring.io/ 生成一個初始項目
這里會下載一個zip的項目壓縮包
2. 使用Gradle導入SpringBoot項目
demo.zip解壓之后記得復制下demo文件夾放的路徑
在此用的開發工具是IntelliJ IDEA
下面是導入流程:
IDEA里點擊File -> Open -> 粘貼剛剛的demo文件夾路徑 -> 找到build.gradle雙擊
-> Open as Peoject -> 等待Gradle加載完就好,看不明白看下圖
打開之后Gradle加載下載的特別慢,要換成國內源,打開build.gradle配置文件用下面的替換
build.gradle
/** buildscript中的聲明是gradle腳本自身需要使用的資源。 * 可以聲明的資源包括依賴項、第三方插件、maven倉庫地址等 */ buildscript { ext { springBootVersion = '1.5.6.RELEASE' } repositories { //使用國內源下載依賴 maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } // 應用Java插件 apply plugin: 'java' //讓工程支持IDEA的導入 apply plugin: 'idea' apply plugin: 'org.springframework.boot' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 //build.gradle文件中直接聲明的依賴項、倉庫地址等信息是項目自身需要的資源。 repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } /** * 在gradle里,對依賴的定義有6種 * compile, runtime, testCompile, testRuntime, providedCompile,providedRuntime * compile:需要引用這個庫才能進行編譯工作 * testRuntime : 測試依賴范圍 * 其他的了解:http://shmilyaw-hotmail-com.iteye.com/blog/2345439 */ dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compile 'com.alibaba:druid:1.0.29' }
3. SpringBoot項目啟動
啟動前准備,依據下圖把 DemoApplication 啟動類移到 demo 文件夾的同級;
啟動類相當於管理項目的負責人,你把他扔到與控制層同級肯定出錯不是;
然后把demo包改名為controller並新建TestController類
TestController.java
package com.example.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 這里的@RestController相當於 @ResponseBody+@Controller * 使用@RestController 相當於使每個方法都加上了 @ResponseBody 注解 * created by cfa 2018-11-06 下午 11:30 **/ @RestController public class TestController { /** * 這里的@GetMapping相當於@RequestMapping(value = "/hello", method = RequestMethod.GET) * created by cfa 2018-11-06 下午 11:29 **/ @GetMapping("hello") public String test(){ return "i love java"; } }
啟動成功之后訪問 http://localhost:8080/hello
上圖成功代表項目可以訪問了
4.配置application.yml
什么是yml?
YML文件格式是YAML (YAML Aint Markup Language)編寫的文件格式,YAML是一種直觀的能夠被電腦識別的的數據數據序列化格式,並且容易被人類閱讀,容易和腳本語言交互的, 可以被支持YAML庫的不同的編程語言程序導入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。引自:https://www.cnblogs.com/hanson1/p/7105248.html
聽不懂吧,其實我也看不明白
就是相當於xml,properties的配置文件,看的更直觀,上代碼吧還是
# 下述properties spring.resources.locations= classpath:/templates # 改為yml格式之后 spring: resources: static-locations: classpath:/templates
yml需要注意,冒號(:)后面要跟空格,第二級和第一級要在上下行用一個Tab的距離
application.yml
server: port: 8080 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/ceilan?characterEncoding=utf-8 username: root password: 123456 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 mvc: view: suffix: .html resources: static-locations: classpath:/templates
5.下期
- mapper.xml、dao接口、實體類自動生成
- 集成一個很nice的開發模板
- CRUD操作以及集成PageHelper分頁
- AOP全局的異常進行處理