diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/controller/TestController.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/controller/TestController.java new file mode 100644 index 00000000..b32d80b5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/controller/TestController.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/AccountTbl.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/AccountTbl.java new file mode 100644 index 00000000..ee716d42 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/AccountTbl.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/OrderTbl.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/OrderTbl.java new file mode 100644 index 00000000..8f5b7c2f --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/OrderTbl.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/StockTbl.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/StockTbl.java new file mode 100644 index 00000000..f452cdec --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/StockTbl.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/AccountTblMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/AccountTblMapper.java new file mode 100644 index 00000000..f6a9cdd5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/AccountTblMapper.java @@ -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 { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/OrderTblMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/OrderTblMapper.java new file mode 100644 index 00000000..56983337 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/OrderTblMapper.java @@ -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 { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/StockTblMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/StockTblMapper.java new file mode 100644 index 00000000..ef6abe50 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/StockTblMapper.java @@ -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 { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/service/TestService.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/service/TestService.java new file mode 100644 index 00000000..b99f4029 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/service/TestService.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/utils/Codegen.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/utils/Codegen.java new file mode 100644 index 00000000..a1dd2649 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/utils/Codegen.java @@ -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; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/application.yml new file mode 100644 index 00000000..7b039f84 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/application.yml @@ -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 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/db/all_in_one.sql b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/db/all_in_one.sql new file mode 100644 index 00000000..e4a1f88e --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/db/all_in_one.sql @@ -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; diff --git a/mybatis-flex-test/pom.xml b/mybatis-flex-test/pom.xml index c1a4a577..de11100a 100644 --- a/mybatis-flex-test/pom.xml +++ b/mybatis-flex-test/pom.xml @@ -18,6 +18,7 @@ mybatis-flex-spring-test mybatis-flex-spring-boot-test mybatis-flex-spring-cloud-test + mybatis-flex-spring-boot-seata