Springboot与thymeleaf整合入门案例

mac2026-05-03  12

Thymeleaf是现代化服务器端的Java模板引擎,不同与JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性。详细资料可以浏览官网。本文主要介绍Thymeleaf模板的使用说明。 本文主要介绍从数据库把数据在使用thymeleaf模板引擎技术把数据显示在前端页面

导入依赖jar <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>

配置文件application.properties

#服务器端口 server.port=80 #配置数据源 spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root #配置mybatis自定义加载文件的配置 mybatis.mapper-locations=classpath:/mapper/*/*.xml #支持数据库字段下划线转驼峰命名法 mybatis.configuration.map-underscore-to-camel-case=true #默认状态超时连接时间/秒 mybatis.configuration.default-statement-timeout=30 #log logging.level.com.tedu=debug #spring web spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false #没有配置spring.thymeleaf.prefix文件默认在/templates/ spring.thymeleaf.prefix=classpath:/templates/pages/ 创建dao层 @Mapper public interface GoodsDao { //查找所有信息 @Select("select * from tb_goods") List<Goods> findAll(); }

3.pojo类

@Data public class Goods { private Integer id; private String name; private String remark; private Date createTime; }

4.创建GoodsService接口

public interface GoodsService { //查找所有信息 List<Goods> findAll(); }

5.实现接口GoodsServiceImp

@Service public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsDao goodsDao; @Override public List<Goods> findAll() { List<Goods> goods = goodsDao.findAll(); return goods; } }

6.Cotroller

@Controller @RequestMapping("/goods/") public class GoodsController { @Autowired private GoodsService goodsService; @RequestMapping("doGoodsUI") public String doGoodsPage(Model model){ List<Goods> goods = goodsService.findAll(); System.out.println(goods); **//使用Model向前端转发数据 model.addAttribute("goods",goods);** return "goods"; } }

7.配置前端页面

<!DOCTYPE html> <!--*引用hymeleaf引擎模板标签,本人用的用idea2018.3.5版本,使用时H5的页面,如果不配置xmlns:th="http://www.thymeleaf.org"会没有标签提示*--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>goods-ajax</h1> <table> <thead> <tr> <td>id</td> <td>name</td> <td>remark</td> <td>createTime</td> </tr> </thead> <tbody> //使用标签遍历取出数据 <tr th:each="goods:${goods}"> <td th:text="${goods.id}"></td> <td th:text="${goods.name}"></td> <td th:text="${goods.remark}"></td> <td th:text="${goods.createTime}"></td> </tr> </tbody> </table> </body> </html>

启动springboot项目即可显示在数据库里面查找到的数据显示在前端的页面上 springboot项目文件架构 注意要点:

确保所有的源文件在sprintboot主程序入口配置文件包同包或者子包确保数据库能连接上确保前端的引用了thyemleaf标签

最新回复(0)