1. 程式人生 > 實用技巧 >【Spring】Spring整合Strust2、Mybatis

【Spring】Spring整合Strust2、Mybatis

11).Spring + Struts2(JdbcTemplate) + myBatis整合使用

(1)Spring整合Struts2(SS)
開發步驟:
1)環境搭建
① 匯入相關的jar:spring的相關jar、struts2的相關的jar、struts2-spring-plugins的jar
② 匯入配置檔案
   a. spring的配置檔案【放在src下任意目錄】:applicationContext.xml
   b. log4j日誌檔案【必須放在src根目錄下】
   c. struts2的配置檔案【必須放在src根目錄下】:struts.xml
   d. web.xml配置檔案
③ 初始化配置
   <!-- 1. struts2的核心控制器 -->
   	<filter>
   		<filter-name>struts2</filter-name>
   		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   	</filter>
   	<filter-mapping>
   		<filter-name>struts2</filter-name>
   		<url-pattern>/*</url-pattern>
   	</filter-mapping>
   	<!--2. 初始化spring工廠的監聽器  -->
   	<listener>
   		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   	</listener>
   	<!--3. 手動指定配置檔案路徑  -->
   	<context-param>
   		<param-name>contextConfigLocation</param-name>
   		<param-value>classpath:/applicationContext.xml</param-value>
   	</context-param>
2)開發Sturts2的action
① action開發
② 交給spring管理(建立多個)
   在bean下管理action時,設定為多例,scope=“prototype”
③ 配置action的訪問路徑
   action標籤的class路徑為上面被管理的bean的id
(2)Spring + Struts2 + JdbcTemplate整合(SSJ)
搭建環境: 
① 匯入相關的資源(jar+配置檔案)
    相關的jar:
        spring相關
        struts2相關
        ojdbc5.jar
        druid連線池的jar
        struts整合spring的jar
        日誌的jar
        aop依賴的jar
            asm-3.3.1.jar
            cglib-2.2.2.jar
            com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
   相關的配置檔案:
   		applicationContext.xml
   		struts.xml
   		log4j.properties
   		web.xml
② 初始化配置
 	環境搭建型別的初始化:
 		struts核心控制器
 		spring工廠初始化監聽器
 		指定spring配置檔案路徑
 	編碼相關的初始化
 		初始化spring配置檔案中常用的內容
 		連線池
 		JdbcTemplate
 		txManager
 		tx:advice
 		aop:config
③ Spring工廠內部: action  service  dao  JdbcTemplate  DataSource
(3)Spring整合MyBatis(SM)
環境搭建
① 匯入jar
    spring jar
    mybatis jar
    aop 的jar
    ojdbc5.jar
    連線池的jar
    日誌的jar
    mybatis-spring的jar。
    提供兩個核心類:
      	SqlSessionFactoryBean  替代mybatis-config
      	MapperScannerConfigurer 代替mybatis生成dao物件的程式碼(注意:生成dao的id是介面名首字母小寫)
② 匯入配置檔案
   	applicationContext.xml
   	log4.properties
③ 初始化配置
	applicationContext.xml:
		連線池
		事務相關配置【txManager tx:advice aop】
		初始化mybatis整合相關配置:sqlSessionFactory、生成dao物件
(4)Spring + Struts2 + myBatis整合
搭建環境:
① 匯入jar:	spring、mybatis、struts2、mybatis整合spring、struts整合spring、aop、連線池、日誌、ojdbc5.jar
② 匯入配置檔案:	struts.xml、spring.xml、log4j、web.xml
③ 初始化配置:
	web.xml配置:① struts2核心控制器、② spring工廠初始化的監聽器、③ spring配置檔案路徑
	applicationContext.xml:
		① 連線池
		② 事務相關配置【txManager tx:advice aop】
		③ 初始化mybatis整合相關配置:sqlSessionFactory、生成dao物件
開發步驟:
例如註冊user功能:表—> 實體--> dao介面--> Mapper檔案(spring自動管理dao物件)--> Service介面--> 
service實現類(Spring管理service物件)-->  Action類(spring管理action 【建立多利】,對映action的訪問路徑和跳轉路徑  【class="action在spring工廠中的id"】)
 
	在mybatis的SQLSessionFactoryBean中開啟快取:
<property name="configurationProperties">
    		<props>   <prop key="cacheEnabled">true</prop>   </props>
    	</property>
	注意:這裡的引用屬性不用ref(spring舊版本),而是用value
 
易錯點:在獲得SQLSessionFactory物件的屬性mapperLocations的value為“classpath:/包/包/../*.xml”