其他分享
首页 > 其他分享> > SSM整合

SSM整合

作者:互联网

搭建整合环境

  1. 整合说明:我们通常使用Spring框架来整合SpringMVC和Mybatis,且使用注解+XML的形式
  2. 整合的思路
    1. 先搭建整合的环境(porm.xml)
    2. 先把Spring的配置搭建完成
    3. 再使用Spring整合SpringMVC框架
    4. 最后使用Spring整合MyBatis框架

搭建和测试Spring框架

  1. 创建ApplicationContext.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: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">

    <!--Spring框架只需要负责业务层和持久层即可,controller由SpringMVC框架来负责-->
    <context:component-scan base-package="service"></context:component-scan>
    <context:component-scan base-package="dao"></context:component-scan>

</beans>
  1. 在测试类中测试业务层方法
public class TestSpring {
    private AccountService accountService;

    @Before
    public void init(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        accountService = ac.getBean("accountService", AccountService.class);
    }
    @Test
    public void testSpring(){
        accountService.findAll();
    }
}

搭建和测试SpringMVC框架

  1. 在web.xml中配置DispatcherServlet前端控制器、编码过滤器
  <!--配置编码过滤器-->
<filter>
  <filter-name>encoding</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>encoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器-->
<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>
  1. 创建SpringMVC.xml的配置文件,编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描Controller包下的注解-->
    <context:component-scan base-package="controller"></context:component-scan>

    <!--开启SpringMVC的注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--视图解析器-->
    <bean id="viewController" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--静态资源不过滤-->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
</beans>
  1. 创建AccountController类,编写方法,进行测试
@Controller
public class AccountController {

    @RequestMapping("testMVC")
    public String testMVC(){
        System.out.println("testMVC执行了!");
        return "success";
    }
}

Spring整合SpringMVC框架

  1. 目的:在controller中能成功的调用service业务层对象中的方法。

  2. 整合原理
    在这里插入图片描述

  3. 实现步骤

    1. 在项目启动的时候,就去加载applicationContext.xml的配置文件,创建好容器
    2. 在web.xml中配置ContextLoaderListener监听器(该监听器默认只能加载WEB-INF目录下的applicationContext.xml的配置文件)
    3. 在controller中注入service对象,调用service对象的方法进行测试
<!--
  配置Spring的监听器(监听ServletContext域对象的创建与销毁)
  当ServletContext域对象被创建(服务器一开启),默认加载WEB-INF下的applicationContext.xml文件
  但是我们的Spring的配置文件在类路径下
  需要配置加载类路径下的配置文件,使用<context-param>标签
-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--配置加载类路径下的配置文件-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("testMVC")
    public String testMVC(){
        System.out.println("表现层:testMVC执行了!");
        accountService.findAll();
        return "success";
    }
}

标签:xml,SpringMVC,Spring,SSM,accountService,testMVC,整合,public
来源: https://blog.csdn.net/qq_41891057/article/details/104680767