【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”
posted @ 2020-09-08 20:39  JWnMing  阅读(100)  评论(0编辑  收藏  举报