Struts2+Spring+Ibatis集成合并

  上一篇博客讲述了Struts2+Spring的集成合并,主要是利用了一个中间jar包,这篇博客在加上Ibatis持久层框架,三个框架进行合并。其中Struts2Spring部分和前边的一样,主要是讲解SpringIbatis之间的合并,这里也涉及到SpringAOP编程思想,声明式事务的使用。

        一,看一下分工吧:

                  Struts2:负责流程控制,主要针对的是从JSP页面到action类这一块的步骤。

                  Spring:负责各个类,对象的创建,包括actionservice,dao,数据连接对象,Ibatis框架的核心对象sqlMapClientSpring自身的事务处理扩展对象transactionManager

                 Ibatis:负责对JDBC的封装,简化访问数据的程序。

  

        二,环境的搭建:

                   1,需要考入的jar包:

commons-fileupload-1.2.1.jar(Struts2:文件上传)

commons-io-1.3.2.jar(Struts2:文件上传)

freemarker-2.3.15.jar(Struts2:视图展现技术)

ognl-2.7.3.jar(Struts2:对象图形导航语言,用于做数据操作)

struts2-core-2.1.8.1.jar(Struts2:核心jar包)

xwork-core-2.1.6.jar(Struts2:webwork框架的核心jar包)

commons-logging.jar(Spring:日志输出操作,实现日志输出的转换)

junit-3.8.2.jar(非必须:单元测试jar包)

log4j-1.2.15.jar(非必须:日志输出jar包)

spring.jar(Spring:核心jar包)

struts2-spring-plugin-2.1.8.1.jar(Struts2Spring的合并jar包)

c3p0-0.9.1.2.jar(做数据源操作的jar包)

ibatis-2.3.4.726.jar(Ibatis:核心jar包)

ojdbc14.jar(oracle数据库的连接jar包)

aspectjrt.jar(Spring:支持AOP功能的jar包)

aspectjweaver.jar(Spring:支持AOP功能的jar包)

cglib-nodep-2.1_3.jar(Spring:实现基于继承的动态代理)

                  2,拷贝进去的配置文件:

                       Struts2:struts.xml

                      Spring:applicationContext.xml

     Ibatis:SqlMapConfig.xml  SqlMap.xml

     日志输出的属性文件:log4j.properties

      配置数据库连接信息:db.properties

 

            三,各个配置文件的编写:

 

                  1struts.xml中,主要用来配置action类,

  1. <span style="font-size:18px;"><package name="system" namespace="/system" extends="struts-default">                <action name="insertUser" class="userAction" method="insert">  
  2.                     <result name="success" type="redirect">/userlist.jsp</result>  
  3.                     <result name="error" type="redirect">/error.jsp</result>  
  4.                 </action>  
  5.             </package>  
  6. </span>  



                 2applicationContext.xml:

 

  1. <span style="font-size:18px;">          <?xml version="1.0" encoding="UTF-8"?>  
  2.                
  3.             <beans xmlns="http://www.springframework.org/schema/beans"  
  4.                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.                     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.                     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.                     xsi:schemaLocation="  
  8.                         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  10.                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  11.                
  12.                   
  13.                 <bean id="loginAction" class="com.ljh.action.LoginAction" scope="prototype">  
  14.                     <property name="userService" ref="userService"></property>  
  15.                 </bean>  
  16.                    
  17.                 <!-- action层类的设置,关联service -->  
  18.                 <bean id="userAction" class="com.ljh.action.UserAction" scope="prototype">  
  19.                     <property name="userService" ref="userService"></property>  
  20.                 </bean>  
  21.                 <!-- service层类的设置,关联dao -->  
  22.                 <bean id="userService" class="com.ljh.service.UserService" >  
  23.                     <property name="userDao" ref="userDao"></property>  
  24.                 </bean>  
  25.                 <!-- dao层类的设置 ,关联sqlMapClient-->  
  26.                 <bean id="userDao" class="com.ljh.dao.UserDao">  
  27.                     <property name="sqlMapClient" ref="sqlMapClient"></property>  
  28.                 </bean>  
  29.                    
  30.                 <!-- 表示把属性资源文件的信息加载到Spring环境中进行利用 -->  
  31.                 <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
  32.                     <property name="locations">  
  33.                         <list>  
  34.                             <value>classpath:db.properties</value>  
  35.                         </list>  
  36.                     </property>  
  37.                 </bean>  
  38.                    
  39.                 <!-- 配置C3P0数据源连接池,通过读取属性文件 -->  
  40.                 <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  41.                     <property name="user" value="${username}"></property>  
  42.                     <property name="password" value="${password}"></property>  
  43.                     <property name="jdbcUrl" value="${url}"></property>  
  44.                     <property name="driverClass" value="${driverClass}"></property>                  
  45.                 </bean>  
  46.                
  47.                 <!-- IBatis核心对象sqlMapClient的声明 ,通过工厂SqlMapClientFactoryBean和IBatis核心配置文件-->  
  48.                 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  49.                     <property name="dataSource" ref="c3p0"></property>  
  50.                     <property name="configLocations">  
  51.                         <list>  
  52.                             <value>classpath:SqlMapConfig.xml</value>  
  53.                         </list>  
  54.                     </property>  
  55.                 </bean>  
  56.                    
  57.                 <!-- 配置功能扩展对象 - 事务管理,通过c3p0 -->  
  58.                 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  59.                     <property name="dataSource" ref="c3p0"></property>  
  60.                 </bean>  
  61.                    
  62.                 <!-- 声明事务管理AOP功能 -->  
  63.                 <aop:config>  
  64.                     <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.ljh.service.*.*(..))"/>          
  65.                 </aop:config>          
  66.                    
  67.                 <!--事务的配置-->  
  68.                 <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
  69.                     <tx:attributes>  
  70.                         <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>  
  71.                         <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>  
  72.                         <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>  
  73.                         <tx:method name="select*" read-only="true"/>  
  74.                     </tx:attributes>  
  75.                 </tx:advice>  
  76.                
  77.             </beans>  
  78. </span>  



                   3SqlMapConfig.xml

  1. <span style="font-size:18px;">          <sqlMapConfig>  
  2.                 <!-- 只需配置映射文件即可 -->  
  3.                 <sqlMap resource="com/ljh/bean/User.xml" />  
  4.   
  5. </sqlMapConfig></span>  



                   4,User.xml(SqlMap.xml)

  1. <span style="font-size:18px;">          <sqlMap namespace="empSQL">  
  2.                     <insert id="insertUserSQL">  
  3.                     insert into t_user(usercode,userpswd,username,orgtype,regdate) values(#usercode#,#userpswd#,#username#,#orgtype#,#regdate#)  
  4.                 </insert>  
  5.   
  6. </sqlMap></span>  


                     5,属性文件db.properties:

 

  1. <span style="font-size:18px;">              url=jdbc:oracle:thin:@127.0.0.1:1521:ljh  
  2.                 driverClass=oracle.jdbc.driver.OracleDriver  
  3.                 username=scott  
  4.   
  5. password=ti</span>  

 


            四,对于jsp和其他三层这里不再给出。但是提醒一点,service中需要将Exception剖出,这样才会被框架捕捉到,事务才能起作用。Dao层可以直接使用sqlMapClient来调用其中的增删改查的方法。

    

           这样我们的三个框架的合并就算完成,在实际中,根据业务进行相关类的添加,相关配置文件的补充即可。总之多运用。

原文地址:https://www.cnblogs.com/waiwai1015/p/4772814.html