本文主要是介绍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整合+事务+单元测试案例 完结 (共四章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!