diff --git a/pom.xml b/pom.xml index 49534a14c38ba8e56ddd8deef9f168f60efc09f7..ab3dc48af7f4cba876e2dad914364ea644567f8b 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,11 @@ seata-spring-boot-starter 1.4.0 + + com.baomidou + dynamic-datasource-spring-boot-starter + 3.5.1 + diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/PersonServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/PersonServiceImpl.java index ea7cb7a5234259eb84e0e3185a1b0453569759a7..de7c46a895a63596af4de45b8f2fc9af63b24733 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/PersonServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/PersonServiceImpl.java @@ -1,5 +1,6 @@ package com.kwan.springbootkwan.service.impl; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.mapper.PersonMapper; @@ -13,11 +14,11 @@ public class PersonServiceImpl extends ServiceImpl impleme @Autowired private PersonMapper personMapper; - // @DS(value ="ali-ds" ) + @DS(value = "ali-ds") @Override public Person savePerson(Person person) { personMapper.insert(person); - final int i = 1 / 0; +// final int i = 1 / 0; return person; } } diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java index 79ddf56abce134d834fc23eafbe16d1391ab7a83..8b3899e0f9c632d59d160dd153b16bf326e2af7b 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java @@ -1,5 +1,6 @@ package com.kwan.springbootkwan.service.impl; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; @@ -40,6 +41,7 @@ public class UserServiceImpl extends ServiceImpl implements IU return userByName; } + @DS(value = "kwan-ds") @GlobalTransactional @Override public User saveUser(User user) { diff --git a/src/main/resources/application-bak.yaml b/src/main/resources/application-bak1.yaml similarity index 66% rename from src/main/resources/application-bak.yaml rename to src/main/resources/application-bak1.yaml index 804b99a0427447cf95073d023b8470bee040c851..2ef682e413999094b7503e41c12ec2e23a6b9aa6 100644 --- a/src/main/resources/application-bak.yaml +++ b/src/main/resources/application-bak1.yaml @@ -1,3 +1,4 @@ +#这个配置是原始配置,不带seata server: port: 8761 servlet: @@ -11,8 +12,6 @@ swagger: #兼容swagger配置 spring: - application: - name: spring-boot-demo #项目启动时创建数据表的 SQL 脚本,该脚本由 Spring Batch 提供 #spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql # 在项目启动时执行建表 SQL @@ -50,48 +49,11 @@ spring: matching-strategy: ant_path_matcher # mysql datasource: - url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 716288qwe - -# dynamic: -# primary: kwan-ds -# datasource: -# kwan-ds: -# url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai -# driver-class-name: com.mysql.cj.jdbc.Driver -# username: root -# password: 716288qwe -## ali-ds: -## url: jdbc:mysql://localhost:3306/spring-boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai -## driver-class-name: com.mysql.cj.jdbc.Driver -## username: root -## password: 1716288qwe -# seata: true -# cloud: -# nacos: -# discovery: -# server-addr: http://120.79.36.53:8848 #服务注册地址 -# config: -# enabled: false - -#seata配置 -seata: - enable-auto-data-source-proxy: false - application-id: ${spring.application.name} - tx-service-group: ${spring.application.name}-group - service: - vgroup-mapping: - spring-boot-demo-group: default - grouplist: - default: 127.0.0.1:8091 - config: - type: file - registry: - type: file - #mybatis-plus配置 mybatis-plus: configuration: @@ -112,4 +74,4 @@ mybatis-plus: #logger配置 logging: - config: classpath:logback-spring.xml + config: classpath:logback-spring.xml \ No newline at end of file diff --git a/src/main/resources/application-bak2.yaml b/src/main/resources/application-bak2.yaml index a8b9276aaf8799e584250bbc74a27a002bf3e4d0..c89b7074178f6d27cd820583f496594c05b4b46d 100644 --- a/src/main/resources/application-bak2.yaml +++ b/src/main/resources/application-bak2.yaml @@ -1,3 +1,4 @@ +#这个配置是单数据库seata server: port: 8761 servlet: @@ -11,6 +12,8 @@ swagger: #兼容swagger配置 spring: + application: + name: spring-boot-name #项目启动时创建数据表的 SQL 脚本,该脚本由 Spring Batch 提供 #spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql # 在项目启动时执行建表 SQL @@ -53,6 +56,18 @@ spring: username: root password: 716288qwe +seata: + application-id: spring-boot-name + tx-service-group: my-tx-group + service: + vgroup-mapping: + my-tx-group: seata-server + grouplist: + seata-server: 127.0.0.1:8091 + enabled: true + + + #mybatis-plus配置 mybatis-plus: configuration: diff --git a/src/main/resources/application-bak3.yaml b/src/main/resources/application-bak3.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5027a85dd81efea0e8f08b0951e44f5c1470a641 --- /dev/null +++ b/src/main/resources/application-bak3.yaml @@ -0,0 +1,102 @@ +#不同库不同表,带seata +server: + port: 8761 + servlet: + encoding: + force: true + charset: UTF-8 + enabled: true + +swagger: + enable: true + +#兼容swagger配置 +spring: + application: + name: spring-boot-name + #项目启动时创建数据表的 SQL 脚本,该脚本由 Spring Batch 提供 + #spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql + # 在项目启动时执行建表 SQL + batch: + initialize-schema: always + # 禁止 Spring Batch 自动执行,在 SpringBoot 中,默认情况,当项目启动时就会执行配置好的批理操作,添加了该配置后则不会自动执行,而需要用户手动触发执行 + job: + enabled: false + mail: + host: smtp.qq.com + # 发送者的邮箱账号 + username: 327782001@qq.com + # 邮箱密码授权码 + password: kypxnmwfniqrcbeh + properties: + mail: + smtp: + auth: true + starttls: + enable: true + required: true + redis: + database: 0 # Redis数据库索引(默认为0) + host: 120.79.36.53 #Redis服务器地址 + port: 6379 # Redis服务器连接端口 + password: # Redis服务器连接密码(默认为空) + jedis: + pool: + max-active: 200 # 连接池最大连接数(使用负值表示没有限制) + max-idle: 10 # 连接池中的最大空闲连接 + min-idle: 0 # 连接池中的最小空闲连接 + max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) + mvc: + pathmatch: + matching-strategy: ant_path_matcher + # mysql + datasource: + dynamic: + seata: true + primary: kwan-ds + datasource: + kwan-ds: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + username: root + password: 716288qwe + ali-ds: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://120.79.36.53:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + username: root + password: 15671628341Qwe. + + +seata: + application-id: spring-boot-name + tx-service-group: my-tx-group + service: + vgroup-mapping: + my-tx-group: seata-server + grouplist: + seata-server: 127.0.0.1:8091 + enabled: true + + + +#mybatis-plus配置 +mybatis-plus: + configuration: + map-underscore-to-camel-case: true + call-setters-on-nulls: true + auto-mapping-behavior: full + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + mapper-locations: classpath*:mapper/**/*Mapper.xml + global-config: + banner: false + # 逻辑删除配置 + db-config: + id-type: AUTO + logic-delete-field: delFlag + logic-delete-value: 1 + logic-not-delete-value: 0 + table-underline: true + +#logger配置 +logging: + config: classpath:logback-spring.xml \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index c03f88e7597e141b9e0d62b0056d0ca4e8eaca44..bbed000a37128baf6e8bdd832ba15fc56c02f24c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -50,10 +50,21 @@ spring: matching-strategy: ant_path_matcher # mysql datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai - username: root - password: 716288qwe + dynamic: + seata: true + primary: kwan-ds + datasource: + kwan-ds: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + username: root + password: 716288qwe + ali-ds: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://120.79.36.53:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + username: root + password: 15671628341Qwe. + seata: application-id: spring-boot-name