SSM整合篇】三. SSM整合+事务+单元测试案例 完结 (共四章)

2024-03-28 18:32

本文主要是介绍SSM整合篇】三. SSM整合+事务+单元测试案例 完结 (共四章),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring+SpringMvc+MyBatis整合+事务+单元测试案例 第四章

github源码(day56-ssm-transaction)https://github.com/1196557363/ideaMavenProject

对一些知识点不懂可以参考 SSM整合篇】一. Spring+SpringMvc+MyBatis简单案例

该案例接上章【SSM整合篇】三. SSM整合+事务+单元测试案例 第三章 (共四章)

8 Spring和SpringMVC整合

8.1 定义Controller

package com.wpj.web.controller;import org.springframework.stereotype.*;/*** ClassName: Controller* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 21:55* @since JDK 1.8*/
@Controller
@RequestMapping("/emp")
public class EmpController {}

8.2 创建springmvc.xml并配置


<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 开启包扫描--><context:component-scan base-package="com.wpj" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 开启注解驱动--><mvc:annotation-driven/><!-- 配置thymeleaf的视图解析器--><bean id="templateResolver"class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!--前缀配置--><property name="prefix" value="/"></property> <!--后缀配置--><property name="suffix" value=".html"></property> <!--模板类型--><property name="templateMode" value="HTML"></property> <!--不使用缓存--><property name="cacheable" value="false"></property> <!--编码类型--><property name="characterEncoding" value="UTF-8"></property></bean><!--模板引擎配置--><bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver"></property></bean><!--视图处理器--><bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine"></property><property name="characterEncoding" value="UTF-8"></property></bean><!-- 静态资源文件处理--><mvc:default-servlet-handler/></beans>

8.3 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>Archetype Created Web Application</display-name><!-- Filter--><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- lsiteneter--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><!-- 在Tomcat启动的时候初始化Spring容器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Servlet--><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 优先级--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

8.4 Controller写映射路径

package com.wpj.web.controller;import com.wpj.bean.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;import java.util.*;/*** ClassName: Controller* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 21:55* @since JDK 1.8*/
@Controller
@RequestMapping("/emp")
public class EmpController {@Autowiredprivate IEmpService iEmpService;@RequestMapping("/getAllEmp")public String getAllEmp(ModelMap map){List<Emp> empList = iEmpService.getAllEmp();map.put("empList",empList);return "list";}
}

8.5 运行

http://localhost:8080/emp/getAllEmpemp ⇒   controller映射路径getAllEmp ⇒  controller内定义方法映射路径

8.6 结果

在这里插入图片描述

8.6.1 ssm整合成功。

这篇关于SSM整合篇】三. SSM整合+事务+单元测试案例 完结 (共四章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/856380

相关文章

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

springboot整合TDengine全过程

《springboot整合TDengine全过程》:本文主要介绍springboot整合TDengine全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录环境准备JDBC-JNI方式准备依赖实体类Mapper配置类测试类RESTful方式实体类配置类测试类总结

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4