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. Python—列表元组和字典

    Python-列表元组和字典 列表 元组 字典 列表: 列表是Python中的一种数据结构,他可以存储不同类型的数据.尽量存储同一种类型 列表索引是从0开始的,我们可以通过索引来访问列表的值. 列表的 ...

  2. Ubuntu18配置静态IP地址

    1. 记住网卡名称 ifconfig 2. 记住网关地址 netstat -rn 3. 配置静态IP 注意:Ubuntu18固定IP的方式跟Ubuntu18之前版本的的配置方式不同, Ubuntu18 ...

  3. Oracle - Trunc() 函数截取日期&截取数值

    Oracle TRUNC函数可以截取数字和日期类型:截取日期:select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; --显示当前时间 s ...

  4. Solution Set - Stirling 数相关杂题

      <好多题的题解>   「洛谷 P5408」第一类斯特林数·行   根据结论 \[x^{\overline{n}}=\sum_i{n\brack i}x^i, \] 我们只需要求出 \( ...

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

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

  6. Solution -「CF 1392H」ZS Shuffles Cards

    \(\mathcal{Description}\)   Link.   打乱的 \(n\) 张编号 \(1\sim n\) 的数字排和 \(m\) 张鬼牌.随机抽牌,若抽到数字,将数字加入集合 \(S ...

  7. 使用docker部署awx-1.7.1.0(ansible图形化界面)

    文章目录 关于环境 下载awx 下载安装所需依赖 安装docker-compose 配置inventory文件 出现的报错 TASK [local_docker : Run migrations in ...

  8. python对文件夹内文件去重

    昨天无聊写了一个百度图片爬虫,测试了一下搜索"斗图".一下给我下了3000多个图片,关键是有一半以上重复的.what a fuck program ! 好吧,今天写一个文件去重功能 ...

  9. 打破刻板印象,了解真正的商业智能BI

    ​在技术飞速发展的过程中,人们越来越怀疑传统的数据分析方法.可以通过对商业智能的各种误解来解释这一点,如今,这种误解正在作为有效的真理传播.例如,数据仓库已达到其目标.而数据质量似乎也正在失去其相关性 ...

  10. Hadoop - HDFS学习笔记(详细)

    第1章 HDFS概述 hdfs背景意义 hdfs是一个分布式文件系统 使用场景:适合一次写入,多次读出的场景,且不支持文件的修改. 优缺点 高容错性,适合处理大数据(数据PB级别,百万规模文件),可部 ...