`
gzcj
  • 浏览: 286409 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 实现ibatis事务回滚

阅读更多

网上发现这篇文章,转过来学习一下,好不好用还没有实践,需要自己检验一下。

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

    <listener>

       <listener-class>

           org.springframework.web.context.ContextLoaderListener

       </listener-class>

    </listener>

 

    <servlet>

       <servlet-name>AppConfigServlet</servlet-name>

       <servlet-class>com.test.ConfigServlet</servlet-class>

       <load-on-startup>2</load-on-startup>

    </servlet>

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

 ApplicationContext.xml

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource" />

    </bean>

<bean id="businessTarget"

       class="com.test.BusinessInterfaceImpl" />

       

    <bean id="businessBean"

       class="org.springframework.aop.framework.ProxyFactoryBean">

       <property name="singleton">

           <value>true</value>

       </property>

       <property name="proxyInterfaces">

           <value>com.test.BusinessInterface</value>

       </property>

       <property name="interceptorNames">

           <list>

              <value>transactionInterceptor</value>

              <value>businessTarget</value>

           </list>

       </property>

       <property name="proxyTargetClass"><value>true</value></property>

    </bean>

    

    <bean id="transactionInterceptor"

       class="org.springframework.transaction.interceptor.TransactionInterceptor">

       <property name="transactionManager">

           <ref local="transactionManager" />

       </property>

       <property name="transactionAttributes">

           <props>

              <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>

           </props>

       </property>

    </bean>

    <bean id="dataSource"

       class="org.springframework.jndi.JndiObjectFactoryBean">

       <property name="jndiName">

           <value>gissys</value>

       </property>

    </bean>

<bean id="sqlMapClient"

       class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

       <property name="configLocation">

           <value>

              classpath:com/emaptech/persistence/SqlMapConfig.xml

           </value>

       </property>

       <property name="dataSource" ref="dataSource"></property>

    </bean>

 Ibatis.xml

<insert id="testInsert" parameterClass="string">

       insert into corebase.cc select 6,#value# from dual

    </insert>

    <insert id="testInsertWrong" parameterClass="string">

       insert into corebase.cc select 6,#value#,7 from dual

    </insert>

 ConfigServlet.java

public class ConfigServlet extends HttpServlet {

    /**

     * UID

     */

    private static final long serialVersionUID = 5118794568550751611L;

    public void init() throws ServletException {

       ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

       BaseService.getInstance().init(ctx);

       super.init();

    }

}

 BaseService.java

public class BaseService implements ApplicationContextAware {

 

    private static ApplicationContext ctx;

    private static SqlMapClient sqlMapClient;

    private static SqlMapClient sqlMapClientProxy;

    private static BusinessInterface businessBean;
 

    private static BaseService instance = new BaseService();
   

    /**

     * @return 返回 静态类

     */

    public static BaseService getInstance(){

       return instance;

    }


    /**

     * @return 返回 初始化BaseService,存于内存

     */

    public void init(ApplicationContext ctx) {

       setApplicationContext(ctx);

       setSqlMapClient(ctx);

       setBusinessBean(ctx);

//     setSqlMapClientProxy(ctx);

       System.out.println(" INFO - 初始化baseservice成功");

    }

    
    public void setSqlMapClient(ApplicationContext ctx){

       sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");

    }


    /**

     * @return 返回 sqlMapClient。

     */

    public SqlMapClient getInstanceSqlMapClient() {

       return this.sqlMapClient;

    }

    
    /**

     * 通过spring注入ApplicationContext

     * 

     * @param ApplicationContext

     * @return null

     */

    public void setApplicationContext(ApplicationContext arg0) throws BeansException {

       ctx = arg0;

    }
 

    public Object getBean(String beanName) {

       return ctx.getBean(beanName);

    }


    /**

     * @return 返回 sqlMapClient。

     */

    public SqlMapClient getSqlMapClient() {

       return (SqlMapClient) ctx.getBean("sqlMapClient");

    }


    public void setSqlMapClientProxy(ApplicationContext ctx) {

       sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");

    }

    public SqlMapClient getSqlMapClientProxy() {

       return sqlMapClientProxy;

    }


    public static BusinessInterface getBusinessBean() {

       return businessBean;

    } 

    public static void setBusinessBean(ApplicationContext ctx) {

       businessBean = (BusinessInterface) ctx.getBean("businessBean");

    }

}

 BusinessInterface.java

 

public interface BusinessInterface {

    public void hello() throws SQLException;

}

 

BusinessInterfaceImpl.java

public class BusinessInterfaceImpl implements BusinessInterface {

    public void hello() throws SQLException {

       System.out.println("hello Spring AOP.");

       SqlMapClient sqlMapClient = BaseService.getInstance().getInstanceSqlMapClient();

    

           sqlMapClient.startTransaction();

           sqlMapClient.insert("testInsert","7");

           System.out.println("1");

           sqlMapClient.insert("testInsert","8");

           System.out.println("2");

           sqlMapClient.insert("testInsert","9");

           System.out.println("3");

           sqlMapClient.insert("testInsertWrong","10");

           sqlMapClient.commitTransaction();

       

    }

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics