我是靠谱客的博主 失眠柚子,最近开发中收集的这篇文章主要介绍注解式事务解决方案,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

更多内容,前往IT-BLOG

一、添加事务配置文件:applicationContext-tx.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


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

    <!-- 开启事务控制的注解支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

二、在 web.xml 中引入 applicationContext-tx.xml 配置文件


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

三、在方法上添加注解:@Transactional 注解

注解可以添加在方法上,表示当前方法具有事务,如果注解添加在类上表上该类的所有方法都具有事务


@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{
........
}

四、扩展-----也可以通过 java类来替代 xml配置文件,如下:


/**
 * Created by Administrator on 2019/11/20 0020.
 * 导入相关依赖:数据源,数据库驱动,spring-jdbc模块
 * 配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据
 */

@ComponentScan("com.spring")
@Configuration
@EnableTransactionManagement
public class TxConfig {
    //数据源
    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("123");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
        DataSource dataSource = dataSource();
        return new DataSourceTransactionManager(dataSource);
    }
}

最后

以上就是失眠柚子为你收集整理的注解式事务解决方案的全部内容,希望文章能够帮你解决注解式事务解决方案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部