From 347478e8460420359f45ac545b9163a7a8e55a4c Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 15:36:17 +0800 Subject: [PATCH 1/5] =?UTF-8?q?seata=E4=BA=8B=E5=8A=A1=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-spring-boot-starter/pom.xml | 5 ++ .../MultiDataSourceAutoConfiguration.java | 13 +++++ .../spring/boot/MybatisFlexProperties.java | 47 +++++++++++++++++++ .../mybatisflex/spring/boot/SeataMode.java | 26 ++++++++++ 4 files changed, 91 insertions(+) create mode 100644 mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java diff --git a/mybatis-flex-spring-boot-starter/pom.xml b/mybatis-flex-spring-boot-starter/pom.xml index a0d685fb..fede16e4 100644 --- a/mybatis-flex-spring-boot-starter/pom.xml +++ b/mybatis-flex-spring-boot-starter/pom.xml @@ -91,6 +91,11 @@ true + + io.seata + seata-spring-boot-starter + 1.5.2 + diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java index bc14eb06..2ae4dd88 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java @@ -19,7 +19,10 @@ import com.mybatisflex.core.datasource.DataSourceBuilder; import com.mybatisflex.core.datasource.DataSourceDecipher; import com.mybatisflex.core.datasource.DataSourceManager; import com.mybatisflex.core.datasource.FlexDataSource; +import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig; import com.mybatisflex.spring.datasource.DataSourceAdvice; +import io.seata.rm.datasource.DataSourceProxy; +import io.seata.rm.datasource.xa.DataSourceProxyXA; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.ObjectProvider; @@ -53,6 +56,8 @@ public class MultiDataSourceAutoConfiguration { private final Map> dataSourceProperties; + private final SeataConfig seataConfig; + //数据源解密器 protected final DataSourceDecipher dataSourceDecipher; @@ -62,6 +67,7 @@ public class MultiDataSourceAutoConfiguration { ) { dataSourceProperties = properties.getDatasource(); dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable(); + seataConfig = properties.getSeataConfig(); } @Bean @@ -76,6 +82,13 @@ public class MultiDataSourceAutoConfiguration { for (Map.Entry> entry : dataSourceProperties.entrySet()) { DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); + if (seataConfig.isEnable()){ + if (seataConfig.getSeataMode() ==SeataMode.XA){ + dataSource = new DataSourceProxyXA(dataSource); + }else { + dataSource = new DataSourceProxy(dataSource); + } + } if (flexDataSource == null) { flexDataSource = new FlexDataSource(entry.getKey(), dataSource); } else { diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java index 2b6e80df..d5bf56fb 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java @@ -122,6 +122,19 @@ public class MybatisFlexProperties { */ private CoreConfiguration configuration; + /** + * A Configuration object for seata + */ + private SeataConfig seataConfig; + + public SeataConfig getSeataConfig() { + return seataConfig; + } + + public void setSeataConfig(SeataConfig seataConfig) { + this.seataConfig = seataConfig; + } + public Map> getDatasource() { return datasource; } @@ -890,4 +903,38 @@ public class MybatisFlexProperties { } + /** + * Seata 配置 + * + * @author life + */ + public static class SeataConfig{ + + /** + * 是否开启 + */ + private boolean enable = false; + + /** + * 事务模式支持,只支持XA或者AT + */ + private SeataMode seataMode = SeataMode.AT; + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } + + public SeataMode getSeataMode() { + return seataMode; + } + + public void setSeataMode(SeataMode seataMode) { + this.seataMode = seataMode; + } + } + } diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java new file mode 100644 index 00000000..b02af607 --- /dev/null +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.spring.boot; + +/** + * @author life + */ +public enum SeataMode { + + XA, + + AT +} From 37a085c1bec7c322ff1d55e0ee655aea21cdac5d Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 16:35:27 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E9=9D=9Eseata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-spring-boot-starter/pom.xml | 5 +++-- .../spring/boot/MultiDataSourceAutoConfiguration.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mybatis-flex-spring-boot-starter/pom.xml b/mybatis-flex-spring-boot-starter/pom.xml index 6509530a..e9546f5f 100644 --- a/mybatis-flex-spring-boot-starter/pom.xml +++ b/mybatis-flex-spring-boot-starter/pom.xml @@ -93,8 +93,9 @@ io.seata - seata-spring-boot-starter - 1.5.2 + seata-rm-datasource + 1.7.0 + true diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java index 2ae4dd88..ffdddc98 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java @@ -82,7 +82,7 @@ public class MultiDataSourceAutoConfiguration { for (Map.Entry> entry : dataSourceProperties.entrySet()) { DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); - if (seataConfig.isEnable()){ + if (seataConfig !=null &&seataConfig.isEnable()){ if (seataConfig.getSeataMode() ==SeataMode.XA){ dataSource = new DataSourceProxyXA(dataSource); }else { From dccfb48361c8f40820c0b84f4d52aaadaffe0526 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 17:09:50 +0800 Subject: [PATCH 3/5] =?UTF-8?q?seata=E4=BA=8B=E5=8A=A1=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TestController.java | 28 ++++++ .../entity/AccountTbl.java | 51 ++++++++++ .../entity/OrderTbl.java | 71 ++++++++++++++ .../entity/StockTbl.java | 51 ++++++++++ .../mapper/AccountTblMapper.java | 15 +++ .../mapper/OrderTblMapper.java | 15 +++ .../mapper/StockTblMapper.java | 15 +++ .../service/TestService.java | 53 ++++++++++ .../utils/Codegen.java | 88 +++++++++++++++++ .../src/main/resources/application.yml | 74 ++++++++++++++ .../src/main/resources/db/all_in_one.sql | 97 +++++++++++++++++++ mybatis-flex-test/pom.xml | 1 + 12 files changed, 559 insertions(+) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/controller/TestController.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/AccountTbl.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/OrderTbl.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/entity/StockTbl.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/AccountTblMapper.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/OrderTblMapper.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/mapper/StockTblMapper.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/service/TestService.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/utils/Codegen.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/application.yml create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/resources/db/all_in_one.sql 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 From cfb318edbe1eead3435195a3c2c9e1140f01c229 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 17:44:48 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=BC=80=E5=90=AFseata=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E4=B9=A6=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/core/tx.md | 22 +++++++++++++++++++ .../src/main/resources/application.yml | 6 ++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/zh/core/tx.md b/docs/zh/core/tx.md index 8f199f41..6caa1776 100644 --- a/docs/zh/core/tx.md +++ b/docs/zh/core/tx.md @@ -124,3 +124,25 @@ public void doSomething(){ >假设在回滚的时候,恰好其中一个数据库出现了异常(比如 网络问题,数据库崩溃),此时,可能只有一个数据库的数据正常回滚(rollback)。 > 但无论如何,MyBatis-Flex 都会保证在同一个 `@Transactional` 中的多个数据源,保持相同的 commit 或者 rollback 行为。 + +## 支持seata事务 +1. 首先,先了解事务的基础(自行百度), +2. 在了解seata事务的项目 官网地址:https://seata.io/zh-cn/docs +3. 然后根据官方的[快速开始](https://seata.io/zh-cn/docs/user/quickstart.html)在下载最新版的seata-server并在本地跑起一个 +4. seata-server服务 +5. 然后使用[mybatis-flex-test](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test)模块 下面的mybatis-flex-spring-boot-seata进行测试 +>此demo只是一个纯演示的demo,模仿的是[官方事例](https://github.com/seata/seata-samples/tree/master/springboot-mybatis) +进行整合,微服务合并成一个多数据源进行测试,当然,这种方法是不提倡的,seata事务并且本地多数据源的方式可能本身就存在设计思路问题,可能存在过度设计,此demo只是作为一个演示。 +### 注意事项 +>使用seata的时候必须数据源代理 +`seata.enable-auto-data-source-proxy: false` + +`pom`自行引入[seata-spring-boot-starter](https://mvnrepository.com/artifact/io.seata/seata-spring-boot-starter)依赖, + +application.yml需要配置如下参数 +1. `mybatis-flex.seata-config.enable` + + 配置含义:seata事务是否开启,true开启,默认false关闭 +2. `mybatis-flex.seata-config.seata-mode` + + 默认启动的事务类型,目前只支持XA或者AT,默认AT 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 index 7b039f84..97c7b42c 100644 --- 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 @@ -1,7 +1,7 @@ mybatis-flex: seata-config: - enable: true - seata-mode: XA + enable: true #启动seata + seata-mode: XA #xa或者ta datasource: accountdb: url: jdbc:mysql://127.0.0.1:3306/db_account @@ -21,7 +21,7 @@ seata: enabled: true application-id: business-service tx-service-group: my_test_tx_group - enable-auto-data-source-proxy: false + enable-auto-data-source-proxy: false #必须 # use-jdk-proxy: false client: rm: From 650c41c10bceff436a536745fd6abea37fc072d8 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 18:25:14 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=BC=BA=E5=B0=91?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis-flex-spring-boot-seata/pom.xml | 155 ++++++++++++++++++ ...MybatisFlexSpringBootSeataApplication.java | 15 ++ ...isFlexSpringBootSeataApplicationTests.java | 13 ++ .../src/main/resources/application.yml | 28 ++-- 4 files changed, 197 insertions(+), 14 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/pom.xml create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplication.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-seata/src/test/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplicationTests.java diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-seata/pom.xml new file mode 100644 index 00000000..6e1055b1 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/pom.xml @@ -0,0 +1,155 @@ + + + 4.0.0 + + mybatis-flex-test + com.mybatis-flex + 1.5.6 + + com.mybatisfle.test + mybatis-flex-spring-boot-seata + 0.0.1-SNAPSHOT + mybatis-flex-spring-boot-seata + mybatis-flex-spring-boot-seata + + 1.8 + + + + com.mybatis-flex + mybatis-flex-spring-boot-starter + ${mybatis-flex.version} + + + + com.mybatis-flex + mybatis-flex-codegen + 1.5.6 + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.32 + + + + com.alibaba + druid-spring-boot-starter + 1.2.18 + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + + com.mysql + mysql-connector-j + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + RELEASE + compile + + + + io.seata + seata-spring-boot-starter + 1.7.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplication.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplication.java new file mode 100644 index 00000000..142a5773 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/main/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplication.java @@ -0,0 +1,15 @@ +package com.mybatisfle.test.mybatisflexspringbootseata; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@MapperScan("com.mybatisfle.test.mybatisflexspringbootseata") +@SpringBootApplication +public class MybatisFlexSpringBootSeataApplication { + + public static void main(String[] args) { + SpringApplication.run(MybatisFlexSpringBootSeataApplication.class, args); + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/test/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplicationTests.java b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/test/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplicationTests.java new file mode 100644 index 00000000..f763ac31 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-seata/src/test/java/com/mybatisfle/test/mybatisflexspringbootseata/MybatisFlexSpringBootSeataApplicationTests.java @@ -0,0 +1,13 @@ +package com.mybatisfle.test.mybatisflexspringbootseata; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class MybatisFlexSpringBootSeataApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index 36c7ddb6..4dca4a42 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -3,11 +3,11 @@ spring: # h2: # console: # enabled: true - datasource: -# driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/flex_test - username: root - password: 123456 +# datasource: +## driver-class-name: com.mysql.cj.jdbc.Driver +# url: jdbc:mysql://localhost:3306/flex_test +# username: root +# password: 131496 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver @@ -37,12 +37,12 @@ mybatis-flex: # username: root # password: 12345678 #mybatis-flex: -# datasource: -# ds1: -# url: jdbc:mysql://127.0.0.1:3306/db -# username: root -# password: 123456 -# ds2: -# url: jdbc:mysql://127.0.0.1:3306/db2 -# username: root -# password: 123456 + datasource: + ds3333: + url: jdbc:mysql://127.0.0.1:3306/flex_test + username: root + password: 131496 + ds2: + url: jdbc:mysql://127.0.0.1:3306/flex_test1 + username: root + password: 131496