1. 程式人生 > >Spring全家桶系列–[SpringBoot入門到跑路]

Spring全家桶系列–[SpringBoot入門到跑路]

//本文作者: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

  1. 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/ 生成一個初始專案

生成SpringBoot

這裡會下載一個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相當於 @[email protected]
* 使用@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.下期

  1. mapper.xml、dao介面、實體類自動生成
  2. 整合一個很nice的開發模板
  3. CRUD操作以及整合PageHelper分頁
  4. AOP全域性的異常進行處理