文 | 平哥 日期 | 20200922
0 Spring Boot 簡介
什么是Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
Spring Boot 使你可以只 “僅僅運行” 來很容易的創(chuàng)建基于Spring的、生產(chǎn)級的獨立應用
Spring Boot 特征
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated 'starter' dependencies to simplify your build configuration
- Automatically configure Spring and 3rd party libraries whenever possible
- Provide production-ready features such as metrics, health checks, and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
- 可以創(chuàng)建獨立的Spring應用程序
- 直接嵌入了Tomcat、Jetty、Undertow等Web 容器,所以在使用SpringBoot做Web開發(fā)時不需要部署WAR文件
- 通過提供自己的啟動器(Starter)依賴,簡化項目構建配置
- 盡量的自動配置Spring和第三方庫
- 絕對沒有代碼生成,也不需要XML配置文件
1 Spring Boot 搭建SSM框架
1.1 Idea創(chuàng)建項目
利用IDEA創(chuàng)建Spring Boot項目:
新建項目,選擇Spring Initializr:

1.2 設置項目名稱、相關包信息、JDK信息
輸入項目信息:

1.3 選擇 SSM 相關依賴
選擇SSM相關依賴,idea會自動為pom文件中添加相關依賴

1.4 配置數(shù)據(jù)庫參數(shù)
在配置文件:application.yml中配置數(shù)據(jù)庫的驅(qū)動、url和賬號密碼:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tingyu?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
username: root
password: root
1.5 MyBatis 映射文件配置
2 Spring Boot 整合Druid
2.1 添加Druid依賴
<!--Druid依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2.2 配置druid參數(shù)
在配置文件application.yml中配置Druid相關參數(shù):
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 連接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
filters: stat,wall,slf4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
# 配置DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# 配置DruidStatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
# IP白名單(沒有配置或者為空,則允許所有訪問)
allow: 127.0.0.1,192.168.163.1
# IP黑名單 (存在共同時,deny優(yōu)先于allow)
deny: 192.168.1.188
# 禁用HTML頁面上的“Reset All”功能
reset-enable: false
# 登錄名
login-username: admin
# 登錄密碼
login-password: 123456
2.3 整合成功驗證:
啟動項目后,通過瀏覽器訪問:http://localhost:8080/druid/login.html,出現(xiàn)如下頁面:

輸入配置文件里配置的登錄名和登錄密碼,即可訪問Druid控制臺:

3 Spring Boot 整合PageHelper
在以前自己敲的分頁代碼中,除了進行查詢條件下的分頁數(shù)據(jù)查詢,還需要再進行相同條件下的數(shù)量查詢,較為麻煩,PageHelper就解決了這個問題,我們只需進行正常數(shù)據(jù)查詢,不需要考慮分頁,通過PageHelper即可自動分頁以及查詢所有數(shù)據(jù)
3.1 添加PageHelper依賴
<!--添加pageHelper依賴-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
3.2 具體使用
Mapper層正常編寫代碼:
Mapper文件:
public interface MarriedPersonMapper {
public List<MarriedPerson> selectMarriedPerson(@Param("pname") String pname, @Param("pphone") String pphone);
}
Mapper xml文件:
<mapper namespace="com.gcp.mapper.MarriedPersonMapper">
<select id="selectMarriedPerson" resultType="com.gcp.pojo.MarriedPerson">
select * from t_married_person
<where>
<if test="pname!=null and pname!=''">
and pname like concat('%', #{pname}, '%')
</if>
<if test="pphone!=null and pphone!=''">
and pphone = #{pphone}
</if>
</where>
</select>
</mapper>
在Service層代碼使用PageHelper:
public PageResult<MarriedPerson> selectMarriedPerson(Integer page, Integer rows, String pname, String pphone) {
Page<MarriedPerson> resultPage = PageHelper.startPage(page, rows);
List<MarriedPerson> marriedPeople = marriedPersonMapper.selectMarriedPerson(pname, pphone);
return new PageResult<>(resultPage.getResult(),resultPage.getTotal());
}
返回的對象PageResult,有兩個屬性,并提供getter、setter方法:
public class PageResult<T> {
private List<T> rows;
private Long total;
// 省略構造器和getter、setter方法
}
此時進行查詢,即可實現(xiàn)分頁操作。
4 Spring Boot 整合JSP
4.1 引入依賴、創(chuàng)建目錄
引入依賴:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
1、在src/main目錄下創(chuàng)建webapp目錄
2、將webapp目錄設置為資源目錄
4.2 在 yml配置文件中配置視圖解析器參數(shù)
spring
# SpringMVC 的內(nèi)部資源視圖解析器
mvc:
view:
prefix: /WEB-INF/pages/
suffix: .jsp
4.3 編寫Controller的單元方法請求轉(zhuǎn)發(fā)jsp資源
@RequestMapping("{urlPath}")
public String publicController(@PathVariable String urlPath){
return urlPath;
}