其他分享
首页 > 其他分享> > @Transactional事务的简单使用

@Transactional事务的简单使用

作者:互联网

@Transactional只有经过经过 Spring 的代理类才能生效,调用类中的方法,事务不会生效,@EnableTransactionManagement不加上事务也可以生效

CREATE TABLE IF NOT EXISTS telephone(
   id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键',
   ocm_cust_id VARCHAR(100) NOT NULL,
   is_default VARCHAR(40) NOT NULL,
   telephone_number VARCHAR(40) NOT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;

SELECT *  FROM  telephone  WHERE ocm_cust_id='ocm123456'  AND  is_default='Y'

INSERT INTO telephone(ocm_cust_id,is_default,telephone_number)
VALUE
('ocm123456','Y','13301455191'),
('ocm123456','N','18852782003')

UPDATE telephone SET is_default='Y' 
WHERE ocm_cust_id='ocm123456' AND telephone_number='13301455191'

  

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
<!--		<version>2.3.4.RELEASE</version>-->
		<relativePath/>
	</parent>
	<groupId>com.java</groupId>
	<artifactId>pingan</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>pingan</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<skipTests>true</skipTests>
	</properties>

	<dependencies>
		<!--移除tomcat容器-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--加入undertow-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-undertow</artifactId>
		</dependency>
        <!--实体类字段校验依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<!--引入thymeleaf依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			<version>2.2.2.RELEASE</version>
		</dependency>
		<!--springboot整合mybatis-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.2</version>
		</dependency>
		<!-- mybatis pagehelper 分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.10</version>
		</dependency>
		<!--springboot整合mongodb-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>3.0.4</version>
		</dependency>
		<!--引入shiro依赖-->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>1.4.0</version>
		</dependency>
		<!--添加fastjson依赖-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<!--<version>1.2.7</version>-->
			<version>1.2.70</version>
		</dependency>
		<!--mysql驱动5.6.17-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
		</dependency>

		<!--腾讯云短信01-->
		<!-- https://mvnrepository.com/artifact/com.github.qcloudsms/qcloudsms -->
		<dependency>
			<groupId>com.github.qcloudsms</groupId>
			<artifactId>qcloudsms</artifactId>
			<version>1.0.6</version>
		</dependency>
		<!--阿里云短信接口-->
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-core</artifactId>
			<version>4.0.6</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
			<version>1.1.0</version>
		</dependency>
		<!--榛子云-->
		<dependency>
			<groupId>com.zhenzikj</groupId>
			<artifactId>zhenzisms</artifactId>
			<version>1.0.2</version>
		</dependency>
		<!-- 热部署模块 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
		</dependency>
		<!--java爬虫需要的jar包-->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.12.2</version>
		</dependency>
		<!--判断空的用法  -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<!--邮件发送相关依赖三个-->
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.9.4</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
        <!--Spring Session使得基于Redis的Session共享-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

		<!--lombok依赖-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.16</version>
		</dependency>
		<!--拼音的依赖包-->
		<dependency>
			<groupId>com.belerweb</groupId>
			<artifactId>pinyin4j</artifactId>
			<version>2.5.1</version>
		</dependency>
		<!--springboot整合aop-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>

		<!--springboot整合邮件发送-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
          <!--ThreadFactoryBuilder的依赖包-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>

          <!--引入jmockit依赖-->
		<dependency>
			<groupId>org.jmockit</groupId>
			<artifactId>jmockit</artifactId>
			<version>1.38</version>
		</dependency>
         <!--引入junit单元测试依赖-->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<!--引入poi依赖-->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.17</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>
		<!--引入druid依赖-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</version>
		</dependency>
    </dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<finalName>pingan</finalName>
	</build>

</project>

  数据库使用的是5.6.17版本

package com.java.bean;

import lombok.Data;

/**
 * @author yourheart
 * @Description
 * @create 2022-05-10 23:39
 */
@Data
public class Telephone {

    private Integer id;

    private String ocmCustId;

    private String isDefault;

    private String telephoneNumber;
}

  

package com.java.mapper.zero;

import com.java.bean.Telephone;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface TelephoneMapper {

    /**
     * 修改默认手机号标识
     * @param telephone
     * @return
     */
    @Update("UPDATE telephone SET is_default=#{isDefault} WHERE ocm_cust_id=#{ocmCustId} AND telephone_number=#{telephoneNumber}")
    int updateTelphone(Telephone telephone);

    /**
     * 查询以前的默认手机号信息
     * @param telephone
     * @return
     */
    @Select("SELECT *  FROM  telephone  WHERE ocm_cust_id=#{ocmCustId}  AND  is_default=#{isDefault}")
    Telephone getTelphoneDto(Telephone telephone);
}

  

package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
//开启定时任务支持
@EnableAsync()
//设置redis共享
@EnableRedisHttpSession
//开启事务支持
@EnableTransactionManagement
public class PinganApplication {


    public static void main(String[] args) {
        SpringApplication.run(PinganApplication.class, args);
    }

}


package com.java.service;

import com.java.bean.Telephone;

public interface TelphoneService {

    void  updateTelephone(Telephone telephone);
}

  

package com.java.service.impl;

import com.java.bean.Telephone;
import com.java.service.TelphoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author yourheart
 * @Description
 * @create 2022-05-11 0:27
 */
@Service("TelphoneServiceImpl")
@Slf4j
public class TelphoneServiceImpl implements TelphoneService {

  @Resource(name = "UpdateTelphoneServiceImpl")
   private TelphoneService telphoneService;


    @Override
    public void updateTelephone(Telephone telephone) {
        telphoneService.updateTelephone(telephone);


    }

}

  

package com.java.service.impl;

import com.java.bean.Telephone;
import com.java.mapper.zero.TelephoneMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author yourheart
 * @Description
 * @create 2022-05-11 0:36
 */
@Service("UpdateTelphoneServiceImpl")
@Slf4j
public class UpdateTelphoneServiceImpl extends TelphoneServiceImpl {

    @Autowired
    private TelephoneMapper telephoneMapper;

    @Transactional
    @Override
    public void updateTelephone(Telephone telephone) {
        //查询出以前的默认手机号信息
        Telephone telphoneDto = telephoneMapper.getTelphoneDto(telephone);
        Telephone one=new Telephone();
        one.setOcmCustId(telphoneDto.getOcmCustId());
        one.setTelephoneNumber(telphoneDto.getTelephoneNumber());
        one.setIsDefault("N");
        log.info("one:{}",one);

        //修改以前默认的手机号标识为N
        telephoneMapper.updateTelphone(one);

        //修改当前接受的手机号为默认手机号
        Telephone two=new Telephone();
        two.setOcmCustId(telephone.getOcmCustId());
        two.setTelephoneNumber(telephone.getTelephoneNumber());
        log.info("two:{}",two);
        telephoneMapper.updateTelphone(two);
    }
}

  单元测试

package com.java;



import com.java.bean.Telephone;
import com.java.service.TelphoneService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class PinganApplicationTests{


    @Resource(name = "TelphoneServiceImpl")
    private TelphoneService telphoneService;




    @Test
    public void moTest(){


        String ocmCustId="";
        String telephoneNumber="";
        String isDefault="";

        Telephone telephone=new Telephone();
        telephone.setOcmCustId(ocmCustId);
        telephone.setTelephoneNumber(telephoneNumber);
        telephone.setIsDefault(isDefault);
        telphoneService.updateTelephone(telephone);


    }

}

  

标签:事务,Transactional,简单,boot,telephone,springframework,import,org,com
来源: https://www.cnblogs.com/q202105271618/p/16256224.html