第一步:保证业务层(Service)和持久层(Dao)在web项目下运行
配置applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--告知spring容器,需要扫描注解配置的包-->
<context:component-scan base-package="com.baidu">
<!--制定扫描规则,排除过滤带有@Controller注解的类-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
</beans>
第二步:保证表现层(Controller)在web项目下运行
配置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>
<!--配置servlet-->
<servlet>
<!--配置前端控制器-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!----初始化配置,加载springmvc的配置文件-->
<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>
<!--解决中文乱码的过滤器-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--配置过滤器的拦截方式-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--告知spring容器,需要扫描注解配置的包-->
<context:component-scan base-package="com.baidu">
<!--制定扫描规则,包含过滤@Controller注解的java类-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller">
</context:component-scan>
<!--配置内部资源视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--使用set方法注入prefix,suffix属性-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置处理器映射器和处理器适配器,可以省略,会默认配置-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
第三步:整合spring和springmvc
配置web.xml
...
<!--配置spring容器提供的环境加载监听器,用于开启服务器时加载applicationContext.xml,但是这个监听器默认只能读取WEB-INF目录下的applicationCOntext.xml-->
<listener>
<listener-calss>
org.springframework.web.context.ContextLoaderListener
</listener-calss>
</listener>
<!--手动指定applicationContext.xml的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
...
<servlet>
</servlet>
第四步:保证MyBatis在web项目下运行
a.配置SqlMapperConfig.xml
b.配置映射配置文件
SqlSessionFactoryBean implements FactoryBean
第五步:整合spring和Mybatis,在applicationContext.xml中配置
a.<!--spring接管SqlSession工厂,配置SqlSessionFactoryBean对象-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--使用set方法注入dataSource属性-->
<property name="dataSource" ref="dataSource"></property>
<!--如果需要用到sqlMapConfig.xml中的配置,注入configLocation的属性-->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!--如果需要使用延迟加载,二级缓存再配置也行-->
</bean>
<!--加载properties配置文件-->
<context:property-placeholder location="classpath:jabcConfig.properties"></context:property-placeholder>
<!--配置数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--使用set方法注入属性-->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
b.<!--配置mapper扫描器对象-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--使用set方法注入basePackage属性-->
<property name="basePackage" value="com.baidu.dao"></property>
</bean>
<!--在service层配置事务传播------------------------------------->
c.<!--配置spring事务管理器对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!--配置事务通知的方法-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pc" expression="execution(* com.baidu.service.impl.*.*(..))"></aop:pointcut>
<!--建立切入点跟通知间的对应关系-->
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice">
</aop:advisor>
</aop:config>
ssm--springmvc3
猜你喜欢
转载自blog.csdn.net/qq_42514129/article/details/84915090
今日推荐
周排行