mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
seata事务测试服务
This commit is contained in:
parent
37a085c1be
commit
dccfb48361
@ -0,0 +1,28 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.controller;
|
||||
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.service.TestService;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class TestController {
|
||||
|
||||
|
||||
@Autowired
|
||||
TestService testService;
|
||||
|
||||
@RequestMapping("buy")
|
||||
public String buy(){
|
||||
//开启审计功能
|
||||
AuditManager.setAuditEnable(true);
|
||||
//设置 SQL 审计收集器
|
||||
MessageCollector collector = new ConsoleMessageCollector();
|
||||
AuditManager.setMessageCollector(collector);
|
||||
String flag =String.valueOf(testService.buy());
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.entity;
|
||||
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 实体类。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
@Table(value = "account_tbl", schema = "db_account")
|
||||
public class AccountTbl implements Serializable {
|
||||
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Integer id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private Integer money;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Integer getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public void setMoney(Integer money) {
|
||||
this.money = money;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.entity;
|
||||
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 实体类。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
@Table(value = "order_tbl", schema = "db_order")
|
||||
public class OrderTbl implements Serializable {
|
||||
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Integer id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String commodityCode;
|
||||
|
||||
private Integer count;
|
||||
|
||||
private Integer money;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getCommodityCode() {
|
||||
return commodityCode;
|
||||
}
|
||||
|
||||
public void setCommodityCode(String commodityCode) {
|
||||
this.commodityCode = commodityCode;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public Integer getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public void setMoney(Integer money) {
|
||||
this.money = money;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.entity;
|
||||
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 实体类。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
@Table(value = "stock_tbl", schema = "db_stock")
|
||||
public class StockTbl implements Serializable {
|
||||
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Integer id;
|
||||
|
||||
private String commodityCode;
|
||||
|
||||
private Integer count;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCommodityCode() {
|
||||
return commodityCode;
|
||||
}
|
||||
|
||||
public void setCommodityCode(String commodityCode) {
|
||||
this.commodityCode = commodityCode;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.mapper;
|
||||
|
||||
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.AccountTbl;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
|
||||
/**
|
||||
* 映射层。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
public interface AccountTblMapper extends BaseMapper<AccountTbl> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.mapper;
|
||||
|
||||
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.OrderTbl;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
|
||||
/**
|
||||
* 映射层。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
public interface OrderTblMapper extends BaseMapper<OrderTbl> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.mapper;
|
||||
|
||||
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.StockTbl;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
|
||||
/**
|
||||
* 映射层。
|
||||
*
|
||||
* @author life
|
||||
* @since 2023-08-03
|
||||
*/
|
||||
public interface StockTblMapper extends BaseMapper<StockTbl> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.service;
|
||||
|
||||
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.AccountTbl;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.OrderTbl;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.StockTbl;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.entity.table.AccountTblTableDef;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.mapper.AccountTblMapper;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.mapper.OrderTblMapper;
|
||||
import com.mybatisfle.test.mybatisflexspringbootseata.mapper.StockTblMapper;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class TestService {
|
||||
|
||||
@Autowired
|
||||
AccountTblMapper accountTblMapper;
|
||||
|
||||
@Autowired
|
||||
OrderTblMapper orderTblMapper;
|
||||
|
||||
@Autowired
|
||||
StockTblMapper stockTblMapper;
|
||||
|
||||
// @Transactional
|
||||
public boolean buy() {
|
||||
// DataSourceKey.use("accountdb");
|
||||
QueryWrapper account =new QueryWrapper();
|
||||
account.where(AccountTblTableDef.ACCOUNT_TBL.USER_ID.eq("1001"));
|
||||
AccountTbl accountTbl = accountTblMapper.selectOneByQuery(account);
|
||||
accountTbl.setMoney(accountTbl.getMoney() - 5);
|
||||
accountTblMapper.update(accountTbl);
|
||||
// DataSourceKey.use("stockdb");
|
||||
// QueryWrapper stock = new QueryWrapper();
|
||||
// stock.where("id=1");
|
||||
// StockTbl stockTbl = stockTblMapper.selectOneByQuery(stock);
|
||||
// stockTbl.setCount(stockTbl.getCount() - 1);
|
||||
// stockTblMapper.update(stockTbl);
|
||||
// DataSourceKey.use("orderdb");
|
||||
// OrderTbl orderTbl = new OrderTbl();
|
||||
// orderTbl.setCount(5);
|
||||
// orderTbl.setMoney(5);
|
||||
// orderTbl.setUserId(accountTbl.getUserId());
|
||||
// orderTbl.setCount(1);
|
||||
// orderTbl.setCommodityCode(stockTbl.getCommodityCode());
|
||||
// orderTblMapper.insert(orderTbl);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,88 @@
|
||||
package com.mybatisfle.test.mybatisflexspringbootseata.utils;
|
||||
|
||||
import com.mybatisflex.codegen.Generator;
|
||||
import com.mybatisflex.codegen.config.ColumnConfig;
|
||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
public class Codegen {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//配置数据源
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db_stock?characterEncoding=utf-8");
|
||||
dataSource.setUsername("root");
|
||||
dataSource.setPassword("131496");
|
||||
|
||||
//创建配置内容,两种风格都可以。
|
||||
GlobalConfig globalConfig = createGlobalConfigUseStyle1();
|
||||
//GlobalConfig globalConfig = createGlobalConfigUseStyle2();
|
||||
|
||||
//通过 datasource 和 globalConfig 创建代码生成器
|
||||
Generator generator = new Generator(dataSource, globalConfig);
|
||||
|
||||
//生成代码
|
||||
generator.generate();
|
||||
}
|
||||
|
||||
public static GlobalConfig createGlobalConfigUseStyle1() {
|
||||
//创建配置内容
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
|
||||
//设置根包
|
||||
globalConfig.setBasePackage("com.mybatisfle.test.mybatisflexspringbootseata");
|
||||
|
||||
//设置表前缀和只生成哪些表
|
||||
globalConfig.setGenerateSchema("db_stock");
|
||||
// globalConfig.setTablePrefix("tb_");
|
||||
globalConfig.setGenerateTable("stock_tbl");
|
||||
|
||||
//设置生成 entity 并启用 Lombok
|
||||
globalConfig.setEntityGenerateEnable(true);
|
||||
globalConfig.setEntityWithLombok(true);
|
||||
|
||||
//设置生成 mapper
|
||||
globalConfig.setMapperGenerateEnable(true);
|
||||
|
||||
//可以单独配置某个列
|
||||
// ColumnConfig columnConfig = new ColumnConfig();
|
||||
// columnConfig.setColumnName("tenant_id");
|
||||
// columnConfig.setLarge(true);
|
||||
// columnConfig.setVersion(true);
|
||||
// globalConfig.setColumnConfig("account", columnConfig);
|
||||
|
||||
return globalConfig;
|
||||
}
|
||||
|
||||
public static GlobalConfig createGlobalConfigUseStyle2() {
|
||||
//创建配置内容
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
|
||||
//设置根包
|
||||
globalConfig.getPackageConfig()
|
||||
.setBasePackage("com.test");
|
||||
|
||||
//设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表
|
||||
globalConfig.getStrategyConfig()
|
||||
.setGenerateSchema("schema")
|
||||
.setTablePrefix("tb_")
|
||||
.setGenerateTable("account", "account_session");
|
||||
|
||||
//设置生成 entity 并启用 Lombok
|
||||
globalConfig.enableEntity()
|
||||
.setWithLombok(true);
|
||||
|
||||
//设置生成 mapper
|
||||
globalConfig.enableMapper();
|
||||
|
||||
//可以单独配置某个列
|
||||
ColumnConfig columnConfig = new ColumnConfig();
|
||||
columnConfig.setColumnName("tenant_id");
|
||||
columnConfig.setLarge(true);
|
||||
columnConfig.setVersion(true);
|
||||
globalConfig.getStrategyConfig()
|
||||
.setColumnConfig("account", columnConfig);
|
||||
|
||||
return globalConfig;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
mybatis-flex:
|
||||
seata-config:
|
||||
enable: true
|
||||
seata-mode: XA
|
||||
datasource:
|
||||
accountdb:
|
||||
url: jdbc:mysql://127.0.0.1:3306/db_account
|
||||
username: root
|
||||
password: 131496
|
||||
orderdb:
|
||||
url: jdbc:mysql://127.0.0.1:3306/db_order
|
||||
username: root
|
||||
password: 131496
|
||||
stockdb:
|
||||
url: jdbc:mysql://127.0.0.1:3306/db_stock
|
||||
username: root
|
||||
password: 131496
|
||||
server:
|
||||
port: 2010
|
||||
seata:
|
||||
enabled: true
|
||||
application-id: business-service
|
||||
tx-service-group: my_test_tx_group
|
||||
enable-auto-data-source-proxy: false
|
||||
# use-jdk-proxy: false
|
||||
client:
|
||||
rm:
|
||||
async-commit-buffer-limit: 1000
|
||||
report-retry-count: 5
|
||||
table-meta-check-enable: false
|
||||
report-success-enable: false
|
||||
lock:
|
||||
retry-interval: 10
|
||||
retry-times: 30
|
||||
retry-policy-branch-rollback-on-conflict: true
|
||||
tm:
|
||||
commit-retry-count: 5
|
||||
rollback-retry-count: 5
|
||||
undo:
|
||||
data-validation: true
|
||||
log-serialization: jackson
|
||||
log-table: undo_log
|
||||
log:
|
||||
exceptionRate: 100
|
||||
service:
|
||||
vgroup-mapping:
|
||||
my_test_tx_group: default
|
||||
grouplist:
|
||||
default: 127.0.0.1:8091
|
||||
#enable-degrade: false
|
||||
#disable-global-transaction: false
|
||||
transport:
|
||||
shutdown:
|
||||
wait: 3
|
||||
thread-factory:
|
||||
boss-thread-prefix: NettyBoss
|
||||
worker-thread-prefix: NettyServerNIOWorker
|
||||
server-executor-thread-prefix: NettyServerBizHandler
|
||||
share-boss-worker: false
|
||||
client-selector-thread-prefix: NettyClientSelector
|
||||
client-selector-thread-size: 1
|
||||
client-worker-thread-prefix: NettyClientWorkerThread
|
||||
worker-thread-size: default
|
||||
boss-thread-size: 1
|
||||
type: TCP
|
||||
server: NIO
|
||||
heartbeat: true
|
||||
serialization: seata
|
||||
compressor: none
|
||||
enable-client-batch-send-request: true
|
||||
config:
|
||||
type: file
|
||||
registry:
|
||||
type: file
|
||||
@ -0,0 +1,97 @@
|
||||
# Account
|
||||
DROP SCHEMA IF EXISTS db_account;
|
||||
CREATE SCHEMA db_account;
|
||||
USE db_account;
|
||||
|
||||
CREATE TABLE `account_tbl`
|
||||
(
|
||||
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` VARCHAR(255) DEFAULT NULL,
|
||||
`money` INT(11) DEFAULT 0,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
INSERT INTO account_tbl (id, user_id, money)
|
||||
VALUES (1, '1001', 10000);
|
||||
INSERT INTO account_tbl (id, user_id, money)
|
||||
VALUES (2, '1002', 10000);
|
||||
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
`ext` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
||||
|
||||
# Order
|
||||
DROP SCHEMA IF EXISTS db_order;
|
||||
CREATE SCHEMA db_order;
|
||||
USE db_order;
|
||||
|
||||
CREATE TABLE `order_tbl`
|
||||
(
|
||||
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` VARCHAR(255) DEFAULT NULL,
|
||||
`commodity_code` VARCHAR(255) DEFAULT NULL,
|
||||
`count` INT(11) DEFAULT '0',
|
||||
`money` INT(11) DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
`ext` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
||||
|
||||
# stock
|
||||
DROP SCHEMA IF EXISTS db_stock;
|
||||
CREATE SCHEMA db_stock;
|
||||
USE db_stock;
|
||||
|
||||
CREATE TABLE `stock_tbl`
|
||||
(
|
||||
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
`commodity_code` VARCHAR(255) DEFAULT NULL,
|
||||
`count` INT(11) DEFAULT '0',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `commodity_code` (`commodity_code`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
|
||||
INSERT INTO stock_tbl (id, commodity_code, count)
|
||||
VALUES (1, '2001', 1000);
|
||||
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
||||
@ -18,6 +18,7 @@
|
||||
<module>mybatis-flex-spring-test</module>
|
||||
<module>mybatis-flex-spring-boot-test</module>
|
||||
<module>mybatis-flex-spring-cloud-test</module>
|
||||
<module>mybatis-flex-spring-boot-seata</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user