SSM整合Thymeleaf

目前很多后端技术栈采用Spring+SpringMVC+MyBatis的项目,其模板引擎大多使用JSP。基于现在前后端分离趋势,我们使用Thymeleaf这个模板引擎和SSM框架配合。但是Thymeleaf不算是真正意义上的前后端分离。

创建项目

  1. 创建一个maven项目,在例子中,我们创建一个名为SSMAndThymeleaf的项目。

  2. 引入全部依赖

    <?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>com.wang</groupId>
        <artifactId>SSMAndThymeleaf</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <dependencies>
            <!--导入spring,下面几个为spring和springmvc需要的依赖-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <!--导入springmvc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>

            <!--导入mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
      <!--mysql数据库的依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.17</version>
            </dependency>
      <!-- 例子中使用的数据库连接池是C3P0,当然,你也可以选择Druid数据库连接池-->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>

            <!--导入Thymeleaf模板引擎 -->
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring5</artifactId>
                <version>3.0.9.RELEASE</version>
            </dependency>
            <!-- servlet api-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf</artifactId>
                <version>3.0.11.RELEASE</version>
            </dependency>

            <!--导入jackson注解-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.9.8</version>
            </dependency>
        </dependencies>
    </project>
  3. 为项目添加web框架支持。

Spring+SpringMVC+MyBatis的配置文件

  1. Spring的配置文件

    主要是扫描数据持久层和业务层,然后数据库的相关配置。

    <?xml version="1.0" encoding="utf-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">

        <!--扫描dao层-->
        <context:component-scan base-package="com.wang.dao"/>
        <context:component-scan base-package="com.wang.service"/>

        <!--加载properties文件,这里的properties文件是数据源配置需要的内容-->
        <context:property-placeholder location="classpath:properties/db.properties"/>

        <!--配置数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <!-- 初始连接池大小 -->
            <property name="initialPoolSize" value="10"/>
            <!-- 连接池中连接最小个数 -->
            <property name="minPoolSize" value="5"/>
            <property name="maxPoolSize" value="20"/>
        </bean>

        <!--配置SqlSession工厂对象,MyBatis配置SqlSessionFactory,Spring中将它封装成了一个Bean-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--MyBatis配置文件的位置-->
            <property name="configLocation" value="classpath:mybatis/mybatis-SqlMapConfig.xml"/>
            <!--MyBatis映射文件的位置-->
            <property name="mapperLocations" value="classpath*:mapper/*Dao.xml"/>
        </bean>

        <!--加载dao的接口对象,这一配置是为了让他能通过反射创建mapper对象-->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.wang.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    </beans>
  2. SpringMVC的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

        <!--扫描controller层注解-->
        <context:component-scan base-package="com.wang"/>
     
        <!--配置模板引擎-->
        <bean id="springResourceTemplateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
            <property name="prefix" value="/WEB-INF/templates/"/>
            <property name="suffix" value=".html"/>
            <!--解决页面的中文乱码-->
            <property name="characterEncoding" value="UTF-8"/>
            <property name="order" value="1"/>
            <property name="templateMode" value="HTML5"/>
            <property name="cacheable" value="false"/>
        </bean>
        <bean id="springTemplateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver" ref="springResourceTemplateResolver"/>
        </bean>
        
     <!-- 配置thymeleaf视图解析器 -->
        <bean id="thymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="templateEngine" ref="springTemplateEngine"/>
            <property name="characterEncoding" value="UTF-8"/>
        </bean>

     <!--开启注解驱动-->
        <mvc:annotation-driven />
    </beans>
  3. MyBatis的配置文件

    其实MyBatis的配置在Spring中都已经配置完,即使我们使用事务,也是使用Spring的事务处理,所以这里只需要配置一点点东西。

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!--扫描pojo对象,起个别名-->
        <typeAliases>
            <package name="com.wang.model"/>
        </typeAliases>
    </configuration>

数据库内容


数据库内容

dao层+service层+controller层

  1. 创建一个实体类(Country)

    package com.wang.model;

    /**
     * @author wya
     * @version 1.0
     * @school hhu
     * @date 2022/3/21 19:10
     */
    public class Country {
        private Integer id;
        
        private String countryName;
        
        private String countryCode;
        
        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCountryName() {
            return countryName;
        }

        public void setCountryName(String countryName) {
            this.countryName = countryName;
        }

        public String getCountryCode() {
            return countryCode;
        }

        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }

    }
  2. 创建dao接口

    import com.wang.model.Country;
    import org.apache.ibatis.annotations.Param;
    import org.springframework.stereotype.Repository;

    /**
     * @author wya
     * @version 1.0
     * @school hhu
     * @date 2022/3/21 19:11
     */
    @Repository
    public interface CountryDao {
        
        Country selectACountry(@Param("countryId")int id);

    }
  3. 创建service接口

    package com.wang.service;

    import com.wang.model.Country;

    /**
     * @author wya
     * @version 1.0
     * @school hhu
     * @date 2022/3/21 19:11
     */
    public interface CountryService {

        Country getCountryById(int id);

    }
  4. 实现service接口

    package com.wang.service.impl;

    import com.wang.dao.CountryDao;
    import com.wang.model.Country;
    import com.wang.service.CountryService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * @author wya
     * @version 1.0
     * @school hhu
     * @date 2022/3/21 19:11
     */
    @Service
    public class CountryServiceImpl implements CountryService {

        @Autowired
        CountryDao countryDao;

        @Override
        public Country getCountryById(int id) {
            /**
             * 这里是业务层的操作,我们这里省略
             */
            return countryDao.selectACountry(id);
        }
    }

  5. 创建controller

    package com.wang.controller;

    import com.wang.model.Country;
    import com.wang.service.CountryService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;

    /**
     * @author wya
     * @version 1.0
     * @school hhu
     * @date 2022/3/21 19:09
     */
    @Controller
    public class CountryController {

        @Autowired
        CountryService countryService;

        @RequestMapping("hello")
        public String test(Model model) {
            Country country = countryService.getCountryById(1);
            model.addAttribute("country",country);
            return "hello";
        }

    }

映射文件

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--定义为当前的命名空间-->
<mapper namespace="com.wang.dao.CountryDao">
    <select id="selectACountry" resultType="com.wang.model.Country">
        select * from country where id = #{countryId};
    </select>

</mapper>

前端简单页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center><h1>显示成功!!!</h1>
国家名字: <p th:text="${country.countryName}"></p>
国家ID:  <p th:text="${country.countryCode}"></p>
</body>
</html>

配置tomcat,运行显示

总体项目架构

补充

后续需要其他依赖的再加入即可,像文件的上传下载依赖等。

有问题请联系我。

SSM集成Thymeleaf的更多相关文章

  1. ssm集成redis

    身在一个传统的IT公司,接触的新技术比较少,打算年后跳槽,所以抽空学了一下redis. 简单的redis测试,咱们这边就不讲了,现在主要讲讲ssm集成redis的过程,因为现在项目用的就是ssm的框架 ...

  2. Springboot 集成 Thymeleaf 及常见错误

    Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...

  3. SpringBoot系列之集成Thymeleaf用法手册

    目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...

  4. MyBatis进阶讲解+ssm集成

    1.sql映射器Mapper MyBatis基于动态代理机制,让我们无需再编写Dao的实现. 传统Dao接口,现在名称统一以Mapper结尾,还有我们映射器配置文件要和映射器在同一个包. 1.1使用映 ...

  5. SSM集成

    SSM集成   Spring和各个框架的整合   Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成   Spring整合方案   SSH Ssh是早期的一种整 ...

  6. 9、Spring Boot 2.x 集成 Thymeleaf

    1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...

  7. java:Mybatis框架3(二级缓存,延时和积极加载,SSI(Ibatis)集成,SSM集成)

    1.二级缓存: 需要导入二级缓存jar包: mybatis03: ehcache.xml: <ehcache xmlns:xsi="http://www.w3.org/2001/XML ...

  8. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的.. ...

  9. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

随机推荐

  1. ABCD四个顺序执行方法,拓展性延申

    今天在群里,有人问 有几个void返回值的方法,但是我想让这几个方法有执行顺序,要怎么处理,ABCD 四个方法,依次执行,但是这几个方法都是无返回值的 这个问题其实很简单,如果方法是同步方法,直接四个 ...

  2. Solution -「NOI.AC 省选膜你赛」寄蒜几盒

    题目 题意简述   给定一个含有 \(n\) 个顶点的凸多边形( \(n\) 是偶数),对于每一对相对的边(即中间有 \(\frac{n}2-1\) 条其它边),延长它们以将平面分割为多块,并把包含原 ...

  3. Note -「圆方树」学习笔记

    目录 圆方树的定义 圆方树的构造 实现 细节 圆方树的运用 「BZOJ 3331」压力 「洛谷 P4320」道路相遇 「APIO 2018」「洛谷 P4630」铁人两项 「CF 487E」Touris ...

  4. Solution -「HDU #6566」The Hanged Man

    \(\mathcal{Description}\)   Link.   给定一棵含 \(n\) 个点的树,每个结点有两个权值 \(a\) 和 \(b\).对于 \(k\in[1,m]\),分别求 \[ ...

  5. 通过Dapr实现一个简单的基于.net的微服务电商系统(十九)——分布式事务之Saga模式

    在之前的系列文章中聊过分布式事务的一种实现方案,即通过在集群中暴露actor服务来实现分布式事务的本地原子化.但是actor服务本身有其特殊性,场景上并不通用.所以今天来讲讲分布式事务实现方案之sag ...

  6. Numpy的各种下标操作

    技术背景 本文所使用的Numpy版本为:Version: 1.20.3.基于Python和C++开发的Numpy一般被认为是Python中最好的Matlab替代品,其中最常见的就是各种Numpy矩阵类 ...

  7. CentOS7防火墙firewall

    一.Firewall 1. 从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2. firewall的配置文件是以xml的格式,存储在 /usr/ ...

  8. 完爆Excel!一个令人惊艳的数据展示工具,让你做图更轻松高效

    数据展示应该是最常见的需求,我们经常利用数据做总结.用数据做分享.但是我们该如何更好地展示给我们需要展示的人,如何才能让我们的数据表达更加动人,这个值得让人思索. 说到数据表达,常用的数据展示方式无非 ...

  9. vs2022 如何让.net库文件参与程序调试【可以.net库文件的源代码中设置断点,单步跟踪】

    由于.net core 是开源的.所以可以让.net库文件参与程序调试.具体vs2022配置如下 1.设置VS2022 加载程序数据文件(.pdb俗称符号文件) 1)选择工具>选项>调试& ...

  10. 图解CPU缓存一致性问题

    产生背景 CPU的读取速度比内存的快,一个快一个慢,就会有矛盾,就会有人想要解决这个矛盾,所以就提出多级缓存来解决,如下图所示. L1级缓存:分为数据域和程序域. L2级缓存:二级缓存.  L3级缓存 ...