SSM框架的整合

本博客分为两部分内容 一:SpringMvc拦截器 二:SSM框架的整合

在这里插入图片描述
一拦截器:
拦截器和过滤器的区别:
springmvc的处理器拦截器类似于Servlet开发中的过滤器,用于对处理器进行预处理和后处理
用户可以自定义拦截器来实现特定的功能

谈到拦截器,我们有拦截器链,拦截器链就是将拦截器按照一定的顺序连接成一条链

拦截器和过滤器的区别:
过滤器:是servlet规范中的一部分,任何java web工程都可以使用
拦截器:是springmvc框架自己的,只有使用了springmvc框架的工程才能使用
过滤器:在url-pattern中设置/*之后,可以对所有的资源访问进行拦截
拦截器:它只会拦截controller中的方法,如果访问静态资源 jsp,css,html,image,js等是不会进行拦截的

实现自定义的拦截器需要实现handlerIntercepetor接口。
在这里插入图片描述
那么自定义拦截器怎么来实现呢???
步骤:
1.编写自定义拦截器的类
2.配置拦截器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二:SSM框架的整合
步骤1:准备工作:
1.首先创建数据库
2.创建 dao service domain controller层
3.在dao层中,写方法接口
4. service层写接口和实现类
5. controller层写一个 *Controller类

在这里插入图片描述
我们整合三大框架是利用spring来整合SpringMvc和Mybatis框架的,起初我们应该保证三大框架的负责的每一层都能够单独的使用,最后在整合。
首先采用的是Spring框架的配置,可以完全是注解配置,但是我觉得注解+xml配置更方便
步骤2:开始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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
     

然后开启扫描:

<!-- 开启扫描 ,这样会导致所有的com.itcast下的注解都会扫描,但是Controller层我想使用SpringMvc来进行,
只需要扫描Service层和dao层即可-->
    <context:component-scan base-package="com.itcast">
        <!-- Controller注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

然后我们进行简单的测试,看看Service层的方法可以执行嘛,此时只需要输出,看一下即可,因为现在还不能查询数据库,主要是看IOC的容器,以及注解是否生效

public class TestSpring {
    @Test
    public  void testspring1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationcontext.xml");
        IAccountService as=context.getBean("accountService",IAccountService.class);
        as.findAll();
    }
}

当测试的方法就可以执行,那么我们就可以继续SpringMvc的配置了
步骤3:SpringMvc的配置
在web.xml中进行配置前端控制器 配置过滤器
在springmvc中开启Controller注解扫描,配置前端解析器,放行静态资源,开始mvc的注解支持
web.xml的配置

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <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>
  <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>

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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 开启注解扫描-->
    <context:component-scan base-package="com.itcast">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 配置前端解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 放行静态资源-->
    <mvc:resources mapping="/img/**" location="/img/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <!-- 开启springmvc的支持-->
    <mvc:annotation-driven/>
</beans>

我们进行测试SpringMvc是否可以访问到Controlelr层:
在这里插入图片描述
现在是我们SpringMvc的配置配好了,Spring的配置也好了,怎么整合到一起呢?

在这里插入图片描述
在controller类中,我们需要在里面加上service层的代码
一开始,我们肯定想到是通过注入,来创建AccountService的对象,但是在服务器启动时
在web.xml中我们配置了springmvc的配置文件,这里我们只扫描了Controller注解,spring的注解根本就没有被扫描,对象根本就没有在容器中

现在我们来介绍一个对象
ServletContext域对象:
在这里插入图片描述
所以我们需要配置web.xml的三大组件之一监听器:

<!-- 依赖于spring-web的jar包-->
  <!-- 配置Spring的监听器 默认情况下是加载WEN-INF/applicationContext.xml配置文件
       但是我们想放到resources下进行统一管理,我们只需要设置路径-->
  <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>

有了监听器,在tomcat启动的时候,我们就可以加载Spring的配置文件applicationcontext.xml
这样在IOC容器中就可以有dao层和Service层的对象了
记再进行测试哦
步骤4:Mybatis的配置
在这里插入图片描述
在这里插入图片描述
一开始学习Mybatis时我们会配置MybatisConfig.xml文件

<?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">
<!-- mybatis的主配置文件-->
<configuration>
    <!-- 配置环境-->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池)-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 指定映射配置文件,映射配置文件指的是每个dao独立的配置文件-->
    <!-- 用class属性,指定接口的全限定类名-->
    <mappers>
      <mapper class="com.itcast.dao.IAccountDao"></mapper>
    </mappers>
</configuration>

但是现在Spring整合Mybatis,只需要配置Spring的applicationcontext.xml即可

 <!-- 配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>

    </bean>
    <!--配置SqlSessionFactory工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置接口所在的包-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itcast.dao"></property>
    </bean>

我们查询的时候可以用直接查询,但是如果执行的是增删改,大家还记不记得,需要我们进行手动的session.commit()提交呢??
所以我们还需要在applicationcontext.xml进行配置事务管理器

<!-- 在增删改查的时候,需要提交事务,所以我们需要配置事务管理器-->
    <!-- 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
           <tx:method name="find*" read-only="true"/>
           <tx:method name="*" isolation="DEFAULT"/>
       </tx:attributes>
    </tx:advice>
    <!--配置Aop增强 -->
    <aop:config>
        <aop:pointcut id="allmethod" expression="execution(* com.itcast.service.Impl.*Serviceimpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allmethod"/>
    </aop:config>

我在配置<aop:comfig>的时候出现了个问题:

'aop:config' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。
因为<aop:advisor advice-ref="txAdvice" pointcut-ref="allmethod"/>后面加上了";"有时出现这种情况就是文件中有特殊字符的问题可以检查一下空格

这样我们的整合就结束了,再整体测试一遍吧!!!

在这里插入图片描述
希望能帮到你鸭!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值