thymeleaf 多条件判断_《Thymeleaf学习+SpringBoot整合MyBatis实现数据库Curd》

Thymeleaf语法详解-字符串操作

    1. th:text的作用是什么?

在前端页面输出变量

  1. th:value的作用是什么?

可以将一个值放入到<input>标签的value属性中进行显示

  1. 什么是Thymeleaf的内置对象?
  2. 内置对象的语法是什么?

1.调用内置对象的语法,一定要在内置对象前加#号

2.大部分的内置对象都以s结尾

  1. Thymeleaf语法详解-日期转换操作
    1. 在Thymeleaf中使用哪个内置对象转换日期?

datas对象

  1. 常见的处理日期函数有哪些?
  1. <!--日期格式转换,如果不给的日期格式,则默认以浏览器语言为格式的标准-->
  2. <span th:text="${#dates.format(date)}"></span><br/>
  3. <span th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}">
  4. </span>
  5. <br/>
  6. <!--按照年月日取日期-->
  7. <span th:text="${#dates.year(date)}"></span><br/>
  8. <span th:text="${#dates.month(date)}"></span><br/>
  9. <span th:text="${#dates.day(date)}"></span><br/>
  1. Thymeleaf语法详解-条件判断
    1. th:if的作用是什么?

条件判断

  1. <span th:if="${sex} =='男'">
  2. 性别:男
  3. </span>
  4. <span th:if="${sex} =='女'">
  5. 性别:女
  6. </span>
  1. th:switch的作用是什么?

条件判断

  1. <div th:switch="${sex}">
  2. <span th:case="男">性别:男</span>
  3. <span th:case="女">性别:男</span>
  4. </div>
  1. Thymeleaf语法详解-迭代遍历
    1. th:each的作用是什么?

迭代遍历集合:

1.遍历List集合

  1. <table border="1" cellspacing="0">
  2. <tr>
  3. <th>id</th>
  4. <th>name</th>
  5. <th>age</th>
  6. <th>introduce</th>
  7. <th>索引(从0开始)</th>
  8. <th>序号(从1开始)</th>
  9. <th>偶数</th>
  10. <th>奇数</th>
  11. <th>总长度</th>
  12. <th>是否是第一个</th>
  13. <th>是否是最后一个</th>
  14. </tr>
  15. <tr th:each="u,var:${users}">
  16. <td th:text="${u.uid}"></td>
  17. <td th:text="${u.uname}"></td>
  18. <td th:text="${u.age}"></td>
  19. <td th:text="${u.describute}"></td>
  20. <td th:text="${var.index}"></td>
  21. <td th:text="${var.count}"></td>
  22. <td th:text="${var.even}"></td>
  23. <td th:text="${var.odd}"></td>
  24. <td th:text="${var.size}"></td>
  25. <td th:text="${var.first}"></td>
  26. <td th:text="${var.last}"></td>
  27. </tr>
  28. </table>

2.遍历Map集合:

  1. <!--(th:each)迭代map集合-->
  2. <table border="1" cellspacing="0">
  3. <tr>
  4. <th>key</th>
  5. <th>id</th>
  6. <th>name</th>
  7. <th>age</th>
  8. <th>introduce</th>
  9. </tr>
  10. <tr th:each="m : ${map}">
  11. <td th:text="${m.key}"></td>
  12. <td th:text="${m.value.uid}"></td>
  13. <td th:text="${m.value.uname}"></td>
  14. <td th:text="${m.value.age}"></td>
  15. <td th:text="${m.value.describute}"></td>
  16. </tr>
  17. </table>
  1. th:each中可以获取哪些状态变量?
  1. index:集合中元素的索引(从0开始)
  2. count:第几个元素(从1开始)
  3. odd:是否是集合中的第奇数个元素
  4. even:是否是集合中的第偶数个元素
  5. size:集合的总长度
  6. first:是否是集合中的第一个元素
  7. last:是否是集合中的最后一个元素

  1. Thymeleaf语法详解-获取作用域对象中的数据
    1. 在Thymeleaf中如何获取HttpServletRequest对象中的值?

request: <span th:text="${#httpServletRequest.getAttribute('message')}"></span><br/>

  1. 在Thymeleaf中如何获取HttpSession中的值?

session:<span th:text="${session.message}"></span><br/>

  1. 在Thymeleaf中如何获取ServletContext中的值?

Application:<span th:text="${application.message}"></span><br/>

  1. Thymeleaf语法详解-URL表达式
    1. URL表达式的语法是什么?

基本语法:@{}

  1. 在URL表达式中可以给定几种URL格式?

三种:

  1. <!--绝对路径 -->
  2. <a th:href="@{https://www.baidu.com}">绝对路径</a>
  3. <hr/>
  4. <!--相对路径1:相对于当前项目的根(相对于项目的上下文的路径)-->
  5. <a th:href="@{/index}">index.html</a>
  6. <hr/>
  7. <!--相对路径2:相对于服务器的根路径-->
  8. <a th:href="@{~/project/resourcename}">相对路径</a>
  1. 如何在URL表达式中传递参数?
  1. <!--URL中实现参数传递-->
  2. <a th:href="@{/tranferParam(id=1,name='张三')}">传参</a><br/>
  3. <a th:href="@{https://www.baidu.com/s(wd=${session.message})}">知乎</a>
  1. 如何在URL表达式中通过RESTful风格传递参数?

<a th:href="@{/tranfer/mde}">restful风格传参</a>

  1. Spring Boot整合MyBatis-创建项目
    1. Spring MVC+Spring Boot+MyBatis整合需要添加哪些坐标?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <artifactId>springboot-study</artifactId>
  6. <groupId>com.bjsxt</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>springboot-mybatis</artifactId>
  11. <name>springboot-mybatis</name>
  12. <!-- FIXME change it to the project's website -->
  13. <url>http://www.example.com</url>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. <maven.compiler.source>1.7</maven.compiler.source>
  17. <maven.compiler.target>1.7</maven.compiler.target>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.11</version>
  24. <scope>test</scope>
  25. </dependency>
  26. <!--web启动器-->
  27. <dependency>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. <groupId>org.springframework.boot</groupId>
  30. </dependency>
  31. <!--数据库连接池-->
  32. <dependency>
  33. <groupId>com.alibaba</groupId>
  34. <artifactId>druid</artifactId>
  35. </dependency>
  36. <!--MyBatis启动器-->
  37. <dependency>
  38. <groupId>org.mybatis.spring.boot</groupId>
  39. <artifactId>mybatis-spring-boot-starter</artifactId>
  40. <version>2.1.1</version>
  41. </dependency>
  42. <!--mysql数据库驱动-->
  43. <dependency>
  44. <groupId>mysql</groupId>
  45. <artifactId>mysql-connector-java</artifactId>
  46. </dependency>
  47. <!--lombok依赖-->
  48. <dependency>
  49. <groupId>org.projectlombok</groupId>
  50. <artifactId>lombok</artifactId>
  51. <version>1.18.8</version>
  52. </dependency>
  53. <!--添加Thymeleaf启动器依赖-->
  54. <dependency>
  55. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  56. <groupId>org.springframework.boot</groupId>
  57. </dependency>
  58. <!--SpringBoot热部署-->
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-devtools</artifactId>
  62. <optional>true</optional>
  63. </dependency>
  64. </dependencies>
  65. <build>
  66. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  67. <plugins>
  68. <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
  69. <plugin>
  70. <artifactId>maven-clean-plugin</artifactId>
  71. <version>3.1.0</version>
  72. </plugin>
  73. <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  74. <plugin>
  75. <artifactId>maven-resources-plugin</artifactId>
  76. <version>3.0.2</version>
  77. </plugin>
  78. <plugin>
  79. <artifactId>maven-compiler-plugin</artifactId>
  80. <version>3.8.0</version>
  81. </plugin>
  82. <plugin>
  83. <artifactId>maven-surefire-plugin</artifactId>
  84. <version>2.22.1</version>
  85. </plugin>
  86. <plugin>
  87. <artifactId>maven-jar-plugin</artifactId>
  88. <version>3.0.2</version>
  89. </plugin>
  90. <plugin>
  91. <artifactId>maven-install-plugin</artifactId>
  92. <version>2.5.2</version>
  93. </plugin>
  94. <plugin>
  95. <artifactId>maven-deploy-plugin</artifactId>
  96. <version>2.8.2</version>
  97. </plugin>
  98. <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
  99. <plugin>
  100. <artifactId>maven-site-plugin</artifactId>
  101. <version>3.7.1</version>
  102. </plugin>
  103. <plugin>
  104. <artifactId>maven-project-info-reports-plugin</artifactId>
  105. <version>3.0.0</version>
  106. </plugin>
  107. </plugins>
  108. </pluginManagement>
  109. </build>
  110. </project>
    1. Spring Boot整合MyBatis时如何在全局配置文件中配置数据源?

v2-f3fe4ca0a06461de2575b4f451910a6a_b.jpg
    1. Spring Boot整合MyBatis时如何在全局配置文件中配置数据库连接池?

v2-5e97d2a81a027b2713584643ebd526ba_b.jpg
    1. Spring Boot整合MyBatis时如何在全局配置文件中配置MyBatis的包别名?

v2-84abf49b296077fdab35e68fea6166de_b.jpg
  1. SpringBoot整合MyBatis完成添加用户
    1. @MapperScan(" ")注解的作用是什么?

扫描项目中的@Mapper注解

  1. SpringBoot整合MyBatis完成用户查询
    1. 在项目中编写一个查询所有用户的案例。

前端:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户管理主页</title>
  6. </head>
  7. <body style="text-align: center">
  8. <table border="1" cellspacing="0" style="width:500px;text-align: center">
  9. <tr>
  10. <th>id</th>
  11. <th>姓名</th>
  12. <th>年龄</th>
  13. <th>性别</th>
  14. <th>修改用户信息</th>
  15. <th>删除</th>
  16. </tr>
  17. <tr th:each="u : ${users}">
  18. <td th:text="${u.id}"></td>
  19. <td th:text="${u.name}"></td>
  20. <td th:text="${u.age}"></td>
  21. <td th:text="${u.sex}"></td>
  22. <td><a th:href="@{/updateUserFind(id=${u.id})}">修改该用户信息</a></td>
  23. <td><a th:href="@{/deleteUser(id=${u.id})}">删除该用户</a></td>
  24. </tr>
  25. </table>
  26. <p><a th:href="@{/adduser}">添加用户</a></p>
  27. </body>
  28. </html>

Controller:

  1. @Controller
  2. public class FuncationController {
  3. @Autowired
  4. private OperateService operate;
  5. @RequestMapping("/findAll")
  6. public String findAll(Model model){
  7. List<User> users = operate.findAll();
  8. if (users!=null){
  9. model.addAttribute("users", users);
  10. }
  11. return "index";
  12. }
  13. }

service层:

v2-e24b900193ef4a847bb033d671e7182e_b.jpg

实现类:

v2-7d7ca4e8215b22e001508025ae4df716_b.jpg

Mapper:

v2-93dc60bf2953a2d6439e92cded7fd236_b.jpg
  1. SpringBoot整合MyBatis完成用户修改-数据回显
    1. 在项目中编写一个预更新查询用户的案例。

前端:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>修改用户</title>
  6. </head>
  7. <body>
  8. <form th:action="@{/update}" method="post">
  9. <p>id:<input type="text" name="id" th:field="${user.id}" readonly="readonly"></p>
  10. <p>姓名:<input type="text" name="name" th:field="${user.name}"></p>
  11. <p>年龄:<input type="text" name="age" th:field="${user.age}"></p>
  12. <p>性别:<select name="sex">
  13. <option value="null">请选择</option>
  14. <option value="男" >男</option>
  15. <option value="女" >女</option>
  16. </select>
  17. </p>
  18. <input type="submit" value="修改"><span th:text="${msg}"></span>
  19. </form>
  20. </body>
  21. </html>

Controller:

  1. @RequestMapping("/updateUserFind")
  2. public String updateUserFind(Integer id,Model model){
  3. User user = operate.findUserById(id);
  4. model.addAttribute("user", user);
  5. return "update";
  6. }

Service层:

v2-4e4c93a7e3e35b551c9adc2575c0e555_b.jpg

实现类:

v2-27bd8e9a6612d660869de5616c0764e0_b.jpg

Mapper:

v2-affe5cb1bec038c318736d30f4944bee_b.jpg
  1. SpringBoot整合MyBatis完成用户修改-更新用户
    1. 在项目中编写一个更新用户的案例。

Controller:

v2-67e0a36aad08123648e659b74d185dd5_b.jpg

Service层:

v2-436f59e8fe28752bba8460514cefad09_b.jpg

实现类:

v2-96e0fad08a2d44a49c1e4d3952bf0c70_b.jpg

Mapper:

v2-2a7507227019e1681411cbac08ee3580_b.jpg
  1. SpringBoot整合MyBatis完成用户删除
    1. 在项目中编写一个删除用户的案例。

前端:

v2-a39989f2c08b300e3573374a6931a5ee_b.jpg

Controller:

v2-e9c6be65d810883138e3e66b79efc173_b.jpg

Service层:

v2-6b7bce12f887913d3ddb451b4bd7f8f9_b.jpg

实现类:

v2-998d59a59d894b6ae510fd9711e21f3f_b.jpg

Mapper:

v2-c0da05f49a182befdfb858f9cf0b3637_b.jpg

分享/讲解/扩展思考

点名提问从第一节课到最后一节课分别学到了什么,直到同学们把所有的知识点都说出来并且保证无误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值