Thymeleaf语法详解-字符串操作
-
- th:text的作用是什么?
在前端页面输出变量
- th:value的作用是什么?
可以将一个值放入到<input>标签的value属性中进行显示
- 什么是Thymeleaf的内置对象?
- 内置对象的语法是什么?
1.调用内置对象的语法,一定要在内置对象前加#号
2.大部分的内置对象都以s结尾
- Thymeleaf语法详解-日期转换操作
- 在Thymeleaf中使用哪个内置对象转换日期?
datas对象
- 常见的处理日期函数有哪些?
- <!--日期格式转换,如果不给的日期格式,则默认以浏览器语言为格式的标准-->
- <span th:text="${#dates.format(date)}"></span><br/>
- <span th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}">
- </span>
- <br/>
- <!--按照年月日取日期-->
- <span th:text="${#dates.year(date)}"></span><br/>
- <span th:text="${#dates.month(date)}"></span><br/>
- <span th:text="${#dates.day(date)}"></span><br/>
- Thymeleaf语法详解-条件判断
- th:if的作用是什么?
条件判断
- <span th:if="${sex} =='男'">
- 性别:男
- </span>
- <span th:if="${sex} =='女'">
- 性别:女
- </span>
- th:switch的作用是什么?
条件判断
- <div th:switch="${sex}">
- <span th:case="男">性别:男</span>
- <span th:case="女">性别:男</span>
- </div>
- Thymeleaf语法详解-迭代遍历
- th:each的作用是什么?
迭代遍历集合:
1.遍历List集合
- <table border="1" cellspacing="0">
- <tr>
- <th>id</th>
- <th>name</th>
- <th>age</th>
- <th>introduce</th>
- <th>索引(从0开始)</th>
- <th>序号(从1开始)</th>
- <th>偶数</th>
- <th>奇数</th>
- <th>总长度</th>
- <th>是否是第一个</th>
- <th>是否是最后一个</th>
- </tr>
- <tr th:each="u,var:${users}">
- <td th:text="${u.uid}"></td>
- <td th:text="${u.uname}"></td>
- <td th:text="${u.age}"></td>
- <td th:text="${u.describute}"></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.size}"></td>
- <td th:text="${var.first}"></td>
- <td th:text="${var.last}"></td>
- </tr>
- </table>
2.遍历Map集合:
- <!--(th:each)迭代map集合-->
- <table border="1" cellspacing="0">
- <tr>
- <th>key</th>
- <th>id</th>
- <th>name</th>
- <th>age</th>
- <th>introduce</th>
- </tr>
- <tr th:each="m : ${map}">
- <td th:text="${m.key}"></td>
- <td th:text="${m.value.uid}"></td>
- <td th:text="${m.value.uname}"></td>
- <td th:text="${m.value.age}"></td>
- <td th:text="${m.value.describute}"></td>
- </tr>
- </table>
- th:each中可以获取哪些状态变量?
- index:集合中元素的索引(从0开始)
- count:第几个元素(从1开始)
- odd:是否是集合中的第奇数个元素
- even:是否是集合中的第偶数个元素
- size:集合的总长度
- first:是否是集合中的第一个元素
- last:是否是集合中的最后一个元素
- Thymeleaf语法详解-获取作用域对象中的数据
- 在Thymeleaf中如何获取HttpServletRequest对象中的值?
request: <span th:text="${#httpServletRequest.getAttribute('message')}"></span><br/>
- 在Thymeleaf中如何获取HttpSession中的值?
session:<span th:text="${session.message}"></span><br/>
- 在Thymeleaf中如何获取ServletContext中的值?
Application:<span th:text="${application.message}"></span><br/>
- Thymeleaf语法详解-URL表达式
- URL表达式的语法是什么?
基本语法:@{}
- 在URL表达式中可以给定几种URL格式?
三种:
- <!--绝对路径 -->
- <a th:href="@{https://www.baidu.com}">绝对路径</a>
- <hr/>
- <!--相对路径1:相对于当前项目的根(相对于项目的上下文的路径)-->
- <a th:href="@{/index}">index.html</a>
- <hr/>
- <!--相对路径2:相对于服务器的根路径-->
- <a th:href="@{~/project/resourcename}">相对路径</a>
- 如何在URL表达式中传递参数?
- <!--URL中实现参数传递-->
- <a th:href="@{/tranferParam(id=1,name='张三')}">传参</a><br/>
- <a th:href="@{https://www.baidu.com/s(wd=${session.message})}">知乎</a>
- 如何在URL表达式中通过RESTful风格传递参数?
<a th:href="@{/tranfer/mde}">restful风格传参</a>
- Spring Boot整合MyBatis-创建项目
- Spring MVC+Spring Boot+MyBatis整合需要添加哪些坐标?
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <parent>
- <artifactId>springboot-study</artifactId>
- <groupId>com.bjsxt</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>springboot-mybatis</artifactId>
- <name>springboot-mybatis</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!--web启动器-->
- <dependency>
- <artifactId>spring-boot-starter-web</artifactId>
- <groupId>org.springframework.boot</groupId>
- </dependency>
- <!--数据库连接池-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- </dependency>
- <!--MyBatis启动器-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.1</version>
- </dependency>
- <!--mysql数据库驱动-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!--lombok依赖-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.8</version>
- </dependency>
- <!--添加Thymeleaf启动器依赖-->
- <dependency>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- <groupId>org.springframework.boot</groupId>
- </dependency>
- <!--SpringBoot热部署-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
- <build>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-jar-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
- <plugin>
- <artifactId>maven-site-plugin</artifactId>
- <version>3.7.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-project-info-reports-plugin</artifactId>
- <version>3.0.0</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </project>
-
- Spring Boot整合MyBatis时如何在全局配置文件中配置数据源?
-
- Spring Boot整合MyBatis时如何在全局配置文件中配置数据库连接池?
-
- Spring Boot整合MyBatis时如何在全局配置文件中配置MyBatis的包别名?
- SpringBoot整合MyBatis完成添加用户
- @MapperScan(" ")注解的作用是什么?
扫描项目中的@Mapper注解
- SpringBoot整合MyBatis完成用户查询
- 在项目中编写一个查询所有用户的案例。
前端:
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>用户管理主页</title>
- </head>
- <body style="text-align: center">
- <table border="1" cellspacing="0" style="width:500px;text-align: center">
- <tr>
- <th>id</th>
- <th>姓名</th>
- <th>年龄</th>
- <th>性别</th>
- <th>修改用户信息</th>
- <th>删除</th>
- </tr>
- <tr th:each="u : ${users}">
- <td th:text="${u.id}"></td>
- <td th:text="${u.name}"></td>
- <td th:text="${u.age}"></td>
- <td th:text="${u.sex}"></td>
- <td><a th:href="@{/updateUserFind(id=${u.id})}">修改该用户信息</a></td>
- <td><a th:href="@{/deleteUser(id=${u.id})}">删除该用户</a></td>
- </tr>
- </table>
- <p><a th:href="@{/adduser}">添加用户</a></p>
- </body>
- </html>
Controller:
- @Controller
- public class FuncationController {
- @Autowired
- private OperateService operate;
- @RequestMapping("/findAll")
- public String findAll(Model model){
- List<User> users = operate.findAll();
- if (users!=null){
- model.addAttribute("users", users);
- }
- return "index";
- }
- }
service层:
实现类:
Mapper:
- SpringBoot整合MyBatis完成用户修改-数据回显
- 在项目中编写一个预更新查询用户的案例。
前端:
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>修改用户</title>
- </head>
- <body>
- <form th:action="@{/update}" method="post">
- <p>id:<input type="text" name="id" th:field="${user.id}" readonly="readonly"></p>
- <p>姓名:<input type="text" name="name" th:field="${user.name}"></p>
- <p>年龄:<input type="text" name="age" th:field="${user.age}"></p>
- <p>性别:<select name="sex">
- <option value="null">请选择</option>
- <option value="男" >男</option>
- <option value="女" >女</option>
- </select>
- </p>
- <input type="submit" value="修改"><span th:text="${msg}"></span>
- </form>
- </body>
- </html>
Controller:
- @RequestMapping("/updateUserFind")
- public String updateUserFind(Integer id,Model model){
- User user = operate.findUserById(id);
- model.addAttribute("user", user);
- return "update";
- }
Service层:
实现类:
Mapper:
- SpringBoot整合MyBatis完成用户修改-更新用户
- 在项目中编写一个更新用户的案例。
Controller:
Service层:
实现类:
Mapper:
- SpringBoot整合MyBatis完成用户删除
- 在项目中编写一个删除用户的案例。
前端:
Controller:
Service层:
实现类:
Mapper:
分享/讲解/扩展思考
点名提问从第一节课到最后一节课分别学到了什么,直到同学们把所有的知识点都说出来并且保证无误。