当前位置: 首页 > news >正文

黄冈网站建设哪家专业苏州建设公司网站

黄冈网站建设哪家专业,苏州建设公司网站,做平台交易网站怎么收款,手机网站建设一般多少钱本文将详细介绍如何在Spring Boot项目中整合Thymeleaf模板引擎、JDBC Template和MyBatis,涵盖YAML配置、依赖版本匹配、项目结构设计及代码示例。 一、版本兼容性说明 Spring Boot版本与Java版本对应关系 Spring Boot 2.x:支持Java 8、11(推…

        本文将详细介绍如何在Spring Boot项目中整合Thymeleaf模板引擎、JDBC Template和MyBatis,涵盖YAML配置、依赖版本匹配、项目结构设计及代码示例。

一、版本兼容性说明
  1. 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。

  2. 依赖版本匹配

    • 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);
}

http://www.yayakq.cn/news/273219/

相关文章:

  • 建设家居网站辽宁建设建设工程信息网
  • 进下加强新闻宣传网站建设wordpress全屏背景
  • 用vue做pc端网站好吗网站建设评价指标
  • 坤和建设 网站交换链接营销的典型案例
  • 网站空间维护网站开发方倍工作室
  • 衡阳做网站建设的公司大学网页设计与制作教材
  • 昆明网站开发兼职群沈阳网站建设024w
  • 网站到期查询备案建筑设计学校排名
  • 电子商务网官方网站海外服务器 vps
  • 专门做电子书的网站深圳市seo上词贵不贵
  • 简单的个人网站制作流程ps做网站设计稿
  • 官方网站免费建设东莞app定制开发公司
  • 模板网站没有源代码什么属于网页制作工具
  • 旅游类网站建设的结论个人网页设计代码模板
  • 浦江县做网站天津做艺术品的网站
  • 网站地图建设有什么用外汇交易网站建设
  • 做电影网站要多少钱中国农技推广网
  • 陇南做网站建立网站原理
  • 代账公司网站模板中国铁塔公司招聘网站
  • 租用服务器网站app 网站
  • 东莞住房和城乡建设厅网站电商网站建设基础教案
  • 西安网站开发制作网站访问量咋做
  • 网站源码建站视频rss 网站插件
  • 做网站沈阳做实验的网站
  • 莆田网站建设方案优化定制型网站制作哪家好
  • 北京网站建设降龙网络代理网页在线
  • 1688代加工官方网站免费logo图标在线制作
  • 网站做推广赚钱项目如何将图片生成网址
  • 百度经验网站建设网页设计师资格证查询官网
  • 济南天桥区网站建设公司wordpress滑块不显示