使用idea+springboot+Mybatis搭建一個簡單的web項目。
首先新建一個項目;
在這里選擇Maven項目也可以,但是IDEA為我們提供了一種更方便快捷的創建方法,即Spring Initializr。選擇后點擊Next;
把項目信息寫好,Next;
按下面三張圖勾選設置;
最后Finish。
等待Maven自動加載完成后,最初的項目結構如下圖。在Springboot屬性文件application.properties中,把數據庫連接屬性加上,同時可以設置服務端口。
1
2
3
4
5
6
7
8
|
spring.datasource.url = jdbc:mysql:
//localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#頁面熱加載
spring.thymeleaf.cache =
false
#端口
server.port=
8888
|
resources目錄下,static文件夾是放置各種靜態資源,如css,js,img等文件的。templates文件夾則是默認放置網頁的。當然也可以更改。
在static文件夾下新建一個測試css,test.css。
1
2
3
|
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了
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package
com.example.demo;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
@Controller
public
class
IndexController {
@RequestMapping
(
"/index"
)
public
String index(){
return
"hello"
;
}
}
|
完成之后,通過方式1和方式2都可以啟動項目
接下來可以在瀏覽器中測試了
添加application.properties中的數據庫配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# 數據庫訪問配置
# 主數據源,默認的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql:
//localhost:3306/flag
spring.datasource.username = root
spring.datasource.password = root
# 下面為連接池的補充設置,應用到上面所有數據源中
# 初始化大小,最小,最大
spring.datasource.initialSize=
5
spring.datasource.minIdle=
5
spring.datasource.maxActive=
20
# 配置獲取連接等待超時的時間
spring.datasource.maxWait=
60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=
60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis=
300000
spring.datasource.validationQuery=SELECT
1
FROM DUAL
spring.datasource.testWhileIdle=
true
spring.datasource.testOnBorrow=
false
spring.datasource.testOnReturn=
false
# 打開PSCache,並且指定每個連接上PSCache的大小
spring.datasource.poolPreparedStatements=
true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=
20
# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,
'wall'
用於防火牆
spring.datasource.filters=stat,wall,log4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql=
true
;druid.stat.slowSqlMillis=
5000
#頁面熱加載
spring.thymeleaf.cache =
false
#mybatis配置
#mapper位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#領域類包別名
mybatis.type-aliases-
package
=com.legoo.flag.model
#mybatis配置文件
mybatis.config-location=classpath:config/mybatis-config.xml
#pagehelper分頁插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=
true
pagehelper.supportMethodsArguments=
true
pagehelper.params=count=countSql
pagehelper.row-bounds-with-count=
true
|
在resource文件夾下添加mapper,用來存放mybatis的xml.
config/mybatis-config.xml包下的文件暫時是空的,都在application.properties里面配置了.
然后就可以寫業務了
到此,一個簡單的項目搭建完成。