spring事务控制-基于XML

2021-04-19 06:26

阅读:282

标签:with   pre   default   ali   coding   tac   alt   inter   账户   

技术图片

 

 

xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>

    groupId>org.examplegroupId>
    artifactId>transactionartifactId>
    version>1.0-SNAPSHOTversion>

    dependencies>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-contextartifactId>
            version>5.2.7.RELEASEversion>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jdbcartifactId>
            version>5.2.7.RELEASEversion>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-txartifactId>
            version>5.2.7.RELEASEversion>
        dependency>
        dependency>
            groupId>org.aspectjgroupId>
            artifactId>aspectjweaverartifactId>
            version>1.9.5version>
        dependency>
        dependency>
            groupId>com.h2databasegroupId>
            artifactId>h2artifactId>
            version>1.4.200version>
        dependency>
        dependency>
            groupId>de.lars-shgroupId>
            artifactId>lombok-annotationsartifactId>
            version>1.18.12version>
        dependency>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.12version>
            scope>testscope>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-testartifactId>
            version>5.2.7.RELEASEversion>
        dependency>
    dependencies>
    properties>
        project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        java.version>14java.version>
        maven.compiler.source>14maven.compiler.source>
        maven.compiler.target>14maven.compiler.target>
        encoding>UTF-8encoding>
    properties>
    repositories>
        repository>
            id>alimavenid>
            name>aliyun mavenname>
            url>http://maven.aliyun.com/nexus/content/groups/public/url>
            releases>
                enabled>trueenabled>
            releases>
            snapshots>
                enabled>falseenabled>
            snapshots>
        repository>
    repositories>
project>
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">

    bean id="accountService" class="com.example.service.impl.AccountServiceImpl">
        property name="accountDao" ref="accountDao">property>
    bean>
    bean id="accountDao" class="com.example.dao.impl.AccountDaoImpl">
        property name="jdbcTemplate" ref="jdbcTemplate">property>
    bean>
    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="org.h2.Driver">property>
        property name="url" value="jdbc:h2:file:~/.h2/h2">property>
        property name="username" value="root">property>
        property name="password" value="123456">property>
    bean>

    bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource">property>
    bean>

    tx:advice id="txAdvice" transaction-manager="transactionManager">

        tx:attributes>
            tx:method name="transfer" />

        
        tx:attributes>
    tx:advice>


    aop:config>
        aop:pointcut id="pt1" expression="execution(* com.example.service.impl.AccountServiceImpl.*(..))">aop:pointcut>
        aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">aop:advisor>
     aop:config>
beans>
package com.example.service;

import com.example.domain.Account;

public interface IAccountService {
    Account findAccountById(int id);
    Account findAccountByName(String name);
    void updateAccount(Account account);
    void transfer(String sourceName,String targetName,float money);
}
package com.example.service.impl;

import com.example.dao.IAccountDao;
import com.example.domain.Account;
import com.example.service.IAccountService;

public class AccountServiceImpl implements IAccountService {


    private  IAccountDao accountDao;

    public IAccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public Account findAccountById(int id) {
        return accountDao.findAccountById(id);
    }

    @Override
    public Account findAccountByName(String name) {
        return accountDao.findAccountByName(name);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void transfer(String sourceName, String targetName, float money) {
        Account source = accountDao.findAccountByName(sourceName);
        Account target = accountDao.findAccountByName(targetName);
        source.setMoney(source.getMoney()-money);
        target.setMoney(target.getMoney()+money);
        accountDao.updateAccount(source);
        accountDao.updateAccount(target);
        int i=1/0;
    }
}
package com.example.domain;






public class Account {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", money=" + money +
                ‘}‘;
    }

    private String name;
    private float money;
}
package com.example.dao;

import com.example.domain.Account;

public interface IAccountDao {
    Account findAccountById(int id);
    Account findAccountByName(String name);
    void updateAccount(Account account);
}
package com.example.dao.impl;

import com.example.dao.IAccountDao;
import com.example.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AccountDaoImpl implements IAccountDao {
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private JdbcTemplate jdbcTemplate;
    @Override
    public Account findAccountById(int id) {
        List accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper(Account.class),id);
        if(accounts.size()==0){
            return null;
        }else{
            return accounts.get(0);
        }
    }

    @Override
    public Account findAccountByName(String name) {
        List accounts = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper(Account.class),name);
        if(accounts.size()==0){
            return null;
        }else if(accounts.size()==1){
            return accounts.get(0);
        }else {
            throw new RuntimeException("账户不唯一");
        }
    }


    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set name = ? , money = ? where id =?",account.getName(),account.getMoney(),account.getId());
    }
}

测试

package com.example;

import com.example.domain.Account;
import com.example.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class AccountSerciveTest {

    @Autowired
    private IAccountService service;
    @Test
    public void testTransfer(){
        Account a;
        Account b;
        a = service.findAccountByName("account1");
        b = service.findAccountByName("account3");
        System.out.println(a);
        System.out.println(b);
        service.transfer("account1","account3",100);
        a = service.findAccountByName("account1");
        b = service.findAccountByName("account3");
        System.out.println(a);
        System.out.println(b);
    }
}

 

spring事务控制-基于XML

标签:with   pre   default   ali   coding   tac   alt   inter   账户   

原文地址:https://www.cnblogs.com/abuduri/p/13290065.html


评论


亲,登录后才可以留言!