Spring_ibatis_jta多数据源配置

来源:互联网 发布:黑沙捏脸数据导入 编辑:程序博客网 时间:2024/06/06 21:04
  1. Spring+iBatis+JOTM实现JTA事务  
  2.    
  3. JOTM是个开源的JTA事务管理组件,可以让程序脱离J2EE容器而获得分布式事务管理的能力。  
  4.    
  5. 测试过程如下:  
  6.    
  7. 一、环境  
  8.    
  9. 1、准备软件环境  
  10.   
  11.  spring-framework-2.5.6.SEC01-with-dependencies.zip  
  12.  ibatis-2.3.4  
  13.  ow2-jotm-dist-2.1.4-bin.tar.gz  
  14.  MySQL-5.1  
  15.  JDK1.5  
  16.    
  17. 2、创建数据库环境,注意数据库引擎为InnoDB,只有这样才能支持事务。  
  18.    
  19. CREATE DATABASE IF NOT EXISTS testdb_a    DEFAULT CHARACTER SET utf8;   
  20.   
  21. USE testdb_a;   
  22.   
  23. DROP TABLE IF EXISTS tab_a;   
  24.   
  25. CREATE TABLE tab_a (   
  26.     id bigint(20) NOT NULL,   
  27.     name varchar(60) DEFAULT NULL,   
  28.     address varchar(120) DEFAULT NULL,   
  29.     PRIMARY KEY (id)   
  30. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  31.   
  32.   
  33. CREATE DATABASE IF NOT EXISTS testdb_b    DEFAULT CHARACTER SET utf8;   
  34.   
  35. USE testdb_b;   
  36.   
  37. DROP TABLE IF EXISTS tab_b;   
  38.   
  39. CREATE TABLE tab_b (   
  40.     id bigint(20) NOT NULL,   
  41.     name varchar(60) DEFAULT NULL,   
  42.     address varchar(120) DEFAULT NULL,   
  43.     PRIMARY KEY (id)   
  44. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  45.   
  46.   
  47. 二、建立项目testJOTM  
  48.    
  49. 1、建立项目后,准备依赖的类库,结构如下:  
  50. │    spring-aop.jar   
  51. │    spring-beans.jar   
  52. │    spring-context-support.jar   
  53. │    spring-context.jar   
  54. │    spring-core.jar   
  55. │    spring-jdbc.jar   
  56. │    spring-jms.jar   
  57. │    spring-orm.jar   
  58. │    spring-test.jar   
  59. │    spring-tx.jar   
  60. │    spring-web.jar   
  61. │    spring-webmvc-portlet.jar   
  62. │    spring-webmvc-struts.jar   
  63. │    spring-webmvc.jar   
  64. │    aspectjrt.jar   
  65. │    aspectjweaver.jar   
  66. │    cglib-nodep-2.1_3.jar   
  67. │    asm-2.2.3.jar   
  68. │    log4j-1.2.15.jar   
  69. │    asm-commons-2.2.3.jar   
  70. │    asm-util-2.2.3.jar   
  71. │    aopalliance.jar   
  72. │    mysql-connector-java-5.1.6-bin.jar   
  73. │   
  74. ├─ibatis   
  75. │            ibatis-2.3.4.726.jar   
  76. │            sql-map-2.dtd   
  77. │            sql-map-config-2.dtd   
  78. │   
  79. ├─jotm   
  80. │            license.txt   
  81. │            xapool.jar   
  82. │            jotm-core.jar   
  83. │            jotm-standalone.jar   
  84. │            jotm-jms.jar   
  85. │            jotm-datasource.jar   
  86. │            ow2-jta-1.1-spec.jar   
  87. │            jotm-client.jar   
  88. │   
  89. ├─jakarta-commons   
  90. │            commons-attributes-api.jar   
  91. │            commons-attributes-compiler.jar   
  92. │            commons-beanutils.jar   
  93. │            commons-codec.jar   
  94. │            commons-collections.jar   
  95. │            commons-dbcp.jar   
  96. │            commons-digester.jar   
  97. │            commons-discovery.jar   
  98. │            commons-fileupload.jar   
  99. │            commons-httpclient.jar   
  100. │            commons-io.jar   
  101. │            commons-lang.jar   
  102. │            commons-logging.jar   
  103. │            commons-pool.jar   
  104. │            commons-validator.jar   
  105. │   
  106. ├─junit   
  107. │            junit-3.8.2.jar   
  108. │            junit-4.4.jar   
  109. │            license.txt   
  110. │   
  111. └─log4j   
  112.                 log4j-1.2.15.jar   
  113. 2、根据表建立entity和SQLMap  
  114. public class TabA implements Serializable {   
  115.         private Long id;   
  116.         private String name;   
  117.         private String address;   
  118.         //省略getter/setter   
  119. public class TabB implements Serializable {   
  120.         private Long id;   
  121.         private String name;   
  122.         private String address;   
  123.         //省略getter/setter  
  124. TabA.xml  
  125. <?xml version="1.0" encoding="UTF-8" ?>   
  126. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >   
  127.   
  128. <!-- 表名:tab_a -->   
  129. <sqlMap namespace="tab_a">   
  130.         <typeAlias alias="TabA" type="com.lavasoft.stu.jtom.entity.TabA"/>   
  131.         <resultMap id="result_base" class="TabA">   
  132.                 <result property="id" column="id"/>   
  133.                 <result property="name" column="name"/>   
  134.                 <result property="address" column="address"/>   
  135.         </resultMap>   
  136.         <!-- 添加 -->   
  137.         <insert id="insert" parameterClass="TabA">   
  138.                 insert into tab_a(   
  139.                 id,   
  140.                 name,   
  141.                 address   
  142.                 ) values (   
  143.                 #id#,   
  144.                 #name#,   
  145.                 #address#   
  146.                 )   
  147.                 <selectKey keyProperty="id" resultClass="long">   
  148.                         select LAST_INSERT_ID()   
  149.                 </selectKey>   
  150.         </insert>   
  151.         <!-- 更新 -->   
  152.         <update id="update" parameterClass="TabA">   
  153.                 update tab_a set   
  154.                 id = #id#,   
  155.                 name = #name#,   
  156.                 address = #address#   
  157.                 where id = #id#   
  158.         </update>   
  159.         <!-- 删除 -->   
  160.         <delete id="deleteById" parameterClass="long">   
  161.                 delete from tab_a   
  162.                 where id = #value#   
  163.         </delete>   
  164.         <!-- 根据ID获取 -->   
  165.         <select id="findById" parameterClass="long" resultMap="tab_a.result_base">   
  166.                 select *   
  167.                 from tab_a   
  168.                 where id = #value#   
  169.         </select>   
  170. </sqlMap>    
  171. TabB.xml  
  172. <?xml version="1.0" encoding="UTF-8" ?>   
  173. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >   
  174.   
  175. <!-- 表名:tab_b -->   
  176. <sqlMap namespace="tab_b">   
  177.         <typeAlias alias="TabB" type="com.lavasoft.stu.jtom.entity.TabB"/>   
  178.         <resultMap id="result_base" class="TabB">   
  179.                 <result property="id" column="id"/>   
  180.                 <result property="name" column="name"/>   
  181.                 <result property="address" column="address"/>   
  182.         </resultMap>   
  183.         <!-- 添加 -->   
  184.         <insert id="insert" parameterClass="TabB">   
  185.                 insert into tab_b(   
  186.                 id,   
  187.                 name,   
  188.                 address   
  189.                 ) values (   
  190.                 #id#,   
  191.                 #name#,   
  192.                 #address#   
  193.                 )   
  194.                 <selectKey keyProperty="id" resultClass="long">   
  195.                         select LAST_INSERT_ID()   
  196.                 </selectKey>   
  197.         </insert>   
  198.         <!-- 更新 -->   
  199.         <update id="update" parameterClass="TabB">   
  200.                 update tab_b set   
  201.                 id = #id#,   
  202.                 name = #name#,   
  203.                 address = #address#   
  204.                 where id = #id#   
  205.         </update>   
  206.         <!-- 删除 -->   
  207.         <delete id="deleteById" parameterClass="long">   
  208.                 delete from tab_b   
  209.                 where id = #value#   
  210.         </delete>   
  211.         <!-- 根据ID获取 -->   
  212.         <select id="findById" parameterClass="long" resultMap="tab_b.result_base">   
  213.                 select *   
  214.                 from tab_b   
  215.                 where id = #value#   
  216.         </select>   
  217. </sqlMap>   
  218. /**  
  219. * TabADAO  
  220.  
  221. * @author leizhimin 2009-6-25 12:39:19  
  222. */   
  223. public interface TabADAO {   
  224.   
  225.         /**  
  226.          * 保存一个TabA对象  
  227.          *  
  228.          * @param tabA TabA对象  
  229.          * @return 返回保存后的对象  
  230.          */   
  231.         TabA saveTabA(TabA tabA);   
  232.   
  233.         /**  
  234.          * 更新一个TabA  
  235.          *  
  236.          * @param tabA TabA对象  
  237.          * @return 返回更新后的对象  
  238.          */   
  239.         TabA updateTabA(TabA tabA);   
  240.   
  241.         /**  
  242.          * 删除指定标识的一个TabA  
  243.          *  
  244.          * @param id TabA标识  
  245.          */   
  246.         void deleteTabAById(Long id);   
  247.   
  248.         /**  
  249.          * 获取指定标识的TabA  
  250.          *  
  251.          * @param id TabA标识  
  252.          * @return 所查询到的TabA  
  253.          */   
  254.         TabA findTabAById(Long id);   
  255. }   
  256. /**  
  257. * TabADAO  
  258.  
  259. * @author leizhimin 2009-6-25 12:43:55  
  260. */   
  261. public class TabADAOImpl extends SqlMapClientDaoSupport implements TabADAO {   
  262.   
  263.         /**  
  264.          * 保存一个TabA对象  
  265.          *  
  266.          * @param tabA TabA对象  
  267.          * @return 返回保存后的对象  
  268.          */   
  269.         public TabA saveTabA(TabA tabA) {   
  270.                 Long id = (Long) getSqlMapClientTemplate().insert("tab_a.insert", tabA);   
  271.                 tabA.setId(id);   
  272.                 return tabA;   
  273.         }   
  274.   
  275.         /**  
  276.          * 更新一个TabA  
  277.          *  
  278.          * @param tabA TabA对象  
  279.          * @return 返回更新后的对象  
  280.          */   
  281.         public TabA updateTabA(TabA tabA) {   
  282.                 getSqlMapClientTemplate().update("tab_a.update", tabA);   
  283.                 return tabA;   
  284.         }   
  285.   
  286.         /**  
  287.          * 删除指定标识的一个TabA  
  288.          *  
  289.          * @param id TabA标识  
  290.          */   
  291.         public void deleteTabAById(Long id) {   
  292.                 getSqlMapClientTemplate().delete("tab_a.deleteById",id);   
  293.         }   
  294.   
  295.         /**  
  296.          * 获取指定标识的TabA  
  297.          *  
  298.          * @param id TabA标识  
  299.          * @return 所查询到的TabA  
  300.          */   
  301.         public TabA findTabAById(Long id) {   
  302.                 return (TabA) getSqlMapClientTemplate().queryForObject("tab_a.findById",id);   
  303.         }   
  304. }   
  305. B的DAO和A类似,就不写了。  
  306.    
  307. /**  
  308. * 测试JOTM的Service  
  309.  
  310. * @author leizhimin 2009-6-25 12:53:55  
  311. */   
  312. public interface StuJotmService {   
  313.         /**  
  314.          * 同时保存TabA、TabB  
  315.          *  
  316.          * @param a TabA对象  
  317.          * @param b TabB对象  
  318.          */   
  319.         void saveAB(TabA a, TabB b);   
  320.   
  321.         /**  
  322.          * 同时更新TabA、TabB  
  323.          *  
  324.          * @param a TabA对象  
  325.          * @param b TabB对象  
  326.          */   
  327.         void updateAB(TabA a, TabB b);   
  328.   
  329.         /**  
  330.          * 删除指定id的TabA、TabB记录  
  331.          *  
  332.          * @param id 指定id  
  333.          */   
  334.         void deleteABif(Long id);   
  335. }   
  336. /**  
  337. * Created by IntelliJ IDEA.  
  338.  
  339. * @author leizhimin 2009-6-25 12:58:48  
  340. */   
  341. //@Transactional   
  342. public class StuJotmServiceImpl implements StuJotmService {   
  343.         private TabADAO tabADAO;   
  344.         private TabBDAO tabBDAO;   
  345.   
  346.         /**  
  347.          * 同时保存TabA、TabB  
  348.          *  
  349.          * @param a TabA对象  
  350.          * @param b TabB对象  
  351.          */   
  352. //        @Transactional(readOnly=false)   
  353.         public void saveAB(TabA a, TabB b) {   
  354.                 tabADAO.saveTabA(a);   
  355.                 tabBDAO.saveTabB(b);   
  356.         }   
  357.   
  358.         /**  
  359.          * 同时更新TabA、TabB  
  360.          *  
  361.          * @param a TabA对象  
  362.          * @param b TabB对象  
  363.          */   
  364. //        @Transactional(readOnly=false)   
  365.         public void updateAB(TabA a, TabB b) {   
  366.                 tabADAO.updateTabA(a);   
  367.                 tabBDAO.updateTabB(b);   
  368.         }   
  369.   
  370.         /**  
  371.          * 删除指定id的TabA、TabB记录  
  372.          *  
  373.          * @param id 指定id  
  374.          */   
  375. //        @Transactional(readOnly=false)   
  376.         public void deleteABif(Long id) {   
  377.                 tabADAO.deleteTabAById(id);   
  378.                 tabBDAO.deleteTabBById(id);   
  379.         }   
  380.   
  381.         public void setTabADAO(TabADAO tabADAO) {   
  382.                 this.tabADAO = tabADAO;   
  383.         }   
  384.   
  385.         public void setTabBDAO(TabBDAO tabBDAO) {   
  386.                 this.tabBDAO = tabBDAO;   
  387.         }   
  388. }   
  389. /**  
  390. * Spring上下文工具  
  391.  
  392. * @author leizhimin 2008-8-13 14:42:58  
  393. */   
  394.   
  395. public class ApplicationContextUtil {   
  396.         private static ApplicationContext applicationContext;   
  397.   
  398.         static {   
  399.                 if (applicationContext == null)   
  400.                         applicationContext = rebuildApplicationContext();   
  401.         }   
  402.   
  403.         /**  
  404.          * 重新构建Spring应用上下文环境  
  405.          *  
  406.          * @return ApplicationContext  
  407.          */   
  408.         public static ApplicationContext rebuildApplicationContext() {   
  409.                 return new ClassPathXmlApplicationContext("/ApplicationContext.xml");   
  410.         }   
  411.   
  412.         /**  
  413.          * 获取Spring应用上下文环境  
  414.          *  
  415.          * @return  
  416.          */   
  417.         public static ApplicationContext getApplicationContext() {   
  418.                 return applicationContext;   
  419.         }   
  420.   
  421.         /**  
  422.          * 简单的上下文环境测试  
  423.          */   
  424.         public static void main(String[] args) {   
  425.                 rebuildApplicationContext();   
  426.                 if (applicationContext == null) {   
  427.                         System.out.println("ApplicationContext is null");   
  428.                 } else {   
  429.                         System.out.println("ApplicationContext is not null!");   
  430.                 }   
  431.         }   
  432. }   
  433. 三、做JTOM、Spring、iBatis、Log4j等配置  
  434.    
  435. JOTM配置:carol.properties  
  436. #JNDI调用协议   
  437. carol.protocols=jrmp   
  438. #不使用CAROL JNDI封装器          
  439. carol.start.jndi=false   
  440. #不启动命名服务器   
  441. carol.start.ns=false   
  442.    
  443. Spring配置:ApplicationContext.xml  
  444. <?xml version="1.0" encoding="UTF-8"?>   
  445. <!-- 局部单元测试使用,不正式发布,不要删除 -->   
  446. <beans xmlns="http://www.springframework.org/schema/beans"   
  447.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  448.              xmlns:jee="http://www.springframework.org/schema/jee"   
  449.              xmlns:aop="http://www.springframework.org/schema/aop"   
  450.              xmlns:tx="http://www.springframework.org/schema/tx"   
  451.              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  452.                      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd   
  453.                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  454.                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
  455.   
  456.         <!--指定Spring配置中用到的属性文件-->   
  457.         <bean id="propertyConfig"   
  458.                     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
  459.                 <property name="locations">   
  460.                         <list>   
  461.                                 <value>classpath:jdbc.properties</value>   
  462.                         </list>   
  463.                 </property>   
  464.         </bean>   
  465.         <!-- JOTM实例 -->   
  466.         <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>   
  467.         <!-- JTA事务管理器 -->   
  468.         <bean id="myJtaManager"   
  469.                     class="org.springframework.transaction.jta.JtaTransactionManager">   
  470.                 <property name="userTransaction">   
  471.                         <ref local="jotm"/>   
  472.                 </property>   
  473.         </bean>   
  474.         <!-- 数据源A -->   
  475.         <bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">   
  476.                 <property name="dataSource">   
  477.                         <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">   
  478.                                 <property name="transactionManager" ref="jotm"/>   
  479.                                 <property name="driverName" value="${jdbc.driver}"/>   
  480.                                 <property name="url" value="${jdbc.url}"/>   
  481.                         </bean>   
  482.                 </property>   
  483.                 <property name="user" value="${jdbc.username}"/>   
  484.                 <property name="password" value="${jdbc.password}"/>   
  485.         </bean>   
  486.         <!-- 数据源B -->   
  487.         <bean id="dataSourceB" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">   
  488.                 <property name="dataSource">   
  489.                         <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">   
  490.                                 <property name="transactionManager" ref="jotm"/>   
  491.                                 <property name="driverName" value="${jdbc2.driver}"/>   
  492.                                 <property name="url" value="${jdbc2.url}"/>   
  493.                         </bean>   
  494.                 </property>   
  495.                 <property name="user" value="${jdbc2.username}"/>   
  496.                 <property name="password" value="${jdbc.password}"/>   
  497.         </bean>   
  498.         <!-- 事务切面配置 -->   
  499.         <aop:config>   
  500.                 <aop:pointcut id="serviceOperation"   
  501.                                             expression="execution(* *..servi1ce*..*(..))"/>   
  502.                 <aop:advisor pointcut-ref="serviceOperation"   
  503.                                          advice-ref="txAdvice"/>   
  504.         </aop:config>   
  505.         <!-- 通知配置 -->   
  506.         <tx:advice id="txAdvice" transaction-manager="myJtaManager">   
  507.                 <tx:attributes>   
  508.                         <tx:method name="delete*" rollback-for="Exception"/>   
  509.                         <tx:method name="save*" rollback-for="Exception"/>   
  510.                         <tx:method name="update*" rollback-for="Exception"/>   
  511.                         <tx:method name="*" read-only="true" rollback-for="Exception"/>   
  512.                 </tx:attributes>   
  513.         </tx:advice>   
  514.   
  515.         <!--根据dataSourceA和sql-map-config_A.xml创建一个SqlMapClientA-->   
  516.         <bean id="sqlMapClientA"   
  517.                     class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
  518.                 <property name="dataSource">   
  519.                         <ref local="dataSourceA"/>   
  520.                 </property>   
  521.                 <property name="configLocation">   
  522.                         <value>sql-map-config_A.xml</value>   
  523.                 </property>   
  524.         </bean>   
  525.         <!--根据dataSourceB和sql-map-config_B.xml创建一个SqlMapClientB-->   
  526.         <bean id="sqlMapClientB"   
  527.                     class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
  528.                 <property name="dataSource">   
  529.                         <ref local="dataSourceB"/>   
  530.                 </property>   
  531.                 <property name="configLocation">   
  532.                         <value>sql-map-config_B.xml</value>   
  533.                 </property>   
  534.         </bean>   
  535.         <!--根据sqlMapClientA创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateA-->   
  536.         <bean id="sqlMapClientTemplateA"   
  537.                     class="org.springframework.orm.ibatis.SqlMapClientTemplate">   
  538.                 <property name="sqlMapClient" ref="sqlMapClientA"/>   
  539.         </bean>   
  540.         <!--根据sqlMapClientB创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateB-->   
  541.         <bean id="sqlMapClientTemplateB"   
  542.                     class="org.springframework.orm.ibatis.SqlMapClientTemplate">   
  543.                 <property name="sqlMapClient" ref="sqlMapClientB"/>   
  544.         </bean>   
  545.   
  546.         <!-- 配置DAO,并注入所使用的sqlMapClientTemplate实例 -->   
  547.         <bean id="tabADAO" class="com.lavasoft.stu.jtom.dao.impl.TabADAOImpl">   
  548.                 <property name="sqlMapClientTemplate" ref="sqlMapClientTemplateA"/>   
  549.         </bean>   
  550.         <bean id="tabBDAO" class="com.lavasoft.stu.jtom.dao.impl.TabBDAOImpl">   
  551.                 <property name="sqlMapClientTemplate" ref="sqlMapClientTemplateB"/>   
  552.         </bean>   
  553.   
  554.         <!-- Service配置,注入DAO -->   
  555.         <bean id="stuJotmService" class="com.lavasoft.stu.jtom.service.StuJotmServiceImpl">   
  556.                 <property name="tabADAO" ref="tabADAO"/>   
  557.                 <property name="tabBDAO" ref="tabBDAO"/>   
  558.         </bean>   
  559. </beans>   
  560. 数据库配置:jdbc.properties  
  561. jdbc.database=cms_release   
  562. jdbc.driver=com.mysql.jdbc.Driver   
  563. jdbc.url=jdbc:mysql://192.168.0.2:3306/testdb_a?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull   
  564. jdbc.username=root   
  565. jdbc.password=leizhimin   
  566.   
  567. jdbc2.database=cms_release   
  568. jdbc2.driver=com.mysql.jdbc.Driver   
  569. jdbc2.url=jdbc:mysql://192.168.0.1:3306/testdb_b?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull   
  570. jdbc2.username=root   
  571. jdbc2.password=leizhimin   
  572.    
  573. iBatis的SQLMap配置:  
  574. sql-map-config_A.xml  
  575. <?xml version="1.0" encoding="UTF-8"?>   
  576. <!DOCTYPE sqlMapConfig   
  577.                 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"   
  578.                 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">   
  579.   
  580. <sqlMapConfig>   
  581.         <settings cacheModelsEnabled="true" enhancementEnabled="true"   
  582.                             lazyLoadingEnabled="true" errorTracingEnabled="true"   
  583.                             useStatementNamespaces="true"/>   
  584.   
  585.         <sqlMap resource="com/lavasoft/stu/jtom/entity/sqlmap/TabA.xml"/>   
  586.   
  587. </sqlMapConfig>   
  588. sql-map-config_B.xml  
  589. <?xml version="1.0" encoding="UTF-8"?>   
  590. <!DOCTYPE sqlMapConfig   
  591.                 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"   
  592.                 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">   
  593.   
  594. <sqlMapConfig>   
  595.         <settings cacheModelsEnabled="true" enhancementEnabled="true"   
  596.                             lazyLoadingEnabled="true" errorTracingEnabled="true"   
  597.                             useStatementNamespaces="true"/>   
  598.   
  599.         <sqlMap resource="com/lavasoft/stu/jtom/entity/sqlmap/TabB.xml"/>   
  600.   
  601. </sqlMapConfig>   
  602. 日志的配置:log4j.properties  
  603. log4j.rootLogger=INFO,CONSOLE,LOGFILE   
  604.   
  605. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender   
  606. log4j.appender.Threshold=INFO   
  607. log4j.appender.CONSOLE.Target=System.out   
  608. log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout   
  609. log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss } - %-5p %c        %x - %m%n   
  610.   
  611. log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender   
  612. log4j.appender.LOGFILE.File=contestlog.log   
  613. log4j.appender.LOGFILE.MaxFileSize=500KB   
  614. log4j.appender.LOGFILE.MaxBackupIndex=10   
  615. log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout   
  616. log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss } -    %-p %c     %x - %m%n   
  617.   
  618. log4j.logger.com.ibatis=INFO   
  619. log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO   
  620. log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO   
  621. log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO   
  622. log4j.logger.java.sql.Connection=debug   
  623. log4j.logger.java.sql.Statement=INFO,CONSOLE   
  624. log4j.logger.java.sql.PreparedStatement=INFO,CONSOLE   
  625.    
  626. 四、测试  
  627.    
  628. public class Test {   
  629.         private static ApplicationContext ctx = ApplicationContextUtil.getApplicationContext();   
  630.         private static StuJotmService ser = (StuJotmService) ctx.getBean("stuJotmService");   
  631.   
  632.         public static void test_() {   
  633.                 TabA a = new TabA();   
  634.                 a.setId(1L);   
  635.                 a.setName("aaa");   
  636.                 a.setAddress("address a");   
  637.                 TabB b = new TabB();   
  638.                 b.setId(1L);   
  639.                 b.setName("bbb");   
  640.                 b.setAddress("address b");   
  641.                 ser.saveAB(a, b);   
  642.         }   
  643.   
  644.         public static void main(String[] args) {   
  645.                 test_();   
  646.         }   
  647. }  
0 0