<!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
#数据库配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/practice spring.datasource.username=root spring.datasource.password=123456
注意:
@Reponsitory使用后,在启动类上加@MapperScan(“xx.xx.mapper”)注解。
@Mapper使用后,就是两者合体,会自动进行配置加载。
<!--thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
spring: thymeleaf: prefix: classpath:/templates/ check-template-location: true cache: false suffix: .html encoding: UTF-8 content-type: text/html mode: HTML5
prefix:指定模板所在的目录
check-tempate-location: 检查模板路径是否存在
cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
encoding&content-type:这个大家应该比较熟悉了,与Servlet中设置输出对应属性效果一致。
<!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta content="text/html;charset=UTF-8"/> </head> <body> <h6>Thymeleaf 模板引擎</h6> <table border="1" bgcolor="#f0ffff"> <thead> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>密码</th> </tr> </thead> <tbody th:each="yty : ${all1}"> <tr> <td th:text="${yty.id}"></td> <td th:text="${yty.name}"></td> <td th:text="${yty.age}"></td> <td th:text="${yty.pwd}"></td> </tr> </tbody> </table> </body> </html>
//1.查询所有 @RequestMapping("/list1") public String list1(Model model){ List<User> list1 = userService.selectAllSer(); System.out.println(list1); model.addAttribute("all1",list1); return "/index"; }
<!--freemarker模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
spring: freemarker: template-loader-path: classpath:/templates/ suffix: .ftl content-type: text/html charset: UTF-8 settings: number_format: '0.##'
<html> <title>文章列表</title> <body> <h2>Freemarker模板</h2> <table border="1"> <thead> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>密码</th> </tr> </thead> <#list all2 as yty> <tr> <td>${yty.id}</td> <td>${yty.name}</td> <td>${yty.age}</td> <td>${yty.pwd}</td> </tr> </#list> </table> </body> </html>
//1.查询所有--freemarker @RequestMapping("/list2") public String list2(Model model){ List<User> list2 = userService.selectAllSer(); System.out.println(list2); model.addAttribute("all2",list2); return "/index2"; }