SSM框架整合的配置文件

SSM框架整合常用的注解驱动有三个

Spring MVC框架web层):可以配置Spring框架通用的注解驱动<context:component-scan/>;如果需要配置SpringMVC框架推荐的映射器RequestMappingHandlerMapping、适配器RequestMappingHandlerAdapter,就需要专属的注解驱动<mvc:annotation-driven/><context:component-scan/>结合使用。

Spring AOP框架(service层)可以配置Spring框架通用的注解驱动<context:component-scan/>;如果事务管理采用注解注入时就必须额外配置注解驱动配置<tx:annotation-driven transaction-manager="transactionManager" />,但是一般都采用xml注入的方式,只需要配置一次就行了。

Mybatis(dao层)现在dao层流行使用的框架是Mybatis,Spring与Mybatis框架整合配置的注解驱动也是<context:component-scan/>。若果使用Spring JdbcTemplate框架,只需要配置Spring框架通用的注解驱动<context:component-scan/>就行了。


下面是SSM框架整合的配置文件

1、Spring与Mybatis的配置文件整合

<?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: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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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">
	<!-- 开启占位符解析器 -->
	<context:property-placeholder location="classpath:jdbc.properties"
		ignore-resource-not-found="true" system-properties-mode="FALLBACK" />
	<!-- 将SqlSessionFactory交给spring的IOC容器管理 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- mybatis核心配置文件路径 -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
		<!-- 
			mapperLocations:配置映射文件 
			value:配置映射文件路径,在根目录下的mybatis中的mappers目录下的任意多级目录(0个或多个)都进行扫描 
		-->
		<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml"></property>
		<!-- 配置别名扫描的包 -->
		<property name="typeAliasesPackage" value="com.the.one.usermanage.pojo"></property>
	</bean>
	<!-- druid数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 配置mapper扫描:一次性将扫描到的多个mapper接口交给IOC容器管理 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置mapper接口所在包路径 -->
		<property name="basePackage" value="com.the.one.usermanage.mapper"></property>
	</bean>
</beans>

2、Spring事务管理的配置文件

<?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: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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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">
	<!-- service层注解驱动 -->
	<context:component-scan base-package="com.the.one.usermanage.service"></context:component-scan>
	<!-- jdbc配置文件的解析器 -->
	<context:property-placeholder location="classpath:jdbc.properties"
		ignore-resource-not-found="true" system-properties-mode="FALLBACK" />
	<!-- 将SqlSessionFactory交给spring的IOC容器管理 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml"></property>
		<!-- 配置别名扫描的包 -->
		<property name="typeAliasesPackage" value="com.the.one.usermanage.pojo"></property>
	</bean>
	<!-- druid数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 配置事物管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置事务策略 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="query*" read-only="true" propagation="SUPPORTS" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置aop切面 -->
	<aop:config>
		<!-- 配置切入点表达式 -->
		<aop:pointcut expression="execution(* com.the.one.usermanage.service.*.*(..))"
			id="pointcut" />
		<!-- 将通知应用到切入点 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
	</aop:config>
</beans>

3、SpringMVC的配置文件

<?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:p="http://www.springframework.org/schema/p"
	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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.the.one.usermanage.controller"></context:component-scan>

	<!-- 配置注解驱动,相当于配置了映射器和适配器 -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<!-- 解决静态资源被DispacherServlet拦截问题 -->
	<mvc:default-servlet-handler />

	<!-- 配置视图解析器配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<bean name="userExcelView" class="com.the.one.usermanage.view.UserExcelView"></bean>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="1"></property>
	</bean>
</beans>

4、Spring全局的配置文件

<?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: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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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">
	<!-- 全局的注解扫描 -->
	<context:component-scan base-package="com.the.one.usermanage"></context:component-scan>
</beans>
注意:当Controller、Service、Repository都配置了自己的注解驱动时,全局的注解驱动就不要在配置了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值