spring的声明式事务管理

一、xml形式具体实现步骤

1.准备jar包

aopalliance-1.0.jar
aspectjrt-1.9.4.jar
aspectjweaver-1.9.4.jar
commons-dbcp2-2.1.1.jar
commons-logging-1.2.jar
commons-pool2-2.4.2.jar
mybatis-3.4.6.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-8.0.15.jar
spring-2.5.6.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-5.2.4.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar

2.编写实体类

//省略import
public class Account {
	private Integer a_id;
	private String a_name;
    //省略getter setter方法
}

3.编写AccountDao文件 原子层

//省略import
public interface AccountDao {
   List<Account> getAccountList();
   int insertAccount(Account account);
}

4.编写AccountDao.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.gdkm.dao.AccountDao">

    <select id="getAccountList" resultType="edu.gdkm.pojo.Account" >
        select * from account
    </select>

    <insert id="insertAccount" parameterType="edu.gdkm.pojo.Account">
        insert into account (a_id,a_name) values (#{a_id},#{a_name});
    </insert>
    
</mapper>

5.编写服务类

public interface AccountService {
    int insertAccount(Account account) throws Exception;
}

6.编写服务类实现类

@Service
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public int insertAccount(Account account) {
        accountDao.insertAccount(new Account(35, "35"));
        int c = 10 / 0;
        return accountDao.insertAccount(account);
    }
}

7.编写applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>
                    db.properties
                </value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">

        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
	
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
	
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
   
    <aop:config>
        <aop:pointcut expression="execution(* edu.gdkm.service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

    <bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="edu.gdkm.dao.AccountDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    <bean id="accountService" class="edu.gdkm.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

</beans>

8.编写db.properties文件

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=root

9.编写ServiceTest测试类

public class ServiceTest {
    public static void main(String[] args) throws Exception {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) act.getBean("accountService");
        int row = accountService.insertAccount(new Account(33, "33"));
        System.out.println(row);
    }
}

二、注解形式具体实现步骤

1.将以上的AccountServiceImpl实现类修改

@Transactional(readOnly = false, rollbackFor = Exception.class)
@Override
public int insertAccount(Account account) {
    int row = accountDao.insertAccount(new Account(3, "3"));
    System.out.println(row);
    int c=10/0;
    System.out.println(c);
    return accountDao.insertAccount(account);
}

2.将以上的applicationContext.xml修改

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>
                    db.properties
                </value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">

        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

    </bean>

    <!--配置mybatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--  配置事务管理器 txManager -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 增加对事务的支持 核心-->
	<tx:annotation-driven transaction-manager="txManager"  />

    <bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="edu.gdkm.dao.AccountDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    <!--注入服务bean-->
    <bean id="accountService" class="edu.gdkm.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

</beans>

编写ServiceTest测试类

public class ServiceTest {
    public static void main(String[] args) throws Exception {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) act.getBean("accountService");
        int row = accountService.insertAccount(new Account(33, "33"));
        System.out.println(row);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值