1、使用idea工具建立springboot項目
第一步:
後續直接點next,下一步
直到進入依賴選擇頁面,Web選擇Spring Web,Template Engines選擇Thymeleaf(模闆引擎若在項目建立之後需要改動,可在application.yml進行配置即可),SQL選擇JDBC API,Mybatis,MySQL Driver
2、建好項目後會自動生成pom.xml并自動添加了相應的maven依賴
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.4.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、配置mysql資料源,因為引入了mysql依賴,是以這一步不能省略,使用application.yml配置,需要将application.properties删除:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
4、環境搭建好了,可以正式開始寫代碼。
HelloController.java
@Controller //可以傳回視圖
//自動為springboot應用進行配置
//@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/myIndex")
public String index() {
System.out.println("hello.springboot的第一個controller");
return "index1";
}
}
-在resources/template路徑下建立 index1.html
Title 你好,這是我的第一個springboot頁面,thymeleaf
在所有java檔案的父路徑下建立springboot啟動類,DemoApplication.java
//辨別為springboot啟動類,必須是父路徑,其他包路徑必須是其子路徑
//@ComponentScan(basePackages = “com”)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5、浏覽器輸入http://localhost:8080/myIndex,成功通路!