Reprint: https://mp.weixin.qq.com/s/mKeoCnV9X0YFvX7LpCUhJQ
CREATE DATABASE /*!32312 IF NOT EXISTS*/`bank1` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `bank1`; /*Table structure for table `account_info` */ DROP TABLE IF EXISTS `account_info`; CREATE TABLE `account_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `account_name` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'Householder's name' , `account_no` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'Bank card number' , `account_password` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'account password' ,`account_balance` double DEFAULT NULL COMMENT 'account balance' , PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC; /*Data for the table `account_info` */ insert into `account_info`(`id`,`account_name`,`account_no` ,`account_password`,`account_balance`) values(2,'Zhang San','1',NULL,1000);
CREATE DATABASE /*!32312 IF NOT EXISTS*/`bank2` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `bank2`; /*Table structure for table `account_info` */ DROP TABLE IF EXISTS `account_info`; CREATE TABLE `account_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `account_name` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'Householder's name' , `account_no` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'Bank card number' , `account_password` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT 'account password' ,`account_balance` double DEFAULT NULL COMMENT 'account balance' , PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC; /*Data for the table `account_info` */ insert into `account_info`(`id`,`account_name`,`account_no` ,`account_password`,`account_balance`) values(3,'李思','2',NULL,0);
Download address: https://github.com/seata/seata/releases
tar -zxvf seata-server-1.4.2.tar.gz -C /usr/local/ #Background process nohup sh seata-server.sh -p 9999 -h 192.168.66.100 -m file & amp;> seata.log & amp;
cat seata.log
Seata provides XA mode to implement distributed transactions_build aggregation parent project construction
Create project distribute-transaction
Java compiled version selection
<!-- Specify JDK version --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compilerplugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
pom configuration version
<properties> <spring-boot.version>2.6.3</spring-boot.version> <spring.cloud.version>2021.0.1</spring.cloud.version> <spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version> <lombok.version>1.18.22</lombok.version> </properties> <dependencyManagement> <dependencies> <!--spring boot 2.6.3--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bootstarter-parent</artifactId> <version>${<!-- -->spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-clouddependencies</artifactId> <version>${<!-- -->spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud Aliabab --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloudalibaba-dependencies</artifactId> <version>${<!-- -->spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${<!-- -->lombok.version}</version> </dependency> </dependencies> </dependencyManagement>
<component name="RunDashboard"> <option name="ruleStates"> <list> <RuleState> <option name="name" value="ConfigurationTypeDashboardGroupingRule"/> </RuleState> <RuleState> <option name="name" value="StatusDashboardGroupingRule" /> </RuleState> </list> </option> <option name="configurationTypes"> <set> <option value="SpringBootApplicationConfigurationType"/> </set> </option> </component>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starterweb</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-bootstarter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connectorjava</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starteralibaba-nacos-discovery</artifactId> </dependency>
Write the main startup class
//Add mapping mapper package scanning Mybatis-plus @MapperScan("com.tong.mapper") @SpringBootApplication @Slf4j //Open discovery registration @EnableDiscoveryClient public class SeataBank2Main6002 {<!-- --> public static void main(String[] args) {<!-- --> SpringApplication.run(SeataBank1Main6002.class,args); log.info("****************** SeataBank1Main6002 *************"); } }
Write YML configuration file
server: port: 6002 spring: application: name: seata-bank2 cloud: nacos: discovery: # Nacos server address server-addr: 192.168.66.101:8848 datasource: url: jdbc:mysql://localhost:3306/bank2?useUnicode=true &characterEncoding=utf-8 &useSSL=false &serverTimezone=UTC username: root password01: 123456 driver-class-name: com.mysql.jdbc.Driver
Code generation
Introducing Mybatis Plus code generation dependencies
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plusgenerator</artifactId> <version>3.5.2</version> </dependency> <!-- Template engine --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-enginecore</artifactId> <version>2.0</version> </dependency>
Generate code
package com.tong.utils; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.Arrays; import java.util.List; public class CodeGenerator {<!-- --> public static void main(String[] args) {<!-- --> FastAutoGenerator.create("jdbc:mysql://192.168.66.100:3306/bank2", "root", "123456") .globalConfig(builder -> {<!-- --> builder.author("itxiaotong")// Set author .commentDate("MM-dd") // Comment date format .outputDir(System.getProperty("user.dir") + "/xa-seata/bank2" + "/src/main/java/") // Specify the output directory .fileOverride(); //Overwrite file }) // package configuration .packageConfig(builder -> {<!-- --> builder.parent("com.tong") // Package name prefix .entity("entity")//Entity class package name .mapper("mapper")//mapper interface package name .service("service"); //service package name }) .strategyConfig(builder -> {<!-- --> //Set the table name to be generated List<String> strings = Arrays.asList("account_info"); builder.addInclude(strings) //Start entity class configuration .entityBuilder() //Open the lombok model .enableLombok() //Convert underline to camel case in table name .naming(NamingStrategy.underline_to_camel) //Column name underline converted to camel case .columnNaming(NamingStrategy.underline_to_camel); }) .execute(); } }
Writing a transfer interface
public interface IAccountInfoService {<!-- --> //John Doe increases the amount void updateAccountBalance(String accountNo, Double amount); }
Write transfer interface implementation class
@Service @Slf4j public class AccountInfoServiceImpl implements IAccountInfoService {<!-- --> @Autowired AccountMapper accountMapper; @Override public void updateAccountBalance(String accountNo, Double amount) {<!-- --> // 1. Get user information AccountInfo accountInfo = accountMapper.selectById(accountNo); accountInfo.setAccountBalance(accountInfo.getAccountBalance() + amount); accountMapper.updateById(accountInfo); } }
Writing control layer
@RestController @RequestMapping("/bank2") public class Bank2Controller {<!-- --> @Autowired IAccountInfoService accountInfoService; //Receive Zhang San’s transfer @GetMapping("/transfer") public String transfer(Double amount){<!-- --> //John Doe increases the amount accountInfoService.updateAccountBalance("3",amount); return "bank2" + amount; } }