关闭

Spring实现iBATIS事务回滚

619人阅读 评论(0) 收藏 举报

配置如下:

Spring实现iBATIS事务回滚之Web.xml

  1. ﹤?xml version="1.0" encoding="UTF-8"?﹥
  2. ﹤web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  5.      ﹤context-param﹥
  6.         ﹤param-name﹥contextConfigLocation﹤/param-name﹥
  7.         ﹤param-value﹥classpath:applicationContext.xml﹤/param-value﹥
  8.      ﹤/context-param﹥
  9.      ﹤listener﹥
  10.         ﹤listener-class﹥
  11.             org.springframework.web.context.ContextLoaderListener
  12.         ﹤/listener-class﹥
  13.      ﹤/listener﹥
  14.      ﹤servlet﹥
  15.         ﹤servlet-name﹥AppConfigServlet﹤/servlet-name﹥
  16.         ﹤servlet-class﹥com.test.ConfigServlet﹤/servlet-class﹥
  17.         ﹤load-on-startup﹥2﹤/load-on-startup﹥
  18.      ﹤/servlet﹥
  19.      ﹤welcome-file-list﹥
  20.         ﹤welcome-file﹥index.jsp﹤/welcome-file﹥
  21.      ﹤/welcome-file-list﹥
  22. ﹤/web-app﹥

Spring实现iBATIS事务回滚之ApplicationContext.xml

  1. ﹤bean id="transactionManager"
  2. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
  3.         ﹤property name="dataSource" ref="dataSource" /﹥
  4.      ﹤/bean﹥
  5. ﹤bean id="businessTarget"
  6.        class="com.test.BusinessInterfaceImpl" /﹥
  7.        
  8.      ﹤bean id="businessBean"
  9.        class="org.springframework.aop.framework.ProxyFactoryBean"
  10.         ﹤property name="singleton"
  11.             ﹤value﹥true﹤/value﹥
  12.         ﹤/property﹥
  13.         ﹤property name="proxyInterfaces"
  14.             ﹤value﹥com.test.BusinessInterface﹤/value﹥
  15.         ﹤/property﹥
  16.         ﹤property name="interceptorNames"
  17.             ﹤list﹥
  18.                ﹤value﹥transactionInterceptor﹤/value﹥
  19.                ﹤value﹥businessTarget﹤/value﹥
  20.             ﹤/list﹥
  21.         ﹤/property﹥
  22.         ﹤property name="proxyTargetClass"﹥﹤value﹥true﹤/value﹥﹤/property﹥
  23.      ﹤/bean﹥
  24.     
  25.      ﹤bean id="transactionInterceptor"
  26.        class="org.springframework.transaction.interceptor.TransactionInterceptor"
  27.         ﹤property name="transactionManager"
  28.             ﹤ref local="transactionManager" /﹥
  29.         ﹤/property﹥
  30.         ﹤property name="transactionAttributes"
  31.             ﹤props﹥
  32.                ﹤prop key="*"﹥PROPAGATION_REQUIRED,-Exception﹤/prop﹥
  33.             ﹤/props﹥
  34.         ﹤/property﹥
  35.      ﹤/bean﹥
  36.      ﹤bean id="dataSource"
  37.        class="org.springframework.jndi.JndiObjectFactoryBean"
  38.         ﹤property name="jndiName"
  39.             ﹤value﹥gissys﹤/value﹥
  40.         ﹤/property﹥
  41.      ﹤/bean﹥
  42. ﹤bean id="sqlMapClient"
  43.        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
  44.         ﹤property name="configLocation"
  45.             ﹤value﹥
  46.                classpath:com/emaptech/persistence/SqlMapConfig.xml
  47.             ﹤/value﹥
  48.         ﹤/property﹥
  49.         ﹤property name="dataSource" ref="dataSource"﹥﹤/property﹥
  50.      ﹤/bean﹥

Spring实现iBATIS事务回滚之Ibatis.xml

  1. ﹤insert id="testInsert" parameterClass="string"
  2.     insert into corebase.cc select 6,#value# from dual
  3. ﹤/insert﹥
  4. ﹤insert id="testInsertWrong" parameterClass="string"
  5.     insert into corebase.cc select 6,#value#,7 from dual
  6. ﹤/insert﹥

Spring实现iBATIS事务回滚之ConfigServlet.java

  1. package com.test;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.web.context.support.WebApplicationContextUtils;
  6. public class ConfigServlet extends HttpServlet {
  7.     /**
  8.       * UID
  9.       */
  10.     private static final long serialVersionUID = 5118794568550751611L;
  11.     public void init() throws ServletException {
  12.         ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
  13.         BaseService.getInstance().init(ctx);
  14.        super.init();
  15.      }
  16. }

Spring实现iBATIS事务回滚之BaseService.java

  1. package com.test;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.ApplicationContextAware;
  6. import org.springframework.core.io.ClassPathResource;
  7. import com.ibatis.sqlmap.client.SqlMapClient;
  8. public class BaseService implements ApplicationContextAware {
  9.     private static ApplicationContext ctx;
  10.     private static SqlMapClient sqlMapClient;
  11.     private static SqlMapClient sqlMapClientProxy;
  12.     private static BusinessInterface businessBean;
  13.     
  14.     private static BaseService instance = new BaseService();
  15.   
  16.     /**
  17.       * @return 返回 静态类
  18.       */
  19.     public static BaseService getInstance(){
  20.        return instance;
  21.      }
  22.     /**
  23.       * @return 返回 初始化BaseService,存于内存
  24.       */
  25.     public void init(ApplicationContext ctx) {
  26.         setApplicationContext(ctx);
  27.         setSqlMapClient(ctx);
  28.         setBusinessBean(ctx);
  29. //      setSqlMapClientProxy(ctx);
  30.         System.out.println(" INFO - 初始化baseservice成功");
  31.      }
  32.     public void setSqlMapClient(ApplicationContext ctx){
  33.         sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
  34.      }    
  35.     /**
  36.       * @return 返回 sqlMapClient。
  37.       */
  38.     public SqlMapClient getInstanceSqlMapClient() {
  39.       return this.sqlMapClient;
  40.      }
  41.     /**
  42.       * 通过spring注入ApplicationContext
  43.       *
  44.       * @param ApplicationContext
  45.       * @return null
  46.       */
  47.     public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  48.         ctx = arg0;
  49.      }
  50.     public Object getBean(String beanName) {
  51.        return ctx.getBean(beanName);
  52.      }
  53.     /**
  54.       * @return 返回 sqlMapClient。
  55.       */
  56.     public SqlMapClient getSqlMapClient() {
  57.        return (SqlMapClient) ctx.getBean("sqlMapClient");
  58.      }
  59.     public void setSqlMapClientProxy(ApplicationContext ctx) {
  60.         sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");
  61.      }
  62.     public SqlMapClient getSqlMapClientProxy() {
  63.        return sqlMapClientProxy;
  64.      }
  65.     public static BusinessInterface getBusinessBean() {
  66.        return businessBean;
  67.      }
  68.     public static void setBusinessBean(ApplicationContext ctx) {
  69.         businessBean = (BusinessInterface) ctx.getBean("businessBean");
  70.      }
  71. }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10168次
    • 积分:245
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:18篇
    • 译文:0篇
    • 评论:3条
    最新评论