一、整合mybatis
1、导入坐标
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.7.RELEASE</version>
</parent>
<dependencies>
<!--mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot的mybaits启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--实体类工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
-
spring-boot-starter-jdbc:提供了默认的 HikariCP 数据库连接池,(也是传说中最快的数据库连接池)。spring-boot-starter-jdbc主要提供了三个功能,第一个就是对数据源的装配,第二个就是提供一个JdbcTemplate简化使用,第三个就是事务
-
mybatis-spring-boot-starter:mybaits与springboot的整合坐标,但是该坐标不是springboot提供的,由mybaits自己开发的
2、创建启动类
/**
* 启动类
*/
3、创建springboot配置文件
#tomcat
#server.port=80
#server.servlet.context-path=/springboot-demo
#日志
logging.level.cn.woniu.dao=DEBUG
logging.level.root=INFO
logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(${LOG_LEVEL_PATTERN:%5p}) %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
4、配置数据源
数据源只需要在application.properties中将数据库的信息配置进来即可,spring-boot-starter-jdbc会自动将数据库信息保存到DataSource中,不需要额外配置
-
配置HikariCP
# 连接四⼤参数
spring.datasource.url=jdbc:mysql://localhost:3306/woniu_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot⾃动推断
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10 -
配置druid
如果你更喜欢Druid连接池,也可以使⽤Druid官⽅提供的启动器,那麼就不需要spring-boot-starter-jdbc启动器了。
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>配置数据库信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/woniu_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-wait=60000
#初始化连接数
spring.datasource.initial-size=1 #最⼩空闲连接
spring.datasource.min-idle=1 #最⼤活动连接
spring.datasource.max-active=20
#获取连接时测试是否可⽤
spring.datasource.test-on-borrow=true
#监控⻚⾯启动
spring.datasource.stat-view-servlet.allow=true
5、springboot整合mybaits
-
所需坐标
<!--springboot的mybaits启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency> -
在springboot配置中添加mybaits配置【不是必须的】
#加载mybatis全局配置文件,注意: 放到resources目录下
mybatis.config-location=classpath:mybatis-config.xml
#加载mybatis的映射文件 如果和dao接口在同一包下,不需要配此项
mybatis.mapper-locations=cn/woniu/dao/*.xml
#mybatis的po实体类的别名 如果dao接口是注解形式,那么就不要定义别名
mybatis.type-aliases-package=cn.woniu.domain -
在启动类上扫描dao接口
/**
* 启动类
*/
6、整合事务
当在springboot项目中导入了spring-boot-starter-jdbc或spring-boot-starter-web,就会自动添加事务相关的包
-
在启动类中开启事务支持
/**
* 启动类
*/springboot中默认集成了cglib
-
在service上添加事务注解
二、整合springmvc
1、配置tomcat相关内容
# 映射端⼝为80
server.port=80
#修改显示在url中的项目名
server.servlet.context-path=/springboog-ssm
2、配置自定义静态文件目录
spring.resources.static-locations=classpath:/META•INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/my/
三、添加mybatis-generator
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version> <!-- 不要低于 1.3.7 版本 -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version> <!-- 不要低于 1.3.7 版本 -->
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose> <!-- 允许移动生成的文件 -->
<overwrite>true</overwrite> <!-- 是否覆盖 -->
<!--配置文件的路径 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
四、SpringBoot整合thymeleaf
网页静态化:将动态网页转为静态网页
伪静态:还是一个动态网页,只是看上去是一个html
真静态:根据动态网页的数据,生成一个html页面
1、添加启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、在springboot配置添加thymeleaf配置
#关闭thymeleaf缓存
spring.thymeleaf.cache=false
#spring.thymeleaf.prefix = classpath:/templates/ #在构建URL时预先查看名称的前缀。
#spring.thymeleaf.suffix = .html #构建URL时附加到查看名称的后缀。
如果不关闭thymeleaf缓存当html页面发生改变量必须重启项目才会生效。
3、创建thymeleaf模板
在springboot中,thymeleaf的模板文件(html),有一个固定的目录存放:resources/templates
thymeleaf模板页面上需要添加如下内容:
<html xmlns:th="http://www.thymeleaf.org">
五、thymeleaf常用属性
1、变量输出
-
th:text
该属性用来将文本显示到标签中间
语法:
<p>欢迎你:<span th:text="${uname}"></span> </p> //显示保存在作用域中的值
<p>欢迎你:<span th:text="小白"></span> </p> //显示值 -
th:value
为表单的value属性赋值
语法:
<input type="text" th:value="${user.id}"> -
判断字符串是否为空
判断字符串是否为空,如果为空返回true,否则返回false
语法:
${#strings.isEmpty(key)}
示例:
<p th:text="${#strings.isEmpty(user.account)}"></p>
判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false
语法:
${#strings.contains(msg,'T')} -
th:attr
将内容拼接到href路径的后面
<a class="deleteUser" th:attr="userid=${user.id},username=${user.userName}"
href="#"><img src="/img/schu.png" alt="删除" title="删除"/>
</a>
2、日期格式化处理
格式化日期,默认的以浏览器默认语言为格式化标准
语法:
${#dates.format(key)}
按照自定义的格式做日期转换
语法:
${#dates.format(key,'yyy/MM/dd')}
示例:
<p>格式化日期:<span th:text="${#dates.format(tim,'yyyy-MM-dd')}"></span> </p>
3、判断
-
if
方式一:
<span th:if="${sex} == ' 男 '">
性别:男
</span>
<span th:if="${sex} == ' 女 '">
性别:女
</span>
方式二:
<td><span th:if="${user.gender eq 1}">男</span>
<span th:if="${user.gender eq 2}">女</span>
</td>
4、循环
用法一:
<table border="1">
</tr>
<tr th:each="u : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
</tr>
</table>
用法二:
<span>用户角色:
<select name="queryUserRole">
<option value="0">--请选择--</option>
<option
th:each="role :${roleList}"
th:value="${role.id}" th:text="${role.roleName}"
th:selected="${queryUserRole eq role.id}">
</option>
</select>
</span>
th:each的参数:
<table border="1">
<tr th:each="u,var : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${var.index}"></td>
<td th:text="${var.count}"></td>
<td th:text="${var.size}"></td>
<td th:text="${var.even}"></td>
<td th:text="${var.odd}"></td>
<td th:text="${var.first}"></td>
<td th:text="${var.last}"></td>
</tr>
</table>
状态变量属性1,index:当前迭代器的索引 从 0 开始2,count:当前迭代对象的计数 从 1 开始3,size:被迭代对象的长度4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false
5、url表达式
是为标签设置href或src路径的
-
代替href
<a class="modifyUser" th:href="@{'/modifyUser.do?id='+${user.id}}"></a>
<a class="modifyUser" th:href="@{'/modifyUser.do/'+${user.id}}"></a> -
代替src
<img th:src="@{'/images/'+${user.img}}" alt="修改" title="修改"/>
6、button的click
<button id = "ddd" th:onclick="show([[${user.id}]])">查看</button>
六、整合pageHelper
添加启动器即可,无需配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
示例:
<!--分页-->
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a th:href="@{'javascript:sup('+${pageInfo.pageNum}+')'}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</li>
<li th:each="p,v:${pageInfo.navigatepageNums}">
<a th:href="@{'javascript:goPage('+${p}+')'}" th:text="${p}">1</a>
</li>
<li>
<a th:href="@{'javascript:sub('+${pageInfo.pageNum}+','+${pageInfo.pages}+')'}" aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</li>
</ul>
</nav>
分页js
<script type="text/javascript">
//上一页
function sup(pageNo){
if(pageNo>1){
pageNo--;
window.location.href="index?pageNo="+pageNo;
}
}
//下一页
function sub(pageNo,pages){
if(pageNo<pages){
pageNo++;
window.location.href="index?pageNo="+pageNo;
}
}
//页码跳转
function goPage(pageNo){
window.location.href="index?pageNo="+pageNo;
}
</script>
七、springboot热部署
springboot热部署可以让程序在修改代码后不重新发布,直接更新数据
1、使用 SpringLoader 进行项目热部署
<build>
<plugins>
<!-- springloader 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
在主启动类中,右键以debug模式启动项目,访问/show路径,项目跳转到index页面,修改/show方法返回的值为“test”并且按 Ctr+shift+F9重新编译文件,刷新页面,发现没有重新启动项目也可以访问到修改后的内容
2、DevTools 工具
-
坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
八、SpringBoot配置拦截器
-
创建拦截器
/**
* 登录拦截器
*/
public class LoginInterceptor implements HandlerInterceptor {
/**
* 在进入controller前先执行
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
-
注册拦截器
/**
* springmvc的配置类
*/<font color=red>配置拦截器后会造成页面静态资源无法加载的问题</font>
-
修改配置文件
#配置mvc静态资源目录 不配置默认为"/**"
spring.mvc.static-path-pattern=/static/** -
修改页面静态资源引用
要加上"/satic/"
<link href='/static/bootstrap/css/bootstrap.css' rel="stylesheet">
<script type="text/javascript" src='/static/js/jquery-3.5.1.js'></script>
<script type="text/javascript" src='/static/bootstrap/js/bootstrap.js'></script> -
修改拦截器注册方法
/**
* springmvc配置类
*/
-
九、类型转换器
将客户端发送过来的数据(字符串类型),在服务端自动转换为想要的类型:如字符串转日期
-
编写类型转换器
/**
* 数据类型转换器
*/
类型转换器与消息转换器的区别:
-
类型转换器:将客户端发送的数据在服务器端进行转换
-
消息转换器:在服务器端将消息转换为固定的格式发送到客户端
十、Springboot上传与下载
1、上传
-
导坐标
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> -
配置文件解析器
spring.servlet.multipart.max-file-size=30MB #单个文件大小
spring.servlet.multipart.max-request-size=60MB #单次上传文件总大小 -
实现上传
/**
* 文件上传
* @return
*/
2、下载
十一、SpringBoot异常处理
异常处理是让程序发生异常时不在客户端显示异常代码,而显示一个友好的提示。
1、默认异常处理
在springboot中有一具默认的异常处理器 BasicErrorController,只要程序发生异常就会默认跳转到一个名为error的页面。只需要在项目中创建一个error.html页面发生异常时就会自动跳到该页面
2、@ExceptionHandle 注解方式
@ExceptionHandle 注解可以捕获程序发生的异常类型,可以根据不同的异常进行不同的提示
/**
* 异常处理类
*/
3、SimpleMappingExceptionResolver方式(异常解析器)
自定义一个配置类,创建一个全局异常SimpleMappingExceptionResolver解析器的bean对象到spring容器中,有spring来管理
/**
* 异常处理类
*/
十二、SpringBoot定时任务
-
导坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> -
编写定时任务
/**
* 定时任务
*/ -
在启动中开启定时任务
/**
* 启动类
*/
十三、SpringBoot集成junit
-
导坐标
<!--测试启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency> -
创建测试类
import org.junit.jupiter.api.Test; //注意test的命名空间,不能导错了
<font color=red>提示:@SpringBootTest要求测试类必须和启动类在同一包下(可以在子包中)</font>
十四、SpringBoot项目打包
springboot项目可以打为:jar包、war包
1、打jar包
-
修改pom将找包方式改为jar
<packaging>jar</packaging> -
添加springboot编译插件
<!--springboot编译插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<!--指定启动类-->
<mainClass>cn.woniu.ApplicationApp</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin> -
执行package命令
打包之后,在target⽬录下⽣成jar⽂件。进⼊该⽬录,然后在命令⾏窗⼝执⾏
java -jar 包名
2、打war包
<font color=red>提示:打war包不需要spring-boot-maven-plugin插件</font>
-
修改打包方式为war
<packaging>war</packaging> -
配置坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除springboot内置tomcat-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--tomcat启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> -
修改启动类
在启动类继承SpringBootServletInitializer,并重写configure⽅法
/**
* 启动类
*/ -
执行package命令

浙公网安备 33010602011771号