配置文件:SSM
【总】
Mybatis中:别名、setting、数据源、mapper接口扫描;
Spring中:加载启动SpringIOC容器、开启注解(生成bean)、数据源、事务;
SpringMVC中:DispatcherServlet前端控制器、开启注解(Handler处理器)、处理器映射器、处理器适配器、视图解析器;
web.xml文件中(Spring、SpringMVC):DispatcherServlet前端控制器(springmvc.xml)、加载启动SpringIOC容器(applicationContext.xml)
springmvc.xml文件中(SpringMVC):开启注解扫描、视图解析器、(处映、处适省、静态资源过滤、统一异常处理、图片上传、自定义转换器、json)
applicationContext.xml文件中(Spring):开启注解支持、数据源、SqlSessionFactory(数据源、mybatis配置文件(SqlMapConfig.xml)、别名包扫描、mapper映射文件)、mapper接口扫描、事务
SqlMapConfig.xml文件中(Mybatiis):setting
1.【Mybatis】
SqlMapConfig.xml: 别名、setting、数据源、mapper接口扫描
【Spring】
web.xml: 加载启动Spring框架(加载spring的IOC容器applicationContext.xml)
applicationContext.xml: 开启注解扫描(生成bean)、配置数据源、配置事务管理器(用到数据源)
【SpringMVC】
web.xml: DispatcherServlet前端控制器(springmvc.xml)
springmvc.xml: 开启注解扫描(配置注解开发的handler(Controller)即handler处理器)、处理器映射器、处理器适配器、视图解析器
2.【Mybatis与Spring整合】
web.xml: 加载启动Spring框架(加载spring的IOC容器applicationContext.xml)
applicationContext.xml: 开启注解扫描(生成bean)、配置数据源、配置SqlSessionFactory(数据源、mybatis配置文件SqlMapConfig.xml、别名包扫描、mapper映射文件)、配置mapper接口扫描器、配置事务管理器(用到数据源)
3.【SSM框架整合】
1.web.xml(SpringMVC、Spring) — 配置DispatcherServlet前端控制器(springmvc.xml)、加载启动Spring框架(加载spring的IOC容器applicationContext.xml)、
启动跳转页、处理post请求时中文乱码
2.springmvc.xml(SpringMVC) — 开启注解扫描(配置注解开发的handler(Controller))、配置注解映射器(可省)、配置注解适配器(可省)、配置视图解析器、静态资源过滤、配置springmvc json、配置自定义转换器、配置统一异常处理、配置图片上传
3.applicationContext.xml(Spring) — 开启注解支持、配置数据源、配置SqlSessionFactory(数据源、mybatis配置文件SqlMapConfig.xml、别名包扫描、mapper映射文件)、
配置mapper接口
扫描器、配置事务管理
器(数据源、再导入下面:xml/注解事务扫描)、
application_xml.xml — 配置XML事务控制(事务通知、AOP)
application_anno.xml — 配置注解事务控制
4.SqlMapConfig.xml(mybatis) — setting
4.【SSM框架整合】与【Mybatis与Spring整合】区别: SSM框架整合的 web.xml 中多了 配置DispatcherServlet前端控制器(springmvc.xml)、和整个 springmvc.xml 文件;
1. web.xml
(springmvc的web.xml文件< web-app>< /web-app>)
<?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_3_1.xsd"
version="3.0">
<!-- 1.配置前端控制器 DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><!-- springmvc 配置文件的位置 -->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 2.加载启动Spring框架 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3.post时中文乱码-->
<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>
<!-- 4.启动跳转到首页 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!--<welcome-file-list>
<welcome-file>/employee_login.jsp</welcome-file>
</welcome-file-list>-->
<!--<welcome-file-list>
<welcome-file>/manage_login.jsp</welcome-file>
</welcome-file-list>-->
<!--<welcome-file-list>
<welcome-file>/WEB-INF/jsp/main/main.jsp</welcome-file>
</welcome-file-list>-->
</web-app>
2. springmvc.xml
(springmvc配置文件< beans>< /beans>)
<?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/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">
<!-- 1.开启注解扫描 -->
<context:component-scan base-package="com.zhizuobiao"/>
<!-- 2.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/> //视图名称-前缀
<property name="suffix" value=".jsp"/> //视图名称-后缀
</bean>
<!-- 3.配置静态资源过滤 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<mvc:resources mapping="/image/**" location="/image/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<!-- 4.配置springmvc json -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
</list>
</property>
</bean>
<!-- 5.配置自定义转换器conversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters"> <!-- 转换器 -->
<list>
<bean class="com.zhizuobiao.converter.CustomDateConverter"/>
</list>
</property>
</bean>
<!-- 6.配置统一异常处理 -->
<bean class="com.zhizuobiao.exception.ExceptionResolver"></bean>
<!-- 7.配置图片上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize"> //上传文件大小限定50M
<value>52428800</value>
</property>
</bean>
</beans>
3. applicationContext.xml
(spring配置文件< beans>< /beans>)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1.开启注解支持-->
<context:annotation-config/>
<!--<context:component-scan base-package="com.zhizuobiao"/>-->
<!-- 2.配置数据源 -->
<!-- 配置DBCP连接池 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db_driver}"/>
<property name="url" value="${db_url}"/>
<property name="username" value="${db_username}"/>
<property name="password" value="${db_password}"/>
</bean>
<!-- 3.mybatis spring整合(配置SqlSessionFactory) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcpDataSource"></property><!-- 数据源 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property><!--mybaits的配置文件-->
<property name="typeAliasesPackage" value="com.zhizuobiao.entity"></property><!--别名包扫描-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property><!-- mapper 映射文件 -->
</bean>
<!-- 4.配置mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhizuobiao.dao"></property><!--指定扫描mapper接口的位置-->
</bean>
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dbcpDataSource"></property><!-- 需要数据源 -->
</bean>
<!-- xml aop 事务控制 -->
<!--<import resource="classpath:spring/transaction_xml.xml"></import>-->
<!-- 注解 aop 事务控制 -->
<import resource="classpath:spring/transaction_anno.xml"></import>
</beans>
3.1 transaction_xml.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- xml aop 事务控制 -->
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="txPointCut" expression="execution(* com.zhizuobiao.service.*.*(..))"></aop:pointcut>
<!-- 切面(事务通知 + 切入点) -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor>
</aop:config>
</beans>
3.2 transaction_anno.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- xml aop 事务控制 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4. mybatis-config.xml
(mybatis配置文件< configuration>< /configuration>)
<?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>
<settings>
<setting name="lazyLoadingEnabled" value="true"/> //延迟加载
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="cacheEnabled" value="true"/> //二级缓存
</settings>
</configuration>