SSM整合案例配置文件詳解
項目的目錄結構
Spring.xml配置
1 <?xml version="1.0" encoding="utf-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:aop="http://www.springframework.org/schema/aop" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.1.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 17 http://www.springframework.org/schema/mvc 18 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 19 20 <!--開啟注解掃描--> 21 <context:component-scan base-package="SSM"> 22 <!--exclude不掃描 注解為Controller--> 23 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 24 </context:component-scan> 25 26 <!--Spring整合mybatis--> 27 28 <!-- 配置連接池--> 29 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 30 <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> 31 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> 32 <property name="user" value="root"/> 33 <property name="password" value="hhp1998."/> 34 </bean> 35 36 37 <!-- 配置sqlsessionfactory對象--> 38 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 39 <property name="dataSource" ref="dataSource"/> 40 <!-- <property name="mapperLocations" value=""/> 41 它表示我們的Mapper文件存放的位置,當我們的Mapper文件跟對應的Mapper接口處於同一位置的時候可以不用指定該屬性的值 42 <property name="typeAliasesPackage" value=""/> 43 它一般對應我們的實體類所在的包,這個時候會自動取對應包中不包括包名的簡單類名作為包括包名的別名 44 --> 45 </bean> 46 47 48 <!-- 配置AccountDao所在的包--> 49 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 50 <property name="basePackage" value="SSM.dao"/> 51 </bean> 52 53 <!--配置Spring框架聲明式事務管理--> 54 55 <!--配置事務管理器--> 56 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 57 <property name="dataSource" ref="dataSource"/> 58 </bean> 59 60 <!--配置事務通知--> 61 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 62 <tx:attributes> 63 <tx:method name="find*" read-only="true"/> 64 <tx:method name="*" isolation="DEFAULT"/> 65 </tx:attributes> 66 </tx:advice> 67 68 <!--配置AOP增強--> 69 <aop:config> 70 <aop:advisor advice-ref="txAdvice" pointcut="execution(* SSM.service.impl.*ServiceImpl.*(..))"/> 71 </aop:config> 72 73 </beans>
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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--開啟注解掃描--> <context:component-scan base-package="SSM"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> </context:component-scan> <!--配置試圖解析對象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!--過濾靜態資源--> <mvc:resources mapping="/css/*" location="/css/"/> <mvc:resources mapping="/images/*" location="/images/"/> <mvc:resources mapping="/js/*" location="/js/"/> <!--開始SpringMVC注解的支持--> <mvc:annotation-driven/> </beans>
WEB.XML配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--配置Spring監聽器默認加載web-inf下的applicatiContext文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--設置配置文件路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:Spring.xml</param-value> </context-param> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加載springmvc.xml的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:SpringMVC.xml</param-value> </init-param> <!--啟動服務器,創建Servlet--> <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> </web-app>