SSM框架整合(配置檔案)

鳴振發表於2020-12-30

(SSM框架 配置檔案)

★SSM框架整合(配置檔案原始碼)
 <!-- 前言-->
 <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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
					http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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-3.1.xsd">	
<!-- 1.開啟註解 -->
	 <context:component-scan base-package="com.zb"/>
<!-- 2.檢視解析器 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property  name="prefix" value="/WEB-INF/jsp/"/>
					<property  name ="suffix" value="/jsp"/>
 	 </bean>
<!-- 3.引入資料來源配置檔案 -->
      <context:  property-placeholder  location="classpath:database.properties"/>
<!-- 4.連線資料來源 -->
	  <bean id ="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
	  		<property name="driverClassName" value="${driver}"/>
	  		<property name ="url" value="${url}"/>
	  		<property name ="username" value ="${user}"/>
	  		<property name ="password" value ="${password}"
	  </bean>
<!-- 5.配置SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<!-- 獲取資料來源 -->
		<property  name="dataSource" ref="dataSource"/>
		<!-- Mybatis別名-->
		<property name="typeAliasePackage" value="com.gr.ssm.po"/>
		<!-- Mybatis 配置檔案-->
		<property name="configLocation" value="classpath:mybatis-config.xml"/>
		<!--配置SQL對映檔案資訊 -->
		<property name ="mapperLocaltions">
				<list>
						<value>classpath:com/gr/ssm/dao/*.xml</value>
				</list>
		<property>
<!--  6.注入注射器(介面掃描器)-->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<!-- 當有多個sessionFactory例項時顯示注入SqlsessionFactory  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
		    <property name="basePackage" value="com.gr.ssm.dao"/>
		</bean>
<!--  7.事務管理器 -->
		<bean id="txManager" value="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"/>
		</bean>
<!--  8. 宣告式事務  -->
		<tx:advice id="txAdivce" transaction-manager="txManager">
					<tx:attributes>
						<tx:method="find*" propagation="REQUIRED"/>
						<tx:method="save*" propagation="REQUIRED"/>
						<tx:method="del*" propagation="REQUIRED"/>
						<tx:method="update*" propagation="REQUIRED"/>
						<tx:method="*" propagation="REQUIRED"/>
					</tx:attributes>
		<tx:advice>	
<!-- 	9. 定義切面  -->
		<aop:config>
				<!--  定義切點 -->
				<aop:pointcut expression="execution(* com.gr.ssm.service..*.*(..) " id="pc")"/>
				<!--  將事務增強和切點結合 -->
				<aop :advisor advice-ref="txAdvice" pointcut-ref="pc"/>
		</aop:config>
<!-- 10.靜態資源對映 -->

		<import resource="spring-mvc.xml"/>

相關文章