Thymeleaf
-
介绍:模板引擎,类似jsp。jsp对于前端开发人员来说不友好 ,jsp必须依赖服务器才能运行。thymeleaf是基于html5的模板引擎,thymeleaf可以完全替代jsp。是springboot官方指定的模板引擎。
-
thymeleaf配置步骤
-
导入依赖
<!-- thymeleaf --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> <!-- thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.9.RELEASE</version> </dependency>
thymeleaf不是普通的html
-
配置spring-mvc中的视图解析器
<!--thymeleaf模板解析器--> <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".html"></property> <property name="characterEncoding" value="utf-8"></property> <!--是否设置缓存--> <property name="cacheable" value="false"></property> <!--模板的类型--> <property name="templateMode" value="HTML5"></property> </bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"></property> </bean> <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="characterEncoding" value="utf-8"></property> <property name="templateEngine" ref="templateEngine"></property> </bean>
-
使用
@Controller public class ThymeleafController { /** * 这里我们配置了thymeleaf,所以可以直接向html页面传值了 * 使用model向页面传值 * @return */ @RequestMapping("/test_thy") public String test_thymeleaf(Model model){ User user = new User(); user.setUserId(1); user.setUserName("张大山"); User user2 = new User(); user2.setUserId(2); user2.setUserName("李大海"); List<User> userList = new ArrayList<>(); userList.add(user); userList.add(user2); //1. 传普通字符串 model.addAttribute("msg","你好,世界"); //2. 传对象 model.addAttribute("user",user); //3. 传集合 model.addAttribute("userList",userList); //4. 传递图片路径 model.addAttribute("imgPath","images/李荣浩.jpg"); return "hello"; } }
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- th:text:用于显示标签内部的文本。 div p a span --> <h1 th:text="${msg}">嘻嘻嘻嘻</h1> <input type="text" th:value="${msg}" value="嘻嘻"> <input type="text" th:value="${user.userName}+${user.userId}" value="嘻嘻"> <h1>下面是遍历列表数据</h1> <!--table>tr>td*2--> <table border="1"> <tr> <td>id</td> <td>名称</td> </tr> <!-- th:each="user:${userList}" ${userList}:要遍历的集合 user:集合中被遍历的每个元素 --> <tr th:each="user:${userList}"> <td th:text="${user.userId}"></td> <td th:text="${user.userName}">名称</td> </tr> </table> <!-- thymeleaf的页面中引入路径时, 官方推荐使用@{${变量}}的语法,因为都可以从项目的根路径开始取 --> <img th:src="@{${imgPath}}" > </body> </html>
逆向工程
-
mybatis的逆向工程:根据数据库的表和字段生成实体类、dao接口和对应mapper文件
-
步骤:
-
先把我的sql导入到你的数据库中去
-
创建maven-webapp项目
-
将pom.xml中的依赖和插件添加到项目中
-
创建java和resources文件夹,将config.properties和generatorConfig.xml复制到resources文件夹中
别忘了修改config.properties中的数据库名和数据库的密码
-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4vvdensH-1624438329432)(images\1.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7cFLJCeD-1624438329434)(images\2.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8hcsrKcY-1624438329435)(images\3.png)]
-
-