風吹來的消息
今天看了一篇博客Spring Boot 把 Maven 幹掉了!就一直想自己構建下。
生成項目
進入這個網站生成SpringBoot的Demo項目。
如下:
導出項目之後解壓放到某個目錄下然後倒入IDEA。
這幾個就包含了很多很多的核心依賴基本導入之後,我們不需要太關心Spring相關的東西,如果要用第三方直接依賴,具體見下文的build.gradle。
導入項目
選擇open,然後選擇你的解壓好的項目,一般解壓好就叫demo,衝突的話自己改名。
然後選擇一個JDK環境就OK了直接下一步。
圖中右下角的問題是因爲我的gradle版本太高了我的idea版本太低導致的,
但是設置的版本太低或者不是指定版本的就會報錯如下圖:
所以爲了避免上邊的問題我們設置5.6.1,設置完之後,會出現下圖中顯示的右下角的彈窗,選擇Enable Auto-Import。
然後我們選擇Build -> Build Project
我們需要打開 module settings裏邊選擇我們已經安裝的JDK。
先選中項目根目錄,Command + 下鍵或者我的Fn + F4都可以打開。
然後再次選擇Build -> Build Project
項目配置
大部分項目配置我們再生成的時候已經選擇好了。
build.gradle內容
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.lyd'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
implementation 'com.alibaba:druid-spring-boot-starter:1.1.10'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
application.properties文件中的內容
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/credit?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#set tomcat port context
#端口被佔用的話就換一個
server.port=8082
server.servlet.context-path=/demo
在新版當中server.context-path已經失效了設置了統一的訪問路徑之後不生效,要修改成server.servlet.context-path。
application.yml文件中的內容
#spring的配置
spring:
datasource:
name: sqldata
url: jdbc:mysql://localhost:3306/credit?characterEncoding=UTF-8 #配置在 配置中心
username: root #配置在 配置中心
password: 123456 #配置在 配置中心
# 使用druid數據連接池
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x' FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
aop:
proxy-target-class: true
#把內置的tomcat 參數 配置在 properties文件中,以便日後使用配置中心
# #tomcat容器的配置
#server:
# context-path: /lyd
# port: 8082
#mybatis的配置
mybatis:
type-aliases-package: com.lyd.demo.mapper
mapper-locations: classpath*:mappings/**/*.xml
簡單的測試代碼
SpringBoot的引導配置
我們可以參考step2中的代碼:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
啓動服務
我們是gradle項目,我們使用
./gradlew rootRun
等進度到75%會卡住沒關係,當我們看到下圖沒有報錯沒有端口占用,就是成功了:
接下來我們訪問http://localhost:8082/demo/hello?name=xxxxxx
成功!
停止服務
看了很多停止服務的比較簡單的就是,查看服務進程ID直接執行:
kill -9 [pid] 如:kill -9 800
那我們如何查看pid呢?
第一種:
ps -ef | grep [Application名稱,只要能查到就行]
如:ps -ef | grep DemoApplication
然後可以看到下圖的第二個數字就是進程Id;然後執行kill -9 1268
第二種:
我們在執行./gradlew bootRun的時候會看到下圖所示:
有個進程ID是不是和我們第一種方式查到的一致。
好了我們執行kill -9 1268
還有一點就是我們殺死之後重新執行./gradlew bootRun啓動服務的時候要多等會可能啓動沒那麼快,導致我們一直請求404。