本文将详细介绍如何在Spring Boot项目中整合Thymeleaf模板引擎、JDBC Template和MyBatis,涵盖YAML配置、依赖版本匹配、项目结构设计及代码示例。
一、版本兼容性说明
-
Spring Boot版本与Java版本对应关系
-
Spring Boot 2.x:支持Java 8、11(推荐Java 11)。
-
Spring Boot 3.x:最低要求Java 17。
-
示例:若使用Spring Boot
2.7.18
,建议搭配Java 11。
-
-
依赖版本匹配
-
JDBC Template:内置于
spring-boot-starter-jdbc
,无需单独指定版本。 -
Thymeleaf:通过
spring-boot-starter-thymeleaf
引入,版本由Spring Boot管理。 -
MyBatis:需使用
mybatis-spring-boot-starter
,版本需与Spring Boot兼容。-
Spring Boot 2.7.x → MyBatis Starter
2.3.x
-
Spring Boot 3.x → MyBatis Starter
3.0.x
-
-
二、项目结构
标准的Maven项目结构如下:
三、YML配置详解
# application.yml
spring:datasource:url: jdbc:mysql://localhost:3306/stu_db?useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverthymeleaf:mode: HTMLprefix: classpath:/templates/suffix: .htmlcache: false # 开发时关闭缓存,生产环境改为true# MyBatis配置(仅整合MyBatis时需添加)
mybatis:mapper-locations: classpath:mapper/*.xml # Mapper XML文件路径type-aliases-package: com.ffyc.entity # 实体类包别名
四、依赖配置(Maven示例)
<?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"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>spbt02</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 引入springboot-starter-parent依赖--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version></parent><dependencies><!-- 引入spring-boot-starter-web依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 引入Thymeleaf依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- Springboot整合MySQL驱动类 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><!-- Springboot整合JDBC模板框架 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- Springboot整合MyBatis框架 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency></dependencies></project>
五、代码示例
UserService
@Service
public class UserService {@Autowiredprivate JdbcTemplate jdbcTemplate;public String insertUser(String username, Integer age) {String sql = "INSERT INTO user_tb(name, age) VALUES (?, ?)";int result = jdbcTemplate.update(sql, username, age);return result > 0 ? "success" : "fail";}
}
MyThymeleaf.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><title>My Thymeleaf Template</title>
</head>
<body><h1>Welcome to my Thymeleaf Template</h1><table>姓名:<span th:text="${user.name}"></span><br>性别:<span th:text="${user.age}"></span><br></table><ul th:each="user : ${list}"><li><span th:text="${user.name}"> </span></li><li><span th:text="${user.age}>"></span> </li></ul><!-- Thymeleaf 条件判断--><span th:if="${user.age >= 18}">已经成年了!</span><span th:if="${user.age < 18}">还未成年!</span></body>
</html>
创建Mapper接口
@Mapper
public interface UserMapper {@Select("SELECT * FROM user_tb WHERE id = #{id}")User getUserById(Integer id);
}