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:
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>