基于JDBCTemple的声明式事务配置

1.导入相关依赖包

<dependencies>
        <dependency>
            <!-- spring核心依赖包-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.8</version>
        </dependency>
        <!-- 声明式事务依赖包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.8</version>
        </dependency>
        <!-- spring-jdbc 事务的实现类-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.8</version>
        </dependency>
        <!-- 配置aop-->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.5.4</version>
        </dependency>

        <!-- 数据库驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

    </dependencies>

2.配置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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 扫描accountDaoImpl类-->
    <bean id="accountDaoImpl" class="com.xie.dao.impl.AccountDaoImpl">
        <property name="template" ref="jdbcTemplate"></property>
    </bean>
    <bean id="accountService" class="com.xie.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDaoImpl"></property>
    </bean>

    <!-- 配置jdbc-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
     </bean>

    <!-- 配置数据库源-->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/briger"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!-- 声明式事务的配置-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启声明事务-->
    <tx:advice id="myAdvice" transaction-manager="txManager">
<!-- 事务属性配置-->
        <tx:attributes>
            <!-- method name 使用通配符 * 如果式增删改dml语句,必须read-only等于false 否则事务失败-->
            <tx:method name="transfar" read-only="false"/>
            <tx:method name="find*" read-only="true" ></tx:method>
        </tx:attributes>
    </tx:advice>

    <!-- 配置切面-->
    <aop:config>
        <aop:pointcut id="ptu" expression="execution(* com.xie.service..*(..))"/>
    </aop:config>
   <aop:config>
       <aop:advisor advice-ref="myAdvice" pointcut-ref="ptu"></aop:advisor>
   </aop:config>
</beans>

3.编写dao类

@Data   //表示set和get
public class AccountDaoImpl implements AccountDao {
      private JdbcTemplate template;

    @Override
    public void sav(Account account) {
        template.update("insert into account(`name`,money) values (?,?)",account.getName(),account.getMoney());

    }

    @Override
    public void add(Account account) {

        template.update("update account set money=?, name=? where id=?", account.getMoney(), account.getName(), account.getId());
    }

    @Override
    public Account findByname(String name) throws Exception {
        List<Map<String, Object>> accounts = template.queryForList("select id, `name`, money from account where `name`=?", name);

        if (accounts.size()==0){
            return  null;
        }
        if (accounts.size() >1 ){
            throw new Exception("账号不唯一,有多个");
        }
        Account account = new Account();
        account.setName(accounts.get(0).get("name").toString());
        account.setMoney(Double.valueOf(accounts.get(0).get("money").toString()) );
        account.setId(Integer.valueOf(accounts.get(0).get("id").toString()));
        return account;
    }
}

4.service类

@Data
public class AccountServiceImpl implements IAccountService {

    private AccountDao accountDao;
    @Override
    public boolean transfar(String source, String target, double money) throws Exception {
        Account a=accountDao.findByname(source);
        Account b=accountDao.findByname(target);
        a.setMoney(a.getMoney()-money);
        b.setMoney(b.getMoney()+money);
        accountDao.add(a);
        int i=1/0;  //发生异常,进行回滚操作
        accountDao.add(b);
        return true;
    }
}

测试类

public class TxMain {
    public static void main(String[] args) throws Exception {

       ApplicationContext context = new ClassPathXmlApplicationContext("tx.xml");

        IAccountService iAccountService = context.getBean("accountService", IAccountService.class);

        iAccountService.transfar("aaa","bbb",200d);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值