SSM框架整合总结
配置Spring
- 在
applicationContext.xml
中开启注解扫描,但是不要扫描controller类型,spring只负责控制service和dao层, 将service层和dao层交由IOC容器进行管理<!-- 开启注解扫描,只处理service和dao, 不需要处理controller--> <context:component-scan base-package="com.wbh"> <!-- 配置不扫描的注解,表示Controller注解不扫描--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
配置SpringMVC
- 在web.xml中配置前端控制器和编码过滤器:
<!-- 配置前端控制器-->
<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>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 在springMVC.xml中配置:
- 注解支持
- 注解扫描(仅扫描controller包)
- 视图解析器
- 资源过滤器
<!-- 开启注解扫描, 只扫描controller-->
<context:component-scan base-package="com.wbh">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 开启注解支持-->
<mvc:annotation-driven/>
<!-- 配置视图解析器对象-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 过滤静态资源-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
整合Spring和SpringMVC
整合思路:需要启动Tomcat的时候完成加载application.xml, 完成依赖注入。解决方案:使用一个Listener监听,当ServletContext被创建的时候,加载application.xml文件,spring为我们提供好了一个监听器
ContextLoaderListener
,所以在web.xml中添加以下内容:
<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>
添加以上文字实现导入applicationContext.xml
,这样配置文件里面的自动扫描就扫描到了所有的service和dao,在Controller
中使用@AutoWired
即可为servise注入依赖
配置Mybatis(aplicationContext.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">
<configuration>
<!-- 配置环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?CharacterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="wbh20000329"/>
</dataSource>
</environment>
</environments>
<!-- 配置引入的映射配置文件或者注解路径-->
<mappers>
<package name="com.wbh.dao"/>
</mappers>
</configuration>
Spring整合Mybatis
在spring.xml中添加Mybatis的相关设置,配置完成后删除applicationContext.xml
<!-- spring整合Mybatis-->
<!-- 配置连接池-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai"/>
<property name="user" value="root"/>
<property name="password" value="wbh20000329"/>
</bean>
<!-- 配置session工厂-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置dao所在的包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
<property name="basePackage" value="com.wbh.dao"/>
</bean>
配置声明式事务管理器
<!-- 配置声明式事务事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知-->
<tx:advice transaction-manager="dataSourceTransactionManager" id="transactionInterceptor">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"
</tx:attributes>
</tx:advice>
<!-- 配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* com.wbh.service.impl.AccountServiceImpl.*(..))"/>
</aop:config>