前提:
- 本地安裝了jdk,並配置系統變量JAVA_HOME,Path
- 本地下載了gradle-4.10u並配hu系統變量GRAhuLEchHOME,Path
1.打開IDEA,創建project項目
2.項目管理工具選擇gradle,點擊Next
3.勾選項目基本組成部分
4.點擊Finish創建項目
5.項目目錄結構如下,標紅部分是自己創的
注意,如果demoo2項目文件夾爲普通文件,無法新建package,需要點擊如下圖標,將src標註爲Source
6.編輯WelcomeController的內容
7.編輯index.html的內容
8.配置build.gradle的內容
文本如下
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile group:'org.springframework.boot',name:'spring-boot-devtools',version: '2.0.4.RELEASE'//熱部署
}
9.配置application.properties文件,內容如下
文本如下:
spring.datasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&CharacterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.thymeleaf.cache=false
10.配置完畢,啓動項目
注意,默認端口是8080,你可以在application.properties文件中添加server.port=8090進行修改端口號,瀏覽器中輸入:
localhost:8090/welcome即可。
spring boot默認是集成html頁面的,如果頁面需要傳參,那麼就需要集成jsp
SpringBoot集成jsp
1.application.properties文件內容更改爲:
spring.datasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&CharacterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#主要配置
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/,classpath:/static,classpath:/templates
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
2.build.gradle依賴如下
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.zy'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//注意點
compile 'javax.servlet:jstl:1.2'
compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
compile 'org.springframework.boot:spring-boot-starter-web'
compile group:'javax.servlet',name:'javax.servlet-api'
compile group:'org.springframework.boot',name:'spring-boot-starter-tomcat'
}
3.新增目錄結構:
4.啓動類集成SpringBootServletInitializer類,重寫 configure 方法
代碼如下:
package com.zy.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
啓動項目正常訪問即可