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. Java基础复习(五)

    1. 接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继承具体类(concrete class)? 抽象类中是否可以有静态的main方法? 接口可以继承接口.抽象类可 ...

  2. 有手就行4——jenkins项目构建类型(自由风格,maven风格)

    有手就行4--构建Maven项目 Jenkins项目构建类型(1)-Jenkins构建的项目类型介绍 Jenkins项目构建类型(2)-自由风格项目构建 Jenkins项目构建类型(3)-Maven项 ...

  3. VUE项目部署到线上生产环境,Loading chunk xxx failed

    项目部署到生产环境,路由点击无效,报错 Loading chunk chunk-xxxxx failed.(missing xxxx) 加载失败,错误的路径. 话不多说,直接贴代码: vue.conf ...

  4. java_JDBC,连接数据库方式,RestSet结果集,Statement,PreparedStatement,事务,批处理,数据库连接池(c3p0和Druid)、Apache-DBUtils、

    一.JDBC的概述 1.JDBC为访问不同的数据薛是供了统一的接口,为使用者屏蔽了细节问题.2. Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作 ...

  5. vue 定义全局函数和变量

    背景 最近我在整一个网站,介绍一些有意思的网站和实用工具的网站并且把他们收集起来,网站刚建有些不成熟希望给点意见 我用的是前端框架的vue, 但是我没有打包,直接甩到服务器上了, 不想扯了, 步骤 1 ...

  6. 《PHP程序员面试笔试宝典》——如何回答技术性的问题?

    如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 程序员面试中,面试官会经常询问一些技术性的问题,有的问题可能比较简单,都是历年的面试.笔试真题,求职者在平时的复习中会经 ...

  7. 07.并发编程Threads

    参考文档 https://www.cnblogs.com/springsnow/p/9409205.html#_label0 1. 基础概念 1.1 进程/线程/多线程 进程(Process) 计算机 ...

  8. Note -「线性规划」学习笔记

    \(\mathcal{Definition}\)   线性规划(Linear Programming, LP)形式上是对如下问题的描述: \[\operatorname{maximize}~~~~z= ...

  9. dbTable

    标签: <my-Double-Table double-Table="doubleTable" head-List="headList" select-M ...

  10. DevOpts 前端开发和 Spug

    DevOpts 前端开发和 Spug 朋友新工作是进行 DevOpts 前端开发,涉及 Spug. DevOps 是什么 DevOps 是一种思想.用于促进开发和运维之间的沟通.协作或整合. Tip: ...