SSM配置檔案之一
配置檔案:SSM
【總】
Mybatis中:別名、setting、資料來源、mapper介面掃描;
Spring中:載入啟動SpringIOC容器、開啟註解(生成bean)、資料來源、事務;
SpringMVC中:DispatcherServlet前端控制器、開啟註解(Handler處理器)、處理器對映器、處理器介面卡、檢視解析器;
web.xml檔案中(Spring、SpringMVC):DispatcherServlet前端控制器(springmvc.xml)、載入啟動SpringIOC容器(applicationContext.xml)
springmvc.xml檔案中
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:
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>