其他分享
首页 > 其他分享> > seata 分布式事务 -- seata-three工程完整代码

seata 分布式事务 -- seata-three工程完整代码

作者:互联网

 

工程结构 ,启动类 ,数据库主键生成工具类(雪花算法) 跟 seata-one 一致

 

入口  controller:

 

package com..controller;

import com..service.Rm_Three_Interface;
import com..service.Rm_Three_Service;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Rm_Three_Controller {


@Autowired
private Rm_Three_Service rm_three_service;

@Autowired
private Rm_Three_Interface rm_three_interface;


@RequestMapping("/three_at")
public String rm3(){

String result = rm_three_service.rm3();
System.out.println(1/0);
if(result.equals("err")){
return "err";
}
return "success_"+result;

}


@RequestMapping("/three_tcc")
@GlobalTransactional(rollbackFor = Exception.class)
public String oneTcc() throws InterruptedException {
rm_three_interface.rm3(null);
return "success";
}



}
 

 

  

数据库实体类:

 

package com..entity;


public class TblthreeInfo {

private String id;


private String name;


public String getId() {
return id;
}


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


public String getName() {
return name;
}


public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}

 

 

mapper:

package com..mapper;

import com..entity.TblthreeInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value = "TblthreeInfoMapper")
public interface TblthreeInfoMapper {

int deleteByPrimaryKey(String id);


int insert(TblthreeInfo record);


TblthreeInfo selectByPrimaryKey(String id);


List<TblthreeInfo> selectAll();


int updateByPrimaryKey(TblthreeInfo record);
}

 

AT 模式 实现类:

package com..service;

import com..entity.TblthreeInfo;
import com..mapper.TblthreeInfoMapper;
import com..sqlToJava.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Rm_Three_Service {

@Autowired
TblthreeInfoMapper tblthreeInfoMapper;

public String rm3(){

long id = SnowFlake.nextId();
TblthreeInfo tblthreeInfo = new TblthreeInfo();
tblthreeInfo.setId(id+"");
tblthreeInfo.setName("Rm_Three_"+id);

int insert = tblthreeInfoMapper.insert(tblthreeInfo);

if (insert==1){
return id+"";
}
return "err";
}

}

 

TCC模式 接口 / 实现类:

接口:

package com..service;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

@LocalTCC
public interface Rm_Three_Interface {

@TwoPhaseBusinessAction(name = "rm3TccAction",commitMethod = "rm3Commit",rollbackMethod = "rm3Rollback")
public String rm3(BusinessActionContext businessActionContext);

public boolean rm3Commit(BusinessActionContext businessActionContext);

public boolean rm3Rollback(BusinessActionContext businessActionContext);


}


实现类:

package com..service;

import com..entity.TblthreeInfo;
import com..mapper.TblthreeInfoMapper;
import com..sqlToJava.SnowFlake;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class Rm_Three_InterfaceImpl implements Rm_Three_Interface {

@Autowired
TblthreeInfoMapper tblthreeInfoMapper;

private static ConcurrentMap<String,String> maps = new ConcurrentHashMap<>();

@Override
@Transactional
public String rm3(BusinessActionContext businessActionContext) {

long id = SnowFlake.nextId();
TblthreeInfo tblthreeInfo = new TblthreeInfo();
tblthreeInfo.setId(id+"");
tblthreeInfo.setName("Rm_Three_"+id);
maps.put("id",id+"");
int insert = tblthreeInfoMapper.insert(tblthreeInfo);

System.out.println("rm3 try....");

// System.out.println(1/0);
return null;
}

@Override
@Transactional
public boolean rm3Commit(BusinessActionContext businessActionContext) {

System.out.println("rm3 rm3Commit....");
return true;
}

@Override
@Transactional
public boolean rm3Rollback(BusinessActionContext businessActionContext) {

String id = maps.get("id");
int i = tblthreeInfoMapper.deleteByPrimaryKey(id);
System.out.println("rm3 rm3Rollback...."+i);
return true;
}
}

 

application.yml  :


server:
port: 8090

#应用名称及验证账号
spring:
application:
name: seata-three


datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/seata-rm-three?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
dbcp2:
initial-size: 5
min-idle: 5
max-total: 5
max-wait-millis: 200
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false

mybatis:
mapper-locations:
- classpath:mapper/*.xml

eureka:
client:
prefer-ip-address: true
service-url:
defaultZone: http://localhost:7900/eureka/

 

数据库链接 TbloneInfoMapper.xml:

(项目地址需要根据情况补充)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com..mapper.TblthreeInfoMapper">
<resultMap id="BaseResultMap" type="com..entity.TblthreeInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
delete from tbl_three
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com..entity.TblthreeInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
insert into tbl_three (id, name)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com..entity.TblthreeInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
update tbl_three
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
select id, name
from tbl_three
where id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon May 17 11:25:15 CST 2021.
-->
select id, name
from tbl_three
</select>
</mapper>

 

pom:

(<groupId>com.</groupId> 需要根据情况补充 )

<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.</groupId>
<artifactId>seata-three</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>seata-three</name>
<description>seata-three</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- euekea 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- mysql:MyBatis相关依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

<!-- 整合MyBatis java类依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<type>maven-plugin</type>
</dependency>

<!-- mysql:mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- mysql:阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- JSONObject -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

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

</project>

 

标签:seata,--,three,generated,org,import,com,id
来源: https://www.cnblogs.com/lifan12589/p/14781696.html