1 thymeleaf常用配置
spring:
thymeleaf:
cache: false
mode: LEGACYHTML5
# 配置了前缀
prefix: classpath:/templates/
# 配置了后缀
suffix: .html
web:
resources:
# 配置静态文件路径默认是classpath:/static/
static-locations: classpath:/static/
mvc:
# 静态文件匹配模式
static-path-pattern: /**
2 设置th的命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
设置完成之后,就会代码的提示了
3 取值操作
3.1 普通取值
<div th:text="${name}"></div>
3.2 富文本取值
<div th:utext="${content}"></div>
3.3 对象取值
3.3.1 通过对象.属性
<div th:text="${user.username}"></div>
3.3.2 通过th:object
<ul th:object="${user}">
<li th:text="*{username}">用户名</li>
<li th:text="*{password}"></li>
<li th:text="*{id}"></li>
</ul>
3.4 标签内取值
<div th:inline="text">
用户名:[[${name}]]
</div>
默认情况th:text 会覆盖掉标签内的值
3.5 js代码里取值
<script th:inline="javascript">
var name = [[${name}]];
console.log(name)
</script>
3.6 数据处理
<!-- 时间格式化 -->
<p th:text="${#dates.format(mydate,'yyyy-MM-dd')}" />
<p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}" />
日期格式化最常用,其余的用到的时候可以再去查阅资料
4 地址操作
4.1 springboot内置地址处理
如果需要引用静态资源,我们可以通过@{}处理地址
<!-- 引用 js-->
<script th:src="@{/js/index.js}" ></script>
<!-- 引用 css-->
<link rel="stylesheet" type="text/css" th:href="@{/css/index.css}">
<!-- 引用 图片-->
<img th:src="@{/images/mao.png}" alt="图片"/>
4.2 css中引入静态资源
可以通过相对地址引用
div {
background-color: lightblue;
background-image: url("../images/mao.png");
}
4.3 链接跳转
<a href="detail">详情</a>
<a href="/api/detail">绝对地址</a>
<a th:href="@{/detail}">thymeleaf写法</a>
<a th:href="@{/detail?name=张三丰}">thymeleaf传参</a>
<a th:href="@{/detail(name=张三丰,id=123)}">thymeleaf新写法</a>
<a th:href="@{/detail(name=${name},id=123)}">thymeleaf引用变量</a>
5 获取内置对象
可以通过#获取内置对象
<!-- 使用request中的方法 -->
<p th:text="${#httpServletRequest.getRemoteAddr()}" />
<!-- 获取request中的数据 -->
<p th:text="${#httpServletRequest.getAttribute('requestMessage')}" />
<!-- 获取session中的数据 -->
<p th:text="${#httpSession.getAttribute('sessionMessage')}" />
<p th:text="${#httpSession.getId()}" />
<!-- 获取项目根目录 -->
<p th:text="${#httpServletRequest.getServletContext().getRealPath('/')}" />
<hr />
<!-- 默认获取request中的数据 -->
<p th:text="'requestMessage = ' + ${requestMessage}" />
<!-- 获取session中的数据,需要使用session调用 -->
<p th:text="'sessionMessage = ' + ${session.sessionMessage}" />
<!-- 获取application中的数据,需要使用application调用 -->
<p th:text="'applicationMessage = ' + ${application.applicationMessage}" />
6 判断操作
6.1 th:if
满足条件的显示
6.2 th:unless
不满足条件的显示
6.3 隐式转换
布尔类型的false
数值型的0
字符型的0
字符串类型的false,off,no
null都是返回false,其余的返回true
7 循环渲染
th:each="【数组的每一项】,【遍历的信息】:【遍历的数组】"
<ul>
<li th:each="item,eachInfo:${list}" th:object="${item}">
<div th:text="*{username}"></div>
<div th:text="*{password}"></div>
<div th:text="${eachInfo}"></div>
</li>
</ul>
8 整合mybatis
8.1 引入依赖
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
8.2 配置mybatis
8.2.1 配置数据库连接
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot_study
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
8.2.2 配置mybatis
mybatis:
# 配置别名的包地址
type-aliases-package: com.tledu.springbootmybatis.domain
# 配置mapper xml的地址
mapper-locations: classpath:com/tledu/springbootmybatis/dao/*.xml
8.2.3 开启mapper扫描
在启动类中配置
@MapperScan(basePackages = "com.tledu.springbootmybatis.dao")
或者在mapper层加@mapper注解
@Mapper
8.2.4 补充
8.2.4.1 xml放置位置
mybatis:
# 配置别名的包地址
type-aliases-package: com.tledu.springbootmybatis.domain
# 配置mapper xml的地址
#和mapper放在一起的情况
#mapper-locations: classpath:com/tledu/springbootmybatis/dao/*.xml
#放在resource中mapper目录下的情况
mapper-locations: classpath:mapper/*.xml
8.2.4.2 引入mybatis其他配置文件
mybatis:
# 配置本地文件
config-location: classpath:mybatis/mybatis-config.xml
8.2.4.3 配置日志
logging:
level:
com.tledu: debug
spring boot内置日志,可以直接配置日志打印级别,让spring boot打印sql语句
8.2.4.4 连接池
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
配置连接池
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot_study
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
max-active: 20
min-idle: 10
useGlobalDataSourceStat: true
# 设置过滤的日志
filters: stat,wall,1og4j
web-stat-filter:
enabled: true
url-pattern: /**
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
allow: 127.0.0.1
filter:
wall:
config:
multi-statement-allow: true