標籤:str maven 否則 initial 方法 local bin 為我 點擊
使用idea+springboot+Mybatis搭建一個簡單的web項目。
首先建立一個項目;
在這裡選擇Maven項目也可以,但是IDEA為我們提供了一種更方便快捷的建立方法,即Spring Initializr。選擇後點擊Next;
把項目資訊寫好,Next;
按下面三張圖勾選設定;
最後Finish。
等待Maven自動載入完成後,最初的項目結構如。在Springboot屬性檔案application.properties中,把資料庫連接屬性加上,同時可以設定服務連接埠。
spring.datasource.url = jdbc:mysql://localhost:3306/testspring.datasource.username = rootspring.datasource.password = rootspring.datasource.driverClassName = com.mysql.jdbc.Driver#頁面熱載入spring.thymeleaf.cache = false#連接埠server.port=8888
resources目錄下,static檔案夾是放置各種靜態資源,如css,js,img等檔案的。templates檔案夾則是預設放置網頁的。當然也可以更改。
在static檔案夾下建立一個測試css,test.css。
body{ color: red;}
在templates檔案夾下建立一個html,要注意的是meta這個標籤的結束符軟體並沒有自動加上,需要手動加上,否則訪問網頁時會報錯。並引入test.css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<link rel="stylesheet" href="test.css" type="text/css" />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
接下來可以寫一個controller了
package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController { @RequestMapping("/index") public String index(){ return "hello"; }}
完成之後,通過方式1和方式2都可以啟動項目
接下來可以在瀏覽器中測試了
到此,一個簡單的項目搭建完成。
idea+springboot+Mybatis搭建web項目