基于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);
    }
}

上一篇:SpringBoot day2 SpringBoot+MyBatis实现增删改查操作(注解实现)


下一篇:JavaWeb技术内幕八:JVM内存管理