1.ThyMeleaf表达式
注意,thymeleaf在3.0以前的版本html 的标记按照严禁的语法去编写。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
1.1变量输出与字符串操作
<!-- 在页面中输出值 -->
<span th:text="Hello"></span>
<hr />
<span th:text="${msg}"></span>
<hr />
<!-- 可以将一个值放入到 input 标签的 value 中
注意:只能写常量,动态的需要使用th:field
-->
<input type="text" th:value="test">
<hr />
<!-- Thymeleaf 内置对象
注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以 s 结尾 strings、numbers、dates -->
<!-- 判断字符串是否为空,如果为空返回 true,否则返回 false -->
<span th:text="${#strings.isEmpty(msg)}"></span>
<hr />
<!-- 判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false -->
<span th:text="${#strings.contains(msg,'o')}"></span>
<hr />
<!-- 判断当前字符串是否以子串开头,如果是返回 true,否则返回 false -->
<span th:text="${#strings.startsWith(msg,'o')}"></span>
<hr />
<!-- 判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false -->
<span th:text="${#strings.endsWith(msg,'o')}"></span>
<hr />
<!-- 返回字符串的长度 -->
<span th:text="${#strings.length(msg)}"></span>
<hr />
<!-- 查找子串的位置,并返回该子串的下标,如果没找到则返回-1 -->
<span th:text="${#strings.indexOf(msg,'k')}"></span>
<hr />
<!-- 截取子串,用户与 jdk String 类下 SubString 方法相同 -->
<span th:text="${#strings.substring(msg,0)}"></span>
<hr />
<span th:text="${#strings.substring(msg,0,1)}"></span>
<hr />
<!-- 字符串转大小写。 -->
<span th:text="${#strings.toUpperCase(msg)}"></span>
<hr />
<span th:text="${#strings.toLowerCase(msg)}"></span>
<hr />
1.2日期格式化处理
<!-- 格式化日期,默认的以浏览器默认语言为格式化标准 -->
<span th:text="${#dates.format(date)}"></span>
<hr />
<!-- 按照自定义的格式做日期转换 -->
<span th:text="${#dates.format(date,'yyyy/MM/dd')}"></span>
<hr />
<!-- year:取年
Month:取月
Day:取日 -->
<span th:text="${#dates.year(date)}"></span>
<hr />
<span th:text="${#dates.month(date)}"></span>
<hr />
<span th:text="${#dates.day(date)}"></span>
1.3条件判断
1.3.1th:if
<!-- 空值解决 th:text="${text!=null?text:''}" -->
<span th:if="${sex}=='男'" >
男
</span>
<span th:if="${sex}=='女'">
女
</span>
1.3.2th:switch
<span th:switch="${id}">
<span th:case="1">ID 为 1</span>
<span th:case="2">ID 为 2</span>
</span>
1.4迭代遍历
<!-- list集合遍历 -->
<tr th:each="u,var : ${list}">
<td th:text="${u.id}"></td>
<td th:text="${u.name}"></td>
<td th:text="${var.index}"></td>
<td th:text="${var.count}"></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>
<!-- map集合遍历 -->
<tr th:each="maps : ${map}">
<!--<td th:each="entry : ${maps}" th:text="${entry.value.id}"></td> -->
<!--<td th:each="entry : ${maps}" th:text="${entry.value.name}"></td> -->
<td th:text="${maps.value.id}"></td>
<td th:text="${maps.value.name}"></td>
</tr>
list状态变量属性
1.index:当前迭代器的索引 从 0 开始
2.count:当前迭代对象的计数 从 1 开始
3.size:被迭代对象的长度
4.even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始
5.first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false
6.last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false
1.5域对象操作
<!-- 作用域数据 -->
<span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<hr/>
<span th:text="${session.sess}"></span>
<hr/>
<span th:text="${application.app}"></span>
<hr/>
1.6URL 表达式
th:href
th:src
基本语法:@{}
<!-- 绝对路径 -->
<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
<!-- 相对于当前项目的根 -->
<a th:href="@{/show}">相对路径</a>
<!-- 相对于服务器路径的根 -->
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
<!-- 在 url 中实现参数传递 -->
<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
<!-- 在 url 中通过 restful 风格进行参数传递 -->
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}"> 相 对 路 径 - 传 参-restful</a>
1.7隔行变色
<tr th:each="u,var : ${list}" th:bgcolor="${var.odd}?'yellow':'green'">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
</tr>
1.8公共元素抽取
三种引入公共片段的th属性:
th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中
<footer th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</footer>
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
引入片段的时候传入参数
例:
bar.html(抽出的公共代码)
//方式一
<nav th:fragment="topbar"></nav>
//方式二
<nav id = "downbar">
dashboard.html
//模板名 片段名
<div th:replace="common/bar::topbar"></div>
//模板名 选择器
<div th:replace="common/bar::#downbar"></div>
选中的条目高亮显示
例:
bar.html
<a th:class="${activeurl=='top'?'nav-link active':'nav-link'}" ">
list.html
<div th:replace="common/bar::topbar(activeurl='top')"></div>
5.自定义html属性
<button th:attr="del_uri=@{/emp/}+${emp.id}" >删除</button>
6.发送put请求
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
7.发送delete请求
<form id="deleteEmpForm" method="post">
<input type="hidden" name="_method" value="delete"/>
</form>
2.springBoot整合mybatis
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- oracle
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency> -->
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
</project>
2.1application.properties
#数据源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
#指定使用哪个数据库连接源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#起别名
mybatis.type-aliases-package=com.springboot.pojo
2.2启动器
@SpringBootApplication
@MapperScan("com.springboot.mapper")//@MapperScan 用户扫描MyBatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
controller service mapper pojo和以前一样这里略