目標(biāo),輸出 Hello SpringBoot !
準(zhǔn)備
- 瀏覽器
- jdk8
- idea(自備 ide 技能均可)
新建項(xiàng)目
進(jìn)入 Spring 官網(wǎng)
點(diǎn)擊 【Start a new Spring Project】
直達(dá)
![圖片](https://dn-coding-net-production-pp.qbox.me/6a0d8dd8-2227-4629-be49-db51e11b9467.png)
圖片
推薦選擇 grandle, 配置文件清爽不少
選擇如下
![圖片](https://dn-coding-net-production-pp.qbox.me/8ff28733-a1de-4e90-975c-19e7069a5d2f.png)
圖片
簡(jiǎn)單介紹
各個(gè)組件的作用
Web: 集成 SpringMVC
Security:
JPA:
Actuator:
Thymeleaf:
Mysql:
Lombok:
HSQLDB:
Idea 導(dǎo)入
解壓后到目錄下改 build.gradle 中 elipse 為 idea,
這是因?yàn)槲矣玫?idea, eclipse 的話(huà)應(yīng)該不用改,不過(guò)我沒(méi)試過(guò)挺勿。
然后直接在目錄下執(zhí)行
╰─$ ./gradlew idea
完成后便可以直接用 idea 打開(kāi)了
![圖片](https://dn-coding-net-production-pp.qbox.me/36fbb00f-edf9-4fed-976d-ec648952e277.png)
圖片
記得要 使用 import
![圖片](https://dn-coding-net-production-pp.qbox.me/43fc4a89-106f-4c85-91e0-52316cf90742.png)
圖片
點(diǎn)擊 Finish 完成,等ok。。款筑。
啟動(dòng)
運(yùn)行 DaxueApplication.java
2017-01-05 09:01:27.132 WARN 53547 --- [ main] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-01-05 09:01:27.138 ERROR 53547 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
缺少數(shù)據(jù)庫(kù)配置
解決:在 application.properties 里加數(shù)據(jù)庫(kù)配置,自己在 MySQL 里新建一個(gè)數(shù)據(jù)庫(kù)。
spring.datasource.url=jdbc:mysql://localhost:3306/daxue
spring.datasource.username=root
spring.datasource.password=123qwe
# update 模式會(huì)自動(dòng)建表腾么,有改動(dòng)后也會(huì)更新奈梳,不過(guò)不會(huì)刪原來(lái)的東西
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.connection.release_mode=auto
OK,啟動(dòng)完畢看到結(jié)果
此時(shí)目錄結(jié)構(gòu)如下
╰─$ tree -a
.
├── .gitignore
├── .gradle
│ └── 2.13
│ └── taskArtifacts
│ ├── cache.properties
│ ├── cache.properties.lock
│ ├── fileHashes.bin
│ ├── fileSnapshots.bin
│ └── taskArtifacts.bin
├── build.gradle
├── daxue.iml
├── daxue.ipr
├── daxue.iws
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
├── main
│ ├── java
│ │ └── cn
│ │ └── dxs2s
│ │ ├── DaxueApplication.java
│ │ ├── domain
│ │ ├── service
│ │ └── web
│ │ └── IndexController.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── cn
└── dxs2s
└── DaxueApplicationTests.java
沒(méi)有 MySQL 哮翘?
木有關(guān)系颈嚼,這么辦
![圖片](https://dn-coding-net-production-pp.qbox.me/d2755456-b76b-4641-85ee-bd046b80b4ff.png)
圖片
hslqdb 是一個(gè)內(nèi)存數(shù)據(jù)庫(kù),一般用來(lái)測(cè)試用饭寺,在使用 JPA 的方式下可以無(wú)縫切換到 MySQL阻课。
沒(méi)有 MySQL 的環(huán)境可以暫時(shí)頂用一下。
再次啟動(dòng)
訪問(wèn) http://localhost:8080
![圖片](https://dn-coding-net-production-pp.qbox.me/32f3645a-a150-48b9-8196-1acde5b9c4bf.png)
圖片
SpringSecurity 的默認(rèn)基本安全配置艰匙,密碼請(qǐng)?jiān)趩?dòng)的日志中自行尋找限煞。
目標(biāo)達(dá)成!
![圖片](https://dn-coding-net-production-pp.qbox.me/6ee9e603-192d-4d03-9050-a5d2d6799d4b.png)
圖片