├── .github
└── workflows
│ └── ci.yml
├── .gitignore
├── .mvn
└── wrapper
│ └── maven-wrapper.properties
├── LICENSE
├── README.md
├── config-samples
├── config-consul-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── consul
│ │ │ ├── ConsulApplication.java
│ │ │ └── controller
│ │ │ └── DataSourceController.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── bootstrap.yml
├── config-nacos-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── nacos
│ │ │ ├── NacosApplication.java
│ │ │ └── controller
│ │ │ └── DataSourceController.java
│ │ └── resources
│ │ └── application.yml
└── pom.xml
├── datasource-samples
├── druid-sample
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── baomidou
│ │ │ │ └── samples
│ │ │ │ └── druid
│ │ │ │ ├── DruidApplication.java
│ │ │ │ ├── controller
│ │ │ │ └── UserController.java
│ │ │ │ ├── dto
│ │ │ │ └── UserDto.java
│ │ │ │ ├── entity
│ │ │ │ └── User.java
│ │ │ │ ├── filters
│ │ │ │ ├── Filter1.java
│ │ │ │ └── Filter2.java
│ │ │ │ ├── mapper
│ │ │ │ └── UserMapper.java
│ │ │ │ └── service
│ │ │ │ ├── UserService.java
│ │ │ │ └── impl
│ │ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ │ ├── application.yml
│ │ │ ├── db
│ │ │ └── schema.sql
│ │ │ └── spy.properties
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── baomidou
│ │ └── samples
│ │ └── druid
│ │ └── test
│ │ └── DruidApplicationTest.java
└── pom.xml
├── features-samples
├── add-remove-datasource-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── ds
│ │ │ ├── AddRemoveDsApplication.java
│ │ │ ├── controller
│ │ │ └── DataSourceController.java
│ │ │ └── dto
│ │ │ └── DataSourceDTO.java
│ │ └── resources
│ │ └── application.yml
├── load-datasource-from-jdbc-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── load
│ │ │ └── LoadDsApplication.java
│ │ └── resources
│ │ └── application.yml
├── pom.xml
└── spel-sample
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── spel
│ │ │ ├── SpelApplication.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
│ └── test
│ └── java
│ └── com
│ └── baomidou
│ └── samples
│ └── spel
│ └── test
│ └── SPELTest.java
├── license.txt
├── mvnw
├── mvnw.cmd
├── no-ds-samples
├── name-pattern-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── pattern
│ │ │ ├── PatternApplication.java
│ │ │ ├── config
│ │ │ └── MyConfig.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dto
│ │ │ └── UserDto.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
└── pom.xml
├── orm-samples
├── beetlsql-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── beetl
│ │ │ ├── BeetlApplication.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ ├── dto
│ │ │ └── UserDto.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
├── jdbc-template-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── jdbc
│ │ │ ├── JdbcTemplateApplication.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dto
│ │ │ └── UserDto.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
├── mybatis-sample
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── baomidou
│ │ │ │ └── samples
│ │ │ │ └── mybatis
│ │ │ │ ├── MybatisApplication.java
│ │ │ │ ├── controller
│ │ │ │ └── UserController.java
│ │ │ │ ├── dto
│ │ │ │ └── UserDto.java
│ │ │ │ ├── entity
│ │ │ │ └── User.java
│ │ │ │ ├── mapper
│ │ │ │ └── UserMapper.java
│ │ │ │ └── service
│ │ │ │ ├── UserService.java
│ │ │ │ └── impl
│ │ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ │ ├── application.yml
│ │ │ └── db
│ │ │ └── schema.sql
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── baomidou
│ │ └── mybatis
│ │ └── test
│ │ └── EncTest.java
├── mybatisplus3-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── mybatisplus3
│ │ │ ├── MybatisPlus3Application.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dto
│ │ │ └── UserDto.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
└── pom.xml
├── pom.xml
├── springboot3-sample
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── baomidou
│ │ └── sample
│ │ └── boot3
│ │ ├── SpringBoot3Application.java
│ │ ├── config
│ │ ├── MyBatisNativeConfiguration.java
│ │ └── ProjectRuntimeHintRegister.java
│ │ ├── controller
│ │ └── UserController.java
│ │ ├── dto
│ │ └── UserDto.java
│ │ ├── entity
│ │ └── User.java
│ │ ├── mapper
│ │ └── UserMapper.java
│ │ └── service
│ │ ├── UserService.java
│ │ └── impl
│ │ └── UserServiceImpl.java
│ └── resources
│ ├── application.yml
│ └── db
│ └── schema.sql
├── third-part-samples
├── pom.xml
├── quartz-sample
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── quartz
│ │ │ ├── QuartzApplication.java
│ │ │ ├── config
│ │ │ ├── MyQuartzAutoConfigurationMode1.java
│ │ │ ├── MyQuartzAutoConfigurationMode2.java
│ │ │ └── QuartzCommonAutoConfiguration.java
│ │ │ └── job
│ │ │ └── HelloworldJob.java
│ │ └── resources
│ │ └── application.yml
└── shardingsphere-sample
│ ├── pom.xml
│ ├── shardingsphere-jdbc-4.x-spring-sample
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── baomidou
│ │ │ │ └── samples
│ │ │ │ └── shardingsphere
│ │ │ │ └── jdbc
│ │ │ │ └── v4
│ │ │ │ └── spring
│ │ │ │ ├── ShardingSphereApplication.java
│ │ │ │ ├── config
│ │ │ │ └── MyDataSourceConfiguration.java
│ │ │ │ ├── controller
│ │ │ │ ├── TOrderController.java
│ │ │ │ └── UserController.java
│ │ │ │ ├── dto
│ │ │ │ └── UserDto.java
│ │ │ │ ├── entity
│ │ │ │ ├── TOrder.java
│ │ │ │ └── User.java
│ │ │ │ ├── mapper
│ │ │ │ ├── TOrderMapper.java
│ │ │ │ └── UserMapper.java
│ │ │ │ └── service
│ │ │ │ ├── TOrderService.java
│ │ │ │ ├── UserService.java
│ │ │ │ └── impl
│ │ │ │ ├── TOrderServiceImpl.java
│ │ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ │ ├── application.yml
│ │ │ └── db
│ │ │ └── schema.sql
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── baomidou
│ │ └── samples
│ │ └── shardingsphere
│ │ └── jdbc
│ │ └── v4
│ │ └── spring
│ │ ├── ControllerLayerTests.java
│ │ └── MapperLayerTests.java
│ ├── shardingsphere-jdbc-5.x-core-sample
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── baomidou
│ │ │ │ └── samples
│ │ │ │ └── shardingsphere
│ │ │ │ └── jdbc
│ │ │ │ └── v5
│ │ │ │ └── core
│ │ │ │ ├── ShardingSphereApplication.java
│ │ │ │ ├── controller
│ │ │ │ ├── TOrderController.java
│ │ │ │ └── UserController.java
│ │ │ │ ├── dto
│ │ │ │ └── UserDto.java
│ │ │ │ ├── entity
│ │ │ │ ├── TOrder.java
│ │ │ │ └── User.java
│ │ │ │ ├── mapper
│ │ │ │ ├── TOrderMapper.java
│ │ │ │ └── UserMapper.java
│ │ │ │ └── service
│ │ │ │ ├── TOrderService.java
│ │ │ │ ├── UserService.java
│ │ │ │ └── impl
│ │ │ │ ├── TOrderServiceImpl.java
│ │ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ │ ├── application.yml
│ │ │ ├── config.yaml
│ │ │ └── db
│ │ │ └── schema.sql
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── baomidou
│ │ └── samples
│ │ └── shardingsphere
│ │ └── jdbc
│ │ └── v5
│ │ └── core
│ │ ├── ControllerLayerTests.java
│ │ └── MapperLayerTests.java
│ └── shardingsphere-jdbc-5.x-spring-sample
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── baomidou
│ │ │ └── samples
│ │ │ └── shardingsphere
│ │ │ └── jdbc
│ │ │ └── v5
│ │ │ └── spring
│ │ │ ├── ShardingSphereApplication.java
│ │ │ ├── config
│ │ │ └── MyDataSourceConfiguration.java
│ │ │ ├── controller
│ │ │ ├── TOrderController.java
│ │ │ └── UserController.java
│ │ │ ├── dto
│ │ │ └── UserDto.java
│ │ │ ├── entity
│ │ │ ├── TOrder.java
│ │ │ └── User.java
│ │ │ ├── mapper
│ │ │ ├── TOrderMapper.java
│ │ │ └── UserMapper.java
│ │ │ └── service
│ │ │ ├── TOrderService.java
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ ├── TOrderServiceImpl.java
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ └── db
│ │ └── schema.sql
│ └── test
│ └── java
│ └── com
│ └── baomidou
│ └── samples
│ └── shardingsphere
│ └── jdbc
│ └── v5
│ └── spring
│ ├── ControllerLayerTests.java
│ └── MapperLayerTests.java
└── tx-samples
├── pom.xml
├── tx-local-sample
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── baomidou
│ │ └── samples
│ │ └── localtx
│ │ ├── LocalTxApplication.java
│ │ ├── common
│ │ └── OrderStatus.java
│ │ ├── config
│ │ └── GlobalExceptionHandler.java
│ │ ├── controller
│ │ └── OrderController.java
│ │ ├── dto
│ │ ├── PlaceOrderRequest.java
│ │ ├── ReduceBalanceRequest.java
│ │ └── ReduceStockRequest.java
│ │ ├── entity
│ │ ├── Account.java
│ │ ├── Order.java
│ │ └── Product.java
│ │ ├── mapper
│ │ ├── AccountMapper.java
│ │ ├── OrderMapper.java
│ │ └── ProductMapper.java
│ │ └── service
│ │ ├── AccountService.java
│ │ ├── OrderService.java
│ │ └── ProductService.java
│ └── resources
│ ├── application.yml
│ └── db
│ └── postgresql
│ ├── schema-account.sql
│ ├── schema-order.sql
│ └── schema-product.sql
└── tx-seata-sample
├── README.md
├── pom.xml
└── src
└── main
├── java
└── com
│ └── baomidou
│ └── samples
│ └── seata
│ ├── SeataApplication.java
│ ├── common
│ └── OrderStatus.java
│ ├── config
│ └── GlobalExceptionHandler.java
│ ├── controller
│ └── OrderController.java
│ ├── dto
│ ├── PlaceOrderRequest.java
│ ├── ReduceBalanceRequest.java
│ └── ReduceStockRequest.java
│ ├── entity
│ ├── Account.java
│ ├── Order.java
│ └── Product.java
│ ├── mapper
│ ├── AccountMapper.java
│ ├── OrderMapper.java
│ └── ProductMapper.java
│ └── service
│ ├── AccountService.java
│ ├── OrderService.java
│ ├── ProductService.java
│ └── impl
│ ├── AccountServiceImpl.java
│ ├── OrderServiceImpl.java
│ └── ProductServiceImpl.java
└── resources
├── application-mysql.yml
├── application-postgresql.yml
├── application.yml
└── db
├── mysql
├── schema-account.sql
├── schema-order.sql
├── schema-product.sql
└── undo_log.sql
└── postgresql
├── schema-account.sql
├── schema-order.sql
└── schema-product.sql
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | test-hotspot-jdk-ci:
11 | name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
12 | runs-on: ${{ matrix.os }}
13 | timeout-minutes: 60
14 | strategy:
15 | matrix:
16 | os: [ 'ubuntu-latest' ]
17 | java-version: [ '17', '21' ]
18 | steps:
19 | - uses: actions/checkout@v3
20 | - name: Setup java
21 | uses: actions/setup-java@v3
22 | with:
23 | distribution: 'temurin'
24 | java-version: ${{ matrix.java-version }}
25 | cache: 'maven'
26 | - name: Build all test with Maven
27 | run: |
28 | ./mvnw -T1C -B -e clean test
29 | test-minimum-hotspot-jdk-ci:
30 | name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
31 | runs-on: ${{ matrix.os }}
32 | timeout-minutes: 60
33 | strategy:
34 | matrix:
35 | os: [ 'ubuntu-latest' ]
36 | java-version: [ '8' ]
37 | steps:
38 | - uses: actions/checkout@v3
39 | - name: Setup java
40 | uses: actions/setup-java@v3
41 | with:
42 | distribution: 'temurin'
43 | java-version: ${{ matrix.java-version }}
44 | cache: 'maven'
45 | - name: Build datasource test with Maven
46 | run: |
47 | ./mvnw -am -pl datasource-samples/druid-sample -T1C -B -e clean test
48 | - name: Build orm test with Maven
49 | run: |
50 | ./mvnw -am -pl orm-samples/mybatis-sample -T1C -B -e clean test
51 | - name: Build third-part test with Maven
52 | run: |
53 | ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample -T1C -B -e clean test
54 | ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample -T1C -B -e clean test
55 | ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample -T1C -B -e clean test
56 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | */target/
2 | target
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
26 |
27 | ### We don't want to check in any binaries in Git ###
28 | **/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one
2 | # or more contributor license agreements. See the NOTICE file
3 | # distributed with this work for additional information
4 | # regarding copyright ownership. The ASF licenses this file
5 | # to you under the Apache License, Version 2.0 (the
6 | # "License"); you may not use this file except in compliance
7 | # with the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing,
12 | # software distributed under the License is distributed on an
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | # KIND, either express or implied. See the License for the
15 | # specific language governing permissions and limitations
16 | # under the License.
17 | wrapperVersion=3.3.2
18 | distributionType=only-script
19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 演示例子
2 |
3 | 大部分数据库连接为 H2Database,仅供测试。
4 |
5 | 除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 [OpenJDK 8, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试。
6 |
7 | 所有单元测试在 Github Actions 完成验证。你可能希望参考 [位于 Github Actions 的 CI 文件](./.github/workflows/ci.yml)。
8 |
9 | - add-remove-datasource 动态添加删除数据源的使用示例
10 | - all-datasource-sample 所有不同连接池使用示例(大乱炖,实际不建议)
11 | - druid-sample 集成mybatis和druid的使用示例
12 | - jdbc-template-sample 集成原生jdbcTemplate的使用示例
13 | - load-datasource-from-jdbc 通过数据库配置来启动数据源示例
14 | - mybatis-sample 集成原生mybatis的使用示例
15 | - mybatisplus2-sample 集成mybatisPlus2的使用示例
16 | - mybatisplus3-sample 集成mybatisPlus3的使用示例
17 | - name-pattern-sample 自定义切面的使用示例
18 | - nest-sample 嵌套切换数据源使用示例
19 | - quartz-sample 多数据源集成quartz示例
20 | - shardingsphere-jdbc-4.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 4.1.1 使用示例, 不再维护,
21 | 参考 https://github.com/apache/shardingsphere/releases/tag/5.0.0-alpha
22 | - shardingsphere-jdbc-5.x-core-sample 集成 ShardingSphere JDBC Driver 5.5.2 使用示例
23 | - shardingsphere-jdbc-5.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 5.2.1 使用示例, 不再维护,
24 | 参考 https://github.com/apache/shardingsphere/issues/22469
25 | - spel-sample 动态从外部参数spel来切换数据源的使用示例
26 | - tx-local-sample 本地事务示例项目★★★★★★必看★★★★★★
27 | - tx-seata-sample 基于seata的分布式事务集成使用示例
28 |
29 | ## Contributing
30 |
31 | 我们欢迎社区的贡献。围绕此 git 的讨论与协作应通过 https://github.com/baomidou/dynamic-datasource/issues 进行。
32 |
33 | 针对 IDE,项目的语言级别应设置为 JDK 8,对于单独的 `com.baomidou:springboot3-sample` 子模块,语言级别应设置为 JDK 17 。
34 | 在提交 Pull Request 之前, 请在本地通过 [OpenJDK 17, OpenJDK 21] 的 JDK 范围下完成此命令的验证。
35 | 我们鼓励通过 `SDKMAN!` 切换到 `21.0.2-graalce` 来验证。
36 |
37 | ```shell
38 | sdk install java 21.0.2-graalce
39 | sdk use java 21.0.2-graalce
40 |
41 | git clone git@github.com:dynamic-datasource/dynamic-datasource-samples.git
42 | cd ./dynamic-datasource-samples/
43 | ./mvnw -T1C -e clean test
44 | ```
45 |
--------------------------------------------------------------------------------
/config-samples/config-consul-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | config-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | config-consul-sample
13 |
14 |
15 | Greenwich.SR2
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 | org.springframework.cloud
25 | spring-cloud-starter-consul-config
26 | 3.0.4
27 |
28 |
29 | org.springdoc
30 | springdoc-openapi-ui
31 |
32 |
33 | com.baomidou
34 | dynamic-datasource-spring-boot-starter
35 |
36 |
37 | com.h2database
38 | h2
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | provided
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/config-samples/config-consul-sample/src/main/java/com/baomidou/samples/consul/ConsulApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.consul;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @EnableDiscoveryClient
26 | public class ConsulApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(ConsulApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/config-samples/config-consul-sample/src/main/java/com/baomidou/samples/consul/controller/DataSourceController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.consul.controller;
17 |
18 | import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
19 | import org.springframework.beans.factory.annotation.Autowired;
20 | import org.springframework.web.bind.annotation.GetMapping;
21 | import org.springframework.web.bind.annotation.RequestMapping;
22 | import org.springframework.web.bind.annotation.RestController;
23 |
24 | import javax.sql.DataSource;
25 | import java.util.Set;
26 |
27 | @RestController
28 | @RequestMapping("/datasources")
29 | public class DataSourceController {
30 |
31 | @Autowired
32 | private DataSource dataSource;
33 |
34 | /**
35 | * 获取当前所有数据源
36 | */
37 | @GetMapping
38 | public Set now() {
39 | DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
40 | return ds.getDataSources().keySet();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/config-samples/config-consul-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | #spring:
2 | # cloud:
3 | # consul:
4 | # host: 110.40.253.205
5 | # config:
6 | # format: yaml
7 | # name: dynamic-datasource
--------------------------------------------------------------------------------
/config-samples/config-consul-sample/src/main/resources/bootstrap.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | cloud:
3 | consul:
4 | host: ali
5 | config:
6 | prefix: dynamic-datasource
7 | enabled: true
8 | format: YAML
9 |
--------------------------------------------------------------------------------
/config-samples/config-nacos-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | config-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | config-nacos-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | com.alibaba.boot
21 | nacos-config-spring-boot-starter
22 | 0.2.12
23 |
24 |
25 | org.springdoc
26 | springdoc-openapi-ui
27 |
28 |
29 | com.baomidou
30 | dynamic-datasource-spring-boot-starter
31 |
32 |
33 | com.h2database
34 | h2
35 |
36 |
37 | org.projectlombok
38 | lombok
39 | provided
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-test
44 | test
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/config-samples/config-nacos-sample/src/main/java/com/baomidou/samples/nacos/NacosApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.nacos;
17 |
18 | import com.alibaba.nacos.api.config.ConfigType;
19 | import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
20 | import lombok.extern.slf4j.Slf4j;
21 | import org.springframework.boot.SpringApplication;
22 | import org.springframework.boot.autoconfigure.SpringBootApplication;
23 |
24 | @Slf4j
25 | @SpringBootApplication
26 | @NacosPropertySource(dataId = "dynamic-datasource", type = ConfigType.YAML)
27 | public class NacosApplication {
28 |
29 | public static void main(String[] args) {
30 | log.info("nacos暂不支持最新版springboot 测试自行降级到2.3.12.RELEASE");
31 | SpringApplication.run(NacosApplication.class, args);
32 | log.info("open http://localhost:8080/swagger-ui.html");
33 | }
34 | }
--------------------------------------------------------------------------------
/config-samples/config-nacos-sample/src/main/java/com/baomidou/samples/nacos/controller/DataSourceController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.nacos.controller;
17 |
18 | import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
19 | import org.springframework.beans.factory.annotation.Autowired;
20 | import org.springframework.web.bind.annotation.GetMapping;
21 | import org.springframework.web.bind.annotation.RequestMapping;
22 | import org.springframework.web.bind.annotation.RestController;
23 |
24 | import javax.sql.DataSource;
25 | import java.util.Set;
26 |
27 | @RestController
28 | @RequestMapping("/datasources")
29 | public class DataSourceController {
30 |
31 | @Autowired
32 | private DataSource dataSource;
33 |
34 | /**
35 | * 获取所有数据源
36 | */
37 | @GetMapping
38 | public Set now() {
39 | DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
40 | return ds.getDataSources().keySet();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/config-samples/config-nacos-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | nacos:
2 | config:
3 | server-addr: 110.40.253.205:8848
--------------------------------------------------------------------------------
/config-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | config-samples
13 | pom
14 |
15 |
16 | config-nacos-sample
17 | config-consul-sample
18 |
19 |
20 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/DruidApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.druid.mapper")
26 | public class DruidApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(DruidApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html \n" +
31 | "http://localhost:8080/druid/index.html");
32 | }
33 | }
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/controller/UserController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.controller;
17 |
18 | import com.baomidou.samples.druid.entity.User;
19 | import com.baomidou.samples.druid.service.UserService;
20 | import lombok.AllArgsConstructor;
21 | import org.springframework.web.bind.annotation.*;
22 |
23 | import java.util.List;
24 | import java.util.Random;
25 |
26 | @RestController
27 | @AllArgsConstructor
28 | @RequestMapping("/users")
29 | public class UserController {
30 |
31 | private static final Random RANDOM = new Random();
32 | private final UserService userService;
33 |
34 | @GetMapping("master")
35 | public List masterUsers() {
36 | return userService.selectMasterUsers();
37 | }
38 |
39 | @GetMapping("slave")
40 | public List slaveUsers() {
41 | return userService.selectSlaveUsers();
42 | }
43 |
44 | @PostMapping
45 | public User addUser() {
46 | User user = new User();
47 | user.setName("测试用户" + RANDOM.nextInt());
48 | user.setAge(RANDOM.nextInt(100));
49 | userService.addUser(user);
50 | return user;
51 | }
52 |
53 | @DeleteMapping("{id}")
54 | public String deleteUser(@PathVariable Long id) {
55 | userService.deleteUserById(id);
56 | return "成功删除用户" + id;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/filters/Filter1.java:
--------------------------------------------------------------------------------
1 | package com.baomidou.samples.druid.filters;
2 |
3 | import com.alibaba.druid.filter.FilterAdapter;
4 | import com.alibaba.druid.filter.FilterChain;
5 | import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.stereotype.Component;
8 |
9 | import java.sql.SQLException;
10 |
11 | @Slf4j
12 | @Component("myFilter1")
13 | public class Filter1 extends FilterAdapter {
14 |
15 | @Override
16 | public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
17 | log.info("ssss");
18 | return super.preparedStatement_executeUpdate(chain, statement);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/filters/Filter2.java:
--------------------------------------------------------------------------------
1 | package com.baomidou.samples.druid.filters;
2 |
3 | import com.alibaba.druid.filter.FilterAdapter;
4 | import com.alibaba.druid.filter.FilterChain;
5 | import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.stereotype.Component;
8 |
9 | import java.sql.SQLException;
10 |
11 | @Slf4j
12 | @Component("myFilter2")
13 | public class Filter2 extends FilterAdapter {
14 |
15 | @Override
16 | public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
17 | log.info("ssss");
18 | return super.preparedStatement_executeUpdate(chain, statement);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.mapper;
17 |
18 | import com.baomidou.samples.druid.entity.User;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @Component
28 | public interface UserMapper {
29 |
30 | @Select("select * from t_user where age > #{age}")
31 | List selectUsers(@Param("age") Integer age);
32 |
33 | @SuppressWarnings("UnusedReturnValue")
34 | @Insert("insert into t_user (name,age) values (#{name},#{age})")
35 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
36 |
37 | @Delete("delete from t_user where id = #{id}")
38 | void deleteUserById(Long id);
39 | }
40 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.service;
17 |
18 |
19 | import com.baomidou.samples.druid.entity.User;
20 |
21 | import java.util.List;
22 |
23 | public interface UserService {
24 |
25 | List selectMasterUsers();
26 |
27 | List selectSlaveUsers();
28 |
29 | void addUser(User user);
30 |
31 | void deleteUserById(Long id);
32 | }
33 |
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/java/com/baomidou/samples/druid/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.service.impl;
17 |
18 |
19 | import com.baomidou.dynamic.datasource.annotation.DS;
20 | import com.baomidou.samples.druid.entity.User;
21 | import com.baomidou.samples.druid.mapper.UserMapper;
22 | import com.baomidou.samples.druid.service.UserService;
23 | import org.springframework.stereotype.Service;
24 |
25 | import java.util.List;
26 |
27 | @Service
28 | public class UserServiceImpl implements UserService {
29 |
30 | private final UserMapper userMapper;
31 |
32 | public UserServiceImpl(UserMapper userMapper) {
33 | this.userMapper = userMapper;
34 | }
35 |
36 | @Override
37 | public List selectMasterUsers() {
38 | return userMapper.selectUsers(1);
39 | }
40 |
41 | @DS("slave")
42 | @Override
43 | public List selectSlaveUsers() {
44 | return userMapper.selectUsers(1);
45 | }
46 |
47 | @Override
48 | public void addUser(User user) {
49 | userMapper.addUser(user.getName(), user.getAge());
50 | }
51 |
52 | @Override
53 | public void deleteUserById(Long id) {
54 | userMapper.deleteUserById(id);
55 | }
56 | }
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | druid:
4 | stat-view-servlet:
5 | enabled: true
6 | login-username: admin
7 | login-password: 123456
8 | dynamic:
9 | p6spy: true
10 | lazy: true
11 | # 配置全局druid参数,请按需配置
12 | druid:
13 | initial-size: 5
14 | max-active: 8
15 | min-idle: 3
16 | max-wait: 1000
17 | validation-query: 'select 1'
18 | proxy-filters: myFilter1,myFilter2
19 | datasource:
20 | master:
21 | username: sa
22 | password: "123456"
23 | url: jdbc:h2:mem:test;MODE=MySQL
24 | driver-class-name: org.h2.Driver
25 | init:
26 | schema: db/schema.sql
27 | druid:
28 | socketTimeout: 1111
29 | slave_1:
30 | username: sa
31 | password: "123456"
32 | url: jdbc:h2:mem:test;MODE=MySQL
33 | driver-class-name: org.h2.Driver
34 | druid:
35 | initial-size: 6
36 | slave_2:
37 | username: sa
38 | password: "123456"
39 | url: jdbc:h2:mem:test;MODE=MySQL
40 | driver-class-name: org.h2.Driver
41 | logging:
42 | level:
43 | com.baomidou: debug
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | `name` VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/main/resources/spy.properties:
--------------------------------------------------------------------------------
1 | appender=com.p6spy.engine.spy.appender.Slf4JLogger
--------------------------------------------------------------------------------
/datasource-samples/druid-sample/src/test/java/com/baomidou/samples/druid/test/DruidApplicationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.druid.test;
17 |
18 | import com.baomidou.samples.druid.DruidApplication;
19 | import com.baomidou.samples.druid.entity.User;
20 | import com.baomidou.samples.druid.service.UserService;
21 | import org.junit.jupiter.api.Test;
22 | import org.springframework.beans.factory.annotation.Autowired;
23 | import org.springframework.boot.test.context.SpringBootTest;
24 |
25 | import java.util.Random;
26 |
27 | @SpringBootTest(classes = DruidApplication.class)
28 | public class DruidApplicationTest {
29 |
30 | private final Random random = new Random();
31 |
32 | @Autowired
33 | private UserService userService;
34 |
35 | @Test
36 | public void addUser() {
37 | User user = new User();
38 | user.setName("测试用户" + random.nextInt());
39 | user.setAge(random.nextInt(100));
40 | userService.addUser(user);
41 | }
42 |
43 | @Test
44 | public void selectUsersFromDs() {
45 | userService.selectMasterUsers();
46 | }
47 |
48 | @Test
49 | public void selectUserFromDsGroup() {
50 | userService.selectSlaveUsers();
51 | }
52 |
53 | }
--------------------------------------------------------------------------------
/datasource-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | datasource-samples
13 | pom
14 |
15 |
16 | druid-sample
17 |
18 |
--------------------------------------------------------------------------------
/features-samples/add-remove-datasource-sample/src/main/java/com/baomidou/samples/ds/AddRemoveDsApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.ds;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @Slf4j
23 | @SpringBootApplication
24 | public class AddRemoveDsApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(AddRemoveDsApplication.class, args);
28 | log.info("open http://localhost:8080/swagger-ui.html");
29 | }
30 | }
--------------------------------------------------------------------------------
/features-samples/add-remove-datasource-sample/src/main/java/com/baomidou/samples/ds/dto/DataSourceDTO.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.ds.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class DataSourceDTO {
22 |
23 | /**
24 | * 连接池名称
25 | */
26 | private String poolName;
27 |
28 | /**
29 | * JDBC driver org.h2.Driver
30 | */
31 | private String driverClassName;
32 |
33 | /**
34 | * JDBC url 地址
35 | */
36 | private String url;
37 |
38 | /**
39 | * JDBC 用户名
40 | */
41 | private String username;
42 |
43 | /**
44 | * JDBC 密码
45 | */
46 | private String password;
47 | }
--------------------------------------------------------------------------------
/features-samples/add-remove-datasource-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | slave_1:
11 | username: sa
12 | password: ""
13 | url: jdbc:h2:mem:test1
14 | driver-class-name: org.h2.Driver
15 | slave_2:
16 | username: sa
17 | password: ""
18 | url: jdbc:h2:mem:test2
19 | driver-class-name: org.h2.Driver
20 | druid:
21 | validation-query: "select 1"
22 |
23 | logging:
24 | level:
25 | com.baomidou: debug
--------------------------------------------------------------------------------
/features-samples/load-datasource-from-jdbc-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | com.baomidou
7 | features-samples
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | load-datasource-from-jdbc-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | com.alibaba
21 | druid
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-validation
26 |
27 |
28 | org.springdoc
29 | springdoc-openapi-ui
30 |
31 |
32 | com.baomidou
33 | dynamic-datasource-spring-boot-starter
34 |
35 |
36 | com.h2database
37 | h2
38 |
39 |
40 | org.projectlombok
41 | lombok
42 | provided
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-test
47 | test
48 |
49 |
50 |
--------------------------------------------------------------------------------
/features-samples/load-datasource-from-jdbc-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | druid:
5 | filters: ""
6 | datasource:
7 | master:
8 | username: sa
9 | password: ""
10 | url: jdbc:h2:mem:test;MODE=MySQL
11 | driver-class-name: org.h2.Driver
12 | logging:
13 | level:
14 | com.baomidou: debug
--------------------------------------------------------------------------------
/features-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 |
11 | 4.0.0
12 |
13 | features-samples
14 | pom
15 |
16 |
17 | add-remove-datasource-sample
18 | load-datasource-from-jdbc-sample
19 | spel-sample
20 |
21 |
--------------------------------------------------------------------------------
/features-samples/spel-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | features-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | spel-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-validation
22 |
23 |
24 | org.springdoc
25 | springdoc-openapi-ui
26 |
27 |
28 | com.baomidou
29 | dynamic-datasource-spring-boot-starter
30 |
31 |
32 | org.mybatis.spring.boot
33 | mybatis-spring-boot-starter
34 |
35 |
36 | com.h2database
37 | h2
38 |
39 |
40 | org.projectlombok
41 | lombok
42 | provided
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-test
47 | test
48 |
49 |
50 |
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/java/com/baomidou/samples/spel/SpelApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.spel;
17 |
18 | import org.mybatis.spring.annotation.MapperScan;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @SpringBootApplication
23 | @MapperScan("com.baomidou.samples.spel.mapper")
24 | public class SpelApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(SpelApplication.class, args);
28 | }
29 | }
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/java/com/baomidou/samples/spel/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.spel.entity;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Data;
20 | import lombok.NoArgsConstructor;
21 |
22 | @Data
23 | @AllArgsConstructor
24 | @NoArgsConstructor
25 | public class User {
26 |
27 | private Integer id;
28 |
29 | private String name;
30 |
31 | private Integer age;
32 |
33 | private String tenantName;
34 |
35 | public User(String tenantName) {
36 | this(null, null, null, tenantName);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/java/com/baomidou/samples/spel/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.spel.mapper;
17 |
18 | import com.baomidou.samples.spel.entity.User;
19 | import org.apache.ibatis.annotations.Select;
20 |
21 | import java.util.List;
22 |
23 | @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
24 | public interface UserMapper {
25 |
26 | @Select("select * from t_user")
27 | List selectUsers();
28 | }
29 |
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/java/com/baomidou/samples/spel/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.spel.service;
17 |
18 |
19 | import com.baomidou.dynamic.datasource.annotation.DS;
20 | import com.baomidou.samples.spel.entity.User;
21 |
22 | import java.util.List;
23 |
24 | public interface UserService {
25 |
26 | List selectSpelBySession();
27 |
28 | List selectSpelByHeader();
29 |
30 | List selectSpelByKey(String tenantName);
31 |
32 | List selecSpelByTenant(User user);
33 |
34 | @DS("#tenantName")
35 | String getGroupNameInSpELSelf(String tenantName);
36 |
37 | @DS("#user.tenantName")
38 | String getGroupNameInsideObjectSpEL(User user);
39 | }
40 |
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
9 | driver-class-name: org.h2.Driver
10 | tenant1_1:
11 | username: sa
12 | password: ""
13 | url: jdbc:h2:mem:tenant1_1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
14 | driver-class-name: org.h2.Driver
15 | tenant1_2:
16 | username: sa
17 | password: ""
18 | url: jdbc:h2:mem:tenant1_2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
19 | driver-class-name: org.h2.Driver
20 | tenant2_1:
21 | username: sa
22 | password: ""
23 | url: jdbc:h2:mem:tenant2_1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
24 | driver-class-name: org.h2.Driver
25 | tenant2_2:
26 | username: sa
27 | password: ""
28 | url: jdbc:h2:mem:tenant2_2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
29 | driver-class-name: org.h2.Driver
30 | logging:
31 | level:
32 | com.baomidou: debug
--------------------------------------------------------------------------------
/features-samples/spel-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT NULL DEFAULT NULL
6 | );
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright © ${project.inceptionYear} organization baomidou
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | no-ds-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | name-pattern-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-validation
22 |
23 |
24 | org.springdoc
25 | springdoc-openapi-ui
26 |
27 |
28 | com.baomidou
29 | dynamic-datasource-spring-boot-starter
30 |
31 |
32 | org.mybatis.spring.boot
33 | mybatis-spring-boot-starter
34 |
35 |
36 | com.h2database
37 | h2
38 |
39 |
40 | org.projectlombok
41 | lombok
42 | provided
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-test
47 | test
48 |
49 |
50 |
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/PatternApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.pattern.mapper")
26 | public class PatternApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(PatternApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern.mapper;
17 |
18 | import com.baomidou.samples.pattern.entity.User;
19 | import org.apache.ibatis.annotations.*;
20 |
21 | import java.util.List;
22 |
23 | public interface UserMapper {
24 |
25 | @Select("select * from t_user where id =#{id}")
26 | User selectById(Integer id);
27 |
28 | @Select("select * from t_user")
29 | List selectUsers();
30 |
31 | @Insert("insert into t_user (name,age) values (#{name},#{age})")
32 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
33 |
34 | @Update("update t_user set name =#{name} where id =#{id}")
35 | void updateUser(User user);
36 |
37 | @Delete("delete from t_user where id = #{id}")
38 | void deleteUserById(Long id);
39 |
40 | @Delete("delete from t_user")
41 | void deleteAll();
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern.service;
17 |
18 |
19 | import com.baomidou.samples.pattern.entity.User;
20 |
21 | import java.util.List;
22 |
23 | public interface UserService {
24 |
25 | User selectById(Integer id);
26 |
27 | List selectAll();
28 |
29 | void addUser(User user);
30 |
31 | void updateUser(User user);
32 |
33 | void deleteUserById(Long id);
34 |
35 | void deleteAll();
36 | }
37 |
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/java/com/baomidou/samples/pattern/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.pattern.service.impl;
17 |
18 |
19 | import com.baomidou.samples.pattern.entity.User;
20 | import com.baomidou.samples.pattern.mapper.UserMapper;
21 | import com.baomidou.samples.pattern.service.UserService;
22 | import org.springframework.beans.factory.annotation.Autowired;
23 | import org.springframework.stereotype.Service;
24 |
25 | import java.util.List;
26 |
27 | @Service
28 | public class UserServiceImpl implements UserService {
29 |
30 | @Autowired
31 | private UserMapper userMapper;
32 |
33 | @Override
34 | public User selectById(Integer id) {
35 | return userMapper.selectById(id);
36 | }
37 |
38 | @Override
39 | public List selectAll() {
40 | return userMapper.selectUsers();
41 | }
42 |
43 | @Override
44 | public void addUser(User user) {
45 | userMapper.addUser(user.getName(), user.getAge());
46 | }
47 |
48 | @Override
49 | public void updateUser(User user) {
50 | userMapper.updateUser(user);
51 | }
52 |
53 | @Override
54 | public void deleteUserById(Long id) {
55 | userMapper.deleteUserById(id);
56 | }
57 |
58 | @Override
59 | public void deleteAll() {
60 | userMapper.deleteAll();
61 | }
62 | }
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | init:
11 | schema: db/schema.sql
12 | slave_1:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | slave_2:
18 | username: sa
19 | password: ""
20 | url: jdbc:h2:mem:test;MODE=MySQL
21 | driver-class-name: org.h2.Driver
22 | logging:
23 | level:
24 | com.baomidou: debug
--------------------------------------------------------------------------------
/no-ds-samples/name-pattern-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/no-ds-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 |
11 | 4.0.0
12 |
13 | no-ds-samples
14 | pom
15 |
16 |
17 | name-pattern-sample
18 |
19 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | orm-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | beetlsql-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-validation
22 |
23 |
24 | org.springdoc
25 | springdoc-openapi-ui
26 |
27 |
28 | com.baomidou
29 | dynamic-datasource-spring-boot-starter
30 |
31 |
32 | com.ibeetl
33 | sql-springboot-starter
34 | 3.19.0-RELEASE
35 |
36 |
37 | com.h2database
38 | h2
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | provided
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/BeetlApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @Slf4j
23 | @SpringBootApplication
24 | public class BeetlApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(BeetlApplication.class, args);
28 | log.info("open http://localhost:8080/swagger-ui.html");
29 | }
30 | }
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/dao/UserDao.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl.dao;
17 |
18 | import com.baomidou.samples.beetl.entity.User;
19 | import org.beetl.sql.mapper.BaseMapper;
20 |
21 | public interface UserDao extends BaseMapper {
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl.entity;
17 |
18 | import lombok.Data;
19 | import org.beetl.sql.annotation.entity.Table;
20 |
21 | @Data
22 | @Table(name = "t_user")
23 | public class User {
24 |
25 | private Integer id;
26 |
27 | private String name;
28 |
29 | private Integer age;
30 | }
31 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl.service;
17 |
18 |
19 | import com.baomidou.samples.beetl.entity.User;
20 |
21 | import java.util.List;
22 |
23 | public interface UserService {
24 |
25 | List selectMasterUsers();
26 |
27 | List selectSlaveUsers();
28 |
29 | List selectSlaveAnnotationUsers();
30 |
31 | void addUser(User user);
32 |
33 | void deleteUserById(Long id);
34 | }
35 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/java/com/baomidou/samples/beetl/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.beetl.service.impl;
17 |
18 |
19 | import com.baomidou.dynamic.datasource.annotation.DS;
20 | import com.baomidou.dynamic.datasource.annotation.Slave;
21 | import com.baomidou.samples.beetl.dao.UserDao;
22 | import com.baomidou.samples.beetl.entity.User;
23 | import com.baomidou.samples.beetl.service.UserService;
24 | import org.springframework.beans.factory.annotation.Autowired;
25 | import org.springframework.stereotype.Service;
26 |
27 | import java.util.List;
28 |
29 | @Service
30 | public class UserServiceImpl implements UserService {
31 |
32 | @Autowired
33 | private UserDao userDao;
34 |
35 | @Override
36 | public List selectMasterUsers() {
37 | return userDao.all();
38 | }
39 |
40 | @Override
41 | @DS("slave")
42 | public List selectSlaveUsers() {
43 | return userDao.all();
44 | }
45 |
46 | @Override
47 | @Slave
48 | public List selectSlaveAnnotationUsers() {
49 | return userDao.all();
50 | }
51 |
52 | @Override
53 | public void addUser(User user) {
54 | userDao.insert(user);
55 | }
56 |
57 | @Override
58 | public void deleteUserById(Long id) {
59 | userDao.deleteById(id);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | init:
11 | schema: db/schema.sql
12 | slave_1:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | slave_2:
18 | username: sa
19 | password: ""
20 | url: jdbc:h2:mem:test;MODE=MySQL
21 | driver-class-name: org.h2.Driver
22 | logging:
23 | level:
24 | com.baomidou: debug
25 | beetlsql:
26 | sqlManagers: sqlManager
27 | sqlManager:
28 | ds: dataSource
29 | basePackage: com.baomidou.samples.beetl
30 | daoSuffix: Dao
--------------------------------------------------------------------------------
/orm-samples/beetlsql-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | orm-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | jdbc-template-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-validation
22 |
23 |
24 | org.springdoc
25 | springdoc-openapi-ui
26 |
27 |
28 | com.baomidou
29 | dynamic-datasource-spring-boot-starter
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-jdbc
34 |
35 |
36 | com.h2database
37 | h2
38 |
39 |
40 | org.projectlombok
41 | lombok
42 | provided
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-test
47 | test
48 |
49 |
50 |
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/java/com/baomidou/samples/jdbc/JdbcTemplateApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.jdbc;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 | import org.springframework.context.annotation.Bean;
22 | import org.springframework.jdbc.core.JdbcTemplate;
23 |
24 | import javax.sql.DataSource;
25 |
26 | @Slf4j
27 | @SpringBootApplication
28 | public class JdbcTemplateApplication {
29 |
30 | public static void main(String[] args) {
31 | SpringApplication.run(JdbcTemplateApplication.class, args);
32 | log.info("open http://localhost:8080/swagger-ui.html");
33 | }
34 |
35 | @Bean
36 | public JdbcTemplate jdbcTemplate(DataSource dataSource) {
37 | return new JdbcTemplate(dataSource);
38 | }
39 | }
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/java/com/baomidou/samples/jdbc/controller/UserController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.jdbc.controller;
17 |
18 |
19 | import com.baomidou.samples.jdbc.entity.User;
20 | import com.baomidou.samples.jdbc.service.UserService;
21 | import lombok.AllArgsConstructor;
22 | import org.springframework.web.bind.annotation.*;
23 |
24 | import java.util.List;
25 | import java.util.Random;
26 |
27 | @RestController
28 | @AllArgsConstructor
29 | @RequestMapping("/users")
30 | public class UserController {
31 |
32 | private static final Random RANDOM = new Random();
33 | private final UserService userService;
34 |
35 | @GetMapping("master")
36 | public List masterUsers() {
37 | return userService.selectMasterUsers();
38 | }
39 |
40 | @GetMapping("slave")
41 | public List slaveUsers() {
42 | return userService.selectSlaveUsers();
43 | }
44 |
45 | @PostMapping
46 | public User addUser() {
47 | User user = new User();
48 | user.setName("测试用户" + RANDOM.nextInt());
49 | user.setAge(RANDOM.nextInt(100));
50 | userService.addUser(user);
51 | return user;
52 | }
53 |
54 | @DeleteMapping("{id}")
55 | public String deleteUser(@PathVariable Long id) {
56 | userService.deleteUserById(id);
57 | return "成功删除用户" + id;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/java/com/baomidou/samples/jdbc/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.jdbc.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/java/com/baomidou/samples/jdbc/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.jdbc.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/java/com/baomidou/samples/jdbc/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.jdbc.service;
17 |
18 | import com.baomidou.samples.jdbc.entity.User;
19 |
20 | import java.util.List;
21 |
22 | public interface UserService {
23 |
24 | List selectMasterUsers();
25 |
26 | List selectSlaveUsers();
27 |
28 | void addUser(User user);
29 |
30 | void deleteUserById(Long id);
31 | }
32 |
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | init:
11 | schema: db/schema.sql
12 | slave_1:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | slave_2:
18 | username: sa
19 | password: ""
20 | url: jdbc:h2:mem:test;MODE=MySQL
21 | driver-class-name: org.h2.Driver
22 | logging:
23 | level:
24 | com.baomidou: debug
--------------------------------------------------------------------------------
/orm-samples/jdbc-template-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/MybatisApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.mybatis.mapper")
26 | public class MybatisApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(MybatisApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/controller/UserController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.controller;
17 |
18 |
19 | import com.baomidou.samples.mybatis.entity.User;
20 | import com.baomidou.samples.mybatis.service.UserService;
21 | import lombok.AllArgsConstructor;
22 | import org.springframework.web.bind.annotation.*;
23 |
24 | import java.util.List;
25 | import java.util.Random;
26 |
27 | @RestController
28 | @AllArgsConstructor
29 | @RequestMapping("/users")
30 | public class UserController {
31 |
32 | private static final Random RANDOM = new Random();
33 | private final UserService userService;
34 |
35 | @GetMapping("master")
36 | public List masterUsers() {
37 | return userService.selectMasterUsers();
38 | }
39 |
40 | @GetMapping("slave")
41 | public List slaveUsers() {
42 | return userService.selectSlaveUsers();
43 | }
44 |
45 | @PostMapping
46 | public User addUser() {
47 | User user = new User();
48 | user.setName("测试用户" + RANDOM.nextInt());
49 | user.setAge(RANDOM.nextInt(100));
50 | userService.addUser(user);
51 | return user;
52 | }
53 |
54 | @DeleteMapping("{id}")
55 | public String deleteUser(@PathVariable Long id) {
56 | userService.deleteUserById(id);
57 | return "成功删除用户" + id;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.mapper;
17 |
18 | import com.baomidou.samples.mybatis.entity.User;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 |
24 | import java.util.List;
25 |
26 | public interface UserMapper {
27 |
28 | @Select("select * from t_user")
29 | List selectUsers();
30 |
31 | @Insert("insert into t_user (name,age) values (#{name},#{age})")
32 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
33 |
34 | @Delete("delete from t_user where id = #{id}")
35 | void deleteUserById(Long id);
36 | }
37 |
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.service;
17 |
18 |
19 | import com.baomidou.samples.mybatis.entity.User;
20 |
21 | import java.util.List;
22 |
23 | public interface UserService {
24 |
25 | List selectMasterUsers();
26 |
27 | List selectSlaveUsers();
28 |
29 | void addUser(User user);
30 |
31 | void deleteUserById(Long id);
32 | }
33 |
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/java/com/baomidou/samples/mybatis/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatis.service.impl;
17 |
18 |
19 | import com.baomidou.dynamic.datasource.annotation.DS;
20 | import com.baomidou.samples.mybatis.entity.User;
21 | import com.baomidou.samples.mybatis.mapper.UserMapper;
22 | import com.baomidou.samples.mybatis.service.UserService;
23 | import org.springframework.beans.factory.annotation.Autowired;
24 | import org.springframework.stereotype.Service;
25 |
26 | import java.util.List;
27 |
28 | @Service
29 | public class UserServiceImpl implements UserService {
30 |
31 | @Autowired
32 | private UserMapper userMapper;
33 |
34 | @Override
35 | public List selectMasterUsers() {
36 | return userMapper.selectUsers();
37 | }
38 |
39 | @DS("slave")
40 | @Override
41 | public List selectSlaveUsers() {
42 | return userMapper.selectUsers();
43 | }
44 |
45 | @Override
46 | public void addUser(User user) {
47 | userMapper.addUser(user.getName(), user.getAge());
48 | }
49 |
50 | @Override
51 | public void deleteUserById(Long id) {
52 | userMapper.deleteUserById(id);
53 | }
54 | }
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | init:
11 | schema: db/schema.sql
12 | slave_1:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | slave_2:
18 | username: sa
19 | password: ""
20 | url: jdbc:h2:mem:test;MODE=MySQL
21 | driver-class-name: org.h2.Driver
22 | strategy: com.baomidou.dynamic.datasource.strategy.LoadBalanceDynamicDataSourceStrategy
23 | logging:
24 | level:
25 | com.baomidou: debug
26 | management:
27 | endpoint:
28 | shutdown:
29 | enabled: true
30 | endpoints:
31 | web:
32 | exposure:
33 | include: '*'
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/orm-samples/mybatis-sample/src/test/java/com/baomidou/mybatis/test/EncTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.mybatis.test;
17 |
18 | import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
19 |
20 | public class EncTest {
21 |
22 | public static void main(String[] args) throws Exception {
23 | String[] arr = CryptoUtils.genKeyPair(512);
24 | System.out.println("privateKey: " + arr[0]);
25 | System.out.println("publicKey: " + arr[1]);
26 | System.out.println("url: " + CryptoUtils.encrypt(arr[0], "jdbc:mysql://127.0.0.1:3306/order"));
27 | System.out.println("username: " + CryptoUtils.encrypt(arr[0], "root"));
28 | System.out.println("password: " + CryptoUtils.encrypt(arr[0], "123456"));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | orm-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 |
12 | mybatisplus3-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-validation
22 |
23 |
24 | org.springdoc
25 | springdoc-openapi-ui
26 |
27 |
28 | com.baomidou
29 | dynamic-datasource-spring-boot-starter
30 |
31 |
32 | com.baomidou
33 | mybatis-plus-boot-starter
34 | 3.5.3.2
35 |
36 |
37 | com.h2database
38 | h2
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | provided
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/java/com/baomidou/samples/mybatisplus3/MybatisPlus3Application.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatisplus3;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.mybatisplus3.mapper")
26 | public class MybatisPlus3Application {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(MybatisPlus3Application.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/java/com/baomidou/samples/mybatisplus3/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatisplus3.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
29 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/java/com/baomidou/samples/mybatisplus3/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatisplus3.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import com.baomidou.mybatisplus.annotation.TableName;
21 | import lombok.Data;
22 |
23 | @Data
24 | @TableName("t_user")
25 | public class User {
26 |
27 | @TableId(type = IdType.AUTO)
28 | private Integer id;
29 |
30 | private String name;
31 |
32 | private Integer age;
33 | }
34 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/java/com/baomidou/samples/mybatisplus3/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatisplus3.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.mybatisplus3.entity.User;
20 |
21 | public interface UserMapper extends BaseMapper {
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/java/com/baomidou/samples/mybatisplus3/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.mybatisplus3.service;
17 |
18 |
19 | import com.baomidou.mybatisplus.extension.service.IService;
20 | import com.baomidou.samples.mybatisplus3.entity.User;
21 |
22 | import java.util.List;
23 |
24 | public interface UserService extends IService {
25 |
26 | List selectMasterUsers();
27 |
28 | List selectSlaveUsers();
29 |
30 | List selectLambdaMasterUsers();
31 |
32 | List selectLambdaSlaveUsers();
33 |
34 | List selectSlaveAnnotationUsers();
35 |
36 | void addUser(User user);
37 |
38 | void deleteUserById(Long id);
39 | }
40 |
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | datasource:
5 | master:
6 | username: sa
7 | password: ""
8 | url: jdbc:h2:mem:test;MODE=MySQL
9 | driver-class-name: org.h2.Driver
10 | init:
11 | schema: db/schema.sql
12 | slave_1:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | slave_2:
18 | username: sa
19 | password: ""
20 | url: jdbc:h2:mem:test;MODE=MySQL
21 | driver-class-name: org.h2.Driver
22 | logging:
23 | level:
24 | com.baomidou: debug
--------------------------------------------------------------------------------
/orm-samples/mybatisplus3-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/orm-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 |
11 | 4.0.0
12 |
13 | orm-samples
14 | pom
15 |
16 |
17 | beetlsql-sample
18 | jdbc-template-sample
19 | mybatis-sample
20 | mybatisplus3-sample
21 |
22 |
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/SpringBoot3Application.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.sample.boot3.mapper")
26 | public class SpringBoot3Application {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(SpringBoot3Application.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html \n" +
31 | "http://localhost:8080/druid/index.html");
32 | }
33 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/config/ProjectRuntimeHintRegister.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.config;
17 |
18 | import org.springframework.aot.hint.RuntimeHints;
19 | import org.springframework.aot.hint.RuntimeHintsRegistrar;
20 | import org.springframework.context.annotation.Configuration;
21 | import org.springframework.context.annotation.ImportRuntimeHints;
22 |
23 | @Configuration(proxyBeanMethods = false)
24 | @ImportRuntimeHints(ProjectRuntimeHintRegister.class)
25 | public class ProjectRuntimeHintRegister implements RuntimeHintsRegistrar {
26 |
27 | @Override
28 | public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
29 | hints.resources()
30 | .registerPattern("db/schema.sql");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/controller/UserController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.controller;
17 |
18 |
19 | import com.baomidou.sample.boot3.entity.User;
20 | import com.baomidou.sample.boot3.service.UserService;
21 | import lombok.AllArgsConstructor;
22 | import org.springframework.web.bind.annotation.*;
23 |
24 | import java.util.List;
25 | import java.util.Random;
26 |
27 | @RestController
28 | @AllArgsConstructor
29 | @RequestMapping("/users")
30 | public class UserController {
31 |
32 | private static final Random RANDOM = new Random();
33 | private final UserService userService;
34 |
35 | @GetMapping("master")
36 | public List masterUsers() {
37 | return userService.selectMasterUsers();
38 | }
39 |
40 | @GetMapping("slave")
41 | public List slaveUsers() {
42 | return userService.selectSlaveUsers();
43 | }
44 |
45 | @PostMapping
46 | public User addUser() {
47 | User user = new User();
48 | user.setName("测试用户" + RANDOM.nextInt());
49 | user.setAge(RANDOM.nextInt(100));
50 | userService.addUser(user);
51 | return user;
52 | }
53 |
54 | @DeleteMapping("{id}")
55 | public String deleteUser(@PathVariable Long id) {
56 | userService.deleteUserById(id);
57 | return "成功删除用户" + id;
58 | }
59 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 |
23 | private Integer id;
24 |
25 | private String name;
26 |
27 | private Integer age;
28 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.mapper;
17 |
18 | import com.baomidou.sample.boot3.entity.User;
19 | import org.apache.ibatis.annotations.*;
20 |
21 | import java.util.List;
22 |
23 | @Mapper
24 | public interface UserMapper {
25 |
26 | @Select("select * from t_user")
27 | List selectUsers();
28 |
29 | @Insert("insert into t_user (name,age) values (#{name},#{age})")
30 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
31 |
32 | @Delete("delete from t_user where id = #{id}")
33 | void deleteUserById(Long id);
34 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.service;
17 |
18 |
19 | import com.baomidou.sample.boot3.entity.User;
20 |
21 | import java.util.List;
22 |
23 | public interface UserService {
24 |
25 | List selectMasterUsers();
26 |
27 | List selectSlaveUsers();
28 |
29 | void addUser(User user);
30 |
31 | void deleteUserById(Long id);
32 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/java/com/baomidou/sample/boot3/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.sample.boot3.service.impl;
17 |
18 |
19 | import com.baomidou.dynamic.datasource.annotation.Slave;
20 | import com.baomidou.sample.boot3.entity.User;
21 | import com.baomidou.sample.boot3.mapper.UserMapper;
22 | import com.baomidou.sample.boot3.service.UserService;
23 | import org.springframework.beans.factory.annotation.Autowired;
24 | import org.springframework.stereotype.Service;
25 |
26 | import java.util.List;
27 |
28 | @Service
29 | public class UserServiceImpl implements UserService {
30 |
31 | @Autowired
32 | private UserMapper userMapper;
33 |
34 | @Override
35 | public List selectMasterUsers() {
36 | return userMapper.selectUsers();
37 | }
38 |
39 | @Slave
40 | @Override
41 | public List selectSlaveUsers() {
42 | return userMapper.selectUsers();
43 | }
44 |
45 | @Override
46 | public void addUser(User user) {
47 | userMapper.addUser(user.getName(), user.getAge());
48 | }
49 |
50 | @Override
51 | public void deleteUserById(Long id) {
52 | userMapper.deleteUserById(id);
53 | }
54 | }
--------------------------------------------------------------------------------
/springboot3-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | druid:
4 | stat-view-servlet:
5 | enabled: true
6 | login-username: admin
7 | login-password: 123456
8 | dynamic:
9 | druid:
10 | validation-query: select 1
11 | datasource:
12 | master:
13 | username: sa
14 | password: ""
15 | url: jdbc:h2:mem:test;MODE=MySQL
16 | driver-class-name: org.h2.Driver
17 | init:
18 | schema: db/schema.sql
19 | slave_1:
20 | username: sa
21 | password: ""
22 | url: jdbc:h2:mem:test;MODE=MySQL
23 | driver-class-name: org.h2.Driver
24 | slave_2:
25 | username: sa
26 | password: ""
27 | url: jdbc:h2:mem:test;MODE=MySQL
28 | driver-class-name: org.h2.Driver
29 | logging:
30 | level:
31 | com.baomidou: debug
--------------------------------------------------------------------------------
/springboot3-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS t_user
2 | (
3 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
4 | name VARCHAR(30) NULL DEFAULT NULL,
5 | age INT(11) NULL DEFAULT NULL,
6 | PRIMARY KEY (id)
7 | );
--------------------------------------------------------------------------------
/third-part-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 | third-part-samples
12 | pom
13 |
14 | quartz-sample
15 | shardingsphere-sample
16 |
17 |
--------------------------------------------------------------------------------
/third-part-samples/quartz-sample/src/main/java/com/baomidou/samples/quartz/QuartzApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.quartz;
17 |
18 | import org.springframework.boot.SpringApplication;
19 | import org.springframework.boot.autoconfigure.SpringBootApplication;
20 |
21 | @SpringBootApplication
22 | public class QuartzApplication {
23 |
24 | public static void main(String[] args) {
25 | SpringApplication.run(QuartzApplication.class, args);
26 | }
27 | }
--------------------------------------------------------------------------------
/third-part-samples/quartz-sample/src/main/java/com/baomidou/samples/quartz/config/QuartzCommonAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.quartz.config;
17 |
18 | import com.baomidou.samples.quartz.job.HelloworldJob;
19 | import org.quartz.*;
20 | import org.springframework.context.annotation.Bean;
21 | import org.springframework.context.annotation.Configuration;
22 |
23 | @Configuration
24 | public class QuartzCommonAutoConfiguration {
25 |
26 | @Bean
27 | public JobDetail job() {
28 | return JobBuilder.newJob(HelloworldJob.class)
29 | .withIdentity("myJob1", "myJobGroup1")
30 | //JobDataMap可以给任务execute传递参数
31 | .usingJobData("job_param", "job_param1")
32 | .storeDurably()
33 | .build();
34 | }
35 |
36 | @Bean
37 | public Trigger myTrigger(JobDetail jobDetail) {
38 | return TriggerBuilder.newTrigger()
39 | .forJob(jobDetail)
40 | .withIdentity("myTrigger1", "myTriggerGroup1")
41 | .usingJobData("job_trigger_param", "job_trigger_param1")
42 | .startNow()
43 | .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ? *"))
44 | .build();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/third-part-samples/quartz-sample/src/main/java/com/baomidou/samples/quartz/job/HelloworldJob.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.quartz.job;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.quartz.JobExecutionContext;
20 | import org.quartz.JobExecutionException;
21 | import org.springframework.scheduling.quartz.QuartzJobBean;
22 |
23 | @Slf4j
24 | public class HelloworldJob extends QuartzJobBean {
25 |
26 | private static int time = 0;
27 |
28 | @Override
29 | protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
30 | log.info("Hello world!:" + jobExecutionContext.getJobDetail().getKey() + "-" + (time++));
31 | }
32 | }
--------------------------------------------------------------------------------
/third-part-samples/quartz-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource: #独立quartz配置
3 | username: root
4 | password: 123456
5 | url: jdbc:mysql://110.40.253.205:3306/quartz
6 | driver-class-name: com.mysql.cj.jdbc.Driver
7 | dynamic:
8 | # enabled: false 如果没有建表可以先打开这个配置禁用多数据源,自动建quartz表
9 | datasource:
10 | master:
11 | username: sa
12 | password: ""
13 | url: jdbc:h2:mem:test;MODE=MySQL
14 | driver-class-name: org.h2.Driver
15 | quartz: #把quartz数据源也作为动态数据源的一个数据源
16 | username: root
17 | password: 123456
18 | url: jdbc:mysql://110.40.253.205:3306/quartz
19 | driver-class-name: com.mysql.cj.jdbc.Driver
20 | quartz:
21 | job-store-type: jdbc
22 | jdbc:
23 | initialize-schema: always
24 |
25 |
26 | logging:
27 | level:
28 | com.baomidou: debug
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | third-part-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 | 4.0.0
11 | shardingsphere-sample
12 | pom
13 |
14 | shardingsphere-jdbc-5.x-spring-sample
15 | shardingsphere-jdbc-4.x-spring-sample
16 | shardingsphere-jdbc-5.x-core-sample
17 |
18 |
19 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/ShardingSphereApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring;
17 |
18 | import org.mybatis.spring.annotation.MapperScan;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @SpringBootApplication
23 | @MapperScan("com.baomidou.samples.shardingsphere.jdbc.v4.spring.mapper")
24 | public class ShardingSphereApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(ShardingSphereApplication.class, args);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/controller/TOrderController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.controller;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity.TOrder;
19 | import com.baomidou.samples.shardingsphere.jdbc.v4.spring.service.TOrderService;
20 | import org.springframework.web.bind.annotation.GetMapping;
21 | import org.springframework.web.bind.annotation.PostMapping;
22 | import org.springframework.web.bind.annotation.RequestMapping;
23 | import org.springframework.web.bind.annotation.RestController;
24 |
25 | import java.util.List;
26 |
27 | @RestController
28 | @RequestMapping("/t_order")
29 | public class TOrderController {
30 | private final TOrderService tOrderService;
31 |
32 | public TOrderController(TOrderService tOrderService) {
33 | this.tOrderService = tOrderService;
34 | }
35 |
36 | @GetMapping("/findAll")
37 | public List findAll() {
38 | return tOrderService.findAll();
39 | }
40 |
41 | @PostMapping("/addAll")
42 | public List addAll() {
43 | return tOrderService.addAll();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/entity/TOrder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class TOrder {
22 | private Long orderId;
23 | private String name;
24 | private Long userId;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.mapper;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity.User;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection", "UnusedReturnValue"})
28 | @Component
29 | public interface UserMapper {
30 | @Select("select * from t_user")
31 | List selectUsers();
32 |
33 | @Insert("insert into t_user (`name`,age) values (#{name},#{age})")
34 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
35 |
36 | @Delete("delete from t_user where id = #{id}")
37 | void deleteUserById(Long id);
38 | }
39 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/service/TOrderService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity.TOrder;
19 |
20 | import java.util.List;
21 |
22 | public interface TOrderService {
23 | List findAll();
24 |
25 | List addAll();
26 | }
27 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v4.spring.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v4.spring.entity.User;
19 |
20 | import java.util.List;
21 |
22 | public interface UserService {
23 | List selectUsersFromMaster();
24 |
25 | List selectUsersFromShardingSlave();
26 |
27 | void addUser(User user);
28 |
29 | void deleteUserById(Long id);
30 | }
31 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS `t_user`;
2 | DROP TABLE IF EXISTS `t_order0`;
3 | DROP TABLE IF EXISTS `t_order1`;
4 |
5 | CREATE TABLE `t_user`
6 | (
7 | `id` BIGINT PRIMARY KEY,
8 | `name` VARCHAR(255),
9 | `age` BIGINT
10 | );
11 |
12 | CREATE TABLE `t_order0`
13 | (
14 | `order_id` BIGINT PRIMARY KEY,
15 | `name` VARCHAR(255),
16 | `user_id` BIGINT
17 | );
18 |
19 | CREATE TABLE `t_order1`
20 | (
21 | `order_id` BIGINT PRIMARY KEY,
22 | `name` VARCHAR(255),
23 | `user_id` BIGINT
24 | );
25 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/ShardingSphereApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core;
17 |
18 | import org.mybatis.spring.annotation.MapperScan;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @SpringBootApplication
23 | @MapperScan("com.baomidou.samples.shardingsphere.jdbc.v5.core.mapper")
24 | public class ShardingSphereApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(ShardingSphereApplication.class, args);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/controller/TOrderController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.controller;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.TOrder;
19 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.service.TOrderService;
20 | import org.springframework.web.bind.annotation.GetMapping;
21 | import org.springframework.web.bind.annotation.PostMapping;
22 | import org.springframework.web.bind.annotation.RequestMapping;
23 | import org.springframework.web.bind.annotation.RestController;
24 |
25 | import java.util.List;
26 |
27 | @RestController
28 | @RequestMapping("/t_order")
29 | public class TOrderController {
30 | private final TOrderService tOrderService;
31 |
32 | public TOrderController(TOrderService tOrderService) {
33 | this.tOrderService = tOrderService;
34 | }
35 |
36 | @GetMapping("/findAll")
37 | public List findAll() {
38 | return tOrderService.findAll();
39 | }
40 |
41 | @PostMapping("/addAll")
42 | public List addAll() {
43 | return tOrderService.addAll();
44 | }
45 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/entity/TOrder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class TOrder {
22 | private Long orderId;
23 | private String name;
24 | private Long userId;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/TOrderMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.mapper;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.TOrder;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection", "SqlResolve", "SqlWithoutWhere", "UnusedReturnValue"})
28 | @Component
29 | public interface TOrderMapper {
30 | @Select("select * from t_order")
31 | List findAll();
32 |
33 | @Insert("insert into t_order (order_id,`name`,user_id) values (#{orderId},#{name},#{userId})")
34 | int addAll(@Param("orderId") Long orderId, @Param("name") String name, @Param("userId") Long userId);
35 |
36 | @Insert("insert into t_order (`name`,user_id) values (#{name},#{userId})")
37 | int addByNameAndUserId(@Param("name") String name, @Param("userId") Long userId);
38 |
39 | @Delete("delete from t_order where user_id = #{userId}")
40 | int deleteById(Long userId);
41 |
42 | @Delete("delete from t_order")
43 | int deleteAll();
44 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.mapper;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.User;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @Component
28 | public interface UserMapper {
29 | @Select("select * from t_user")
30 | List selectUsers();
31 |
32 | @Insert("insert into t_user (`name`,age) values (#{name},#{age})")
33 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
34 |
35 | @Delete("delete from t_user where id = #{id}")
36 | void deleteUserById(Long id);
37 | }
38 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/service/TOrderService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.TOrder;
19 |
20 | import java.util.List;
21 |
22 | public interface TOrderService {
23 | List findAll();
24 |
25 | List addAll();
26 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.User;
19 |
20 | import java.util.List;
21 |
22 | public interface UserService {
23 | List selectUsersFromMaster();
24 |
25 | List selectUsersFromShardingSlave();
26 |
27 | void addUser(User user);
28 |
29 | void deleteUserById(Long id);
30 | }
31 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/service/impl/TOrderServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.core.service.impl;
17 |
18 | import com.baomidou.dynamic.datasource.annotation.DS;
19 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.entity.TOrder;
20 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.mapper.TOrderMapper;
21 | import com.baomidou.samples.shardingsphere.jdbc.v5.core.service.TOrderService;
22 | import org.springframework.stereotype.Service;
23 |
24 | import java.util.List;
25 | import java.util.stream.IntStream;
26 |
27 | @Service
28 | public class TOrderServiceImpl implements TOrderService {
29 | private final TOrderMapper tOrderMapper;
30 |
31 | public TOrderServiceImpl(TOrderMapper tOrderMapper) {
32 | this.tOrderMapper = tOrderMapper;
33 | }
34 |
35 | @Override
36 | @DS("shardingSphere")
37 | public List findAll() {
38 | return tOrderMapper.findAll();
39 | }
40 |
41 | @Override
42 | @DS("shardingSphere")
43 | public List addAll() {
44 | IntStream.range(0, 5)
45 | .forEach(i -> tOrderMapper.addByNameAndUserId("测试" + i, (long) i));
46 | return tOrderMapper.findAll();
47 | }
48 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | # dynamic-datasource 配置
3 | datasource:
4 | dynamic:
5 | datasource:
6 | master:
7 | username: sa
8 | password: ""
9 | url: jdbc:h2:mem:baomidou_master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
10 | driver-class-name: org.h2.Driver
11 | test:
12 | username: sa
13 | password: ""
14 | url: jdbc:h2:mem:baomidou_test;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
15 | driver-class-name: org.h2.Driver
16 | shardingSphere:
17 | url: jdbc:shardingsphere:classpath:config.yaml
18 | driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
19 | logging:
20 | level:
21 | com.baomidou: debug
22 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/config.yaml:
--------------------------------------------------------------------------------
1 | dataSources:
2 | shardingmaster:
3 | dataSourceClassName: com.zaxxer.hikari.HikariDataSource
4 | driverClassName: org.h2.Driver
5 | jdbcUrl: jdbc:h2:mem:master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
6 | username: sa
7 | password:
8 | shardingslave0:
9 | dataSourceClassName: com.zaxxer.hikari.HikariDataSource
10 | driverClassName: org.h2.Driver
11 | jdbcUrl: jdbc:h2:mem:slave1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
12 | username: sa
13 | password:
14 | shardingslave1:
15 | dataSourceClassName: com.zaxxer.hikari.HikariDataSource
16 | driverClassName: org.h2.Driver
17 | jdbcUrl: jdbc:h2:mem:slave2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
18 | username: sa
19 | password:
20 |
21 | rules:
22 | - !SHARDING
23 | tables:
24 | t_order:
25 | actualDataNodes: shardingmaster.t_order$->{0..1}
26 | tableStrategy:
27 | standard:
28 | shardingColumn: order_id
29 | shardingAlgorithmName: baomidou_inline
30 | keyGenerateStrategy:
31 | column: order_id
32 | keyGeneratorName: baomidou_snowflake
33 | shardingAlgorithms:
34 | baomidou_inline:
35 | type: INLINE
36 | props:
37 | # TODO Affected by https://github.com/apache/shardingsphere/issues/27955 .
38 | algorithm-expression: t_order$->{ORDER_ID % 2}
39 | allow-range-query-with-inline-sharding: true
40 | keyGenerators:
41 | baomidou_snowflake:
42 | type: SNOWFLAKE
43 | - !READWRITE_SPLITTING
44 | dataSourceGroups:
45 | baomidou_readwrite_data_sources:
46 | writeDataSourceName: shardingmaster
47 | readDataSourceNames:
48 | - shardingslave0
49 | - shardingslave1
50 | loadBalancerName: baomidou_load_balance_algorithm
51 | loadBalancers:
52 | baomidou_load_balance_algorithm:
53 | type: ROUND_ROBIN
54 | - !SINGLE
55 | tables:
56 | - "*.*"
57 |
58 | props:
59 | sql-show: true
60 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS `t_user`;
2 | DROP TABLE IF EXISTS `t_order0`;
3 | DROP TABLE IF EXISTS `t_order1`;
4 |
5 | CREATE TABLE `t_user`
6 | (
7 | `id` BIGINT PRIMARY KEY,
8 | `name` VARCHAR(255),
9 | `age` BIGINT
10 | );
11 |
12 | CREATE TABLE `t_order0`
13 | (
14 | `order_id` BIGINT PRIMARY KEY,
15 | `name` VARCHAR(255),
16 | `user_id` BIGINT
17 | );
18 |
19 | CREATE TABLE `t_order1`
20 | (
21 | `order_id` BIGINT PRIMARY KEY,
22 | `name` VARCHAR(255),
23 | `user_id` BIGINT
24 | );
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/ShardingSphereApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring;
17 |
18 | import org.mybatis.spring.annotation.MapperScan;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 |
22 | @SpringBootApplication
23 | @MapperScan("com.baomidou.samples.shardingsphere.jdbc.v5.spring.mapper")
24 | public class ShardingSphereApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(ShardingSphereApplication.class, args);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/controller/TOrderController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.controller;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity.TOrder;
19 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.service.TOrderService;
20 | import org.springframework.web.bind.annotation.GetMapping;
21 | import org.springframework.web.bind.annotation.PostMapping;
22 | import org.springframework.web.bind.annotation.RequestMapping;
23 | import org.springframework.web.bind.annotation.RestController;
24 |
25 | import java.util.List;
26 |
27 | @RestController
28 | @RequestMapping("/t_order")
29 | public class TOrderController {
30 | private final TOrderService tOrderService;
31 |
32 | public TOrderController(TOrderService tOrderService) {
33 | this.tOrderService = tOrderService;
34 | }
35 |
36 | @GetMapping("/findAll")
37 | public List findAll() {
38 | return tOrderService.findAll();
39 | }
40 |
41 | @PostMapping("/addAll")
42 | public List addAll() {
43 | return tOrderService.addAll();
44 | }
45 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/dto/UserDto.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.dto;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class UserDto {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/entity/TOrder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class TOrder {
22 | private Long orderId;
23 | private String name;
24 | private Long userId;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/entity/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity;
17 |
18 | import lombok.Data;
19 |
20 | @Data
21 | public class User {
22 | private Integer id;
23 | private String name;
24 | private Integer age;
25 | }
26 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/TOrderMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.mapper;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity.TOrder;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @Component
28 | @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection", "SqlResolve", "SqlWithoutWhere", "UnusedReturnValue"})
29 | public interface TOrderMapper {
30 | @Select("select * from t_order")
31 | List findAll();
32 |
33 | @Insert("insert into t_order (order_id,`name`,user_id) values (#{orderId},#{name},#{userId})")
34 | int addAll(@Param("orderId") Long orderId, @Param("name") String name, @Param("userId") Long userId);
35 |
36 | @Insert("insert into t_order (`name`,user_id) values (#{name},#{userId})")
37 | int addByNameAndUserId(@Param("name") String name, @Param("userId") Long userId);
38 |
39 | @Delete("delete from t_order where user_id = #{userId}")
40 | int deleteById(Long userId);
41 |
42 | @Delete("delete from t_order")
43 | int deleteAll();
44 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.mapper;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity.User;
19 | import org.apache.ibatis.annotations.Delete;
20 | import org.apache.ibatis.annotations.Insert;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.Select;
23 | import org.springframework.stereotype.Component;
24 |
25 | import java.util.List;
26 |
27 | @Component
28 | public interface UserMapper {
29 | @Select("select * from t_user")
30 | List selectUsers();
31 |
32 | @Insert("insert into t_user (`name`,age) values (#{name},#{age})")
33 | boolean addUser(@Param("name") String name, @Param("age") Integer age);
34 |
35 | @Delete("delete from t_user where id = #{id}")
36 | void deleteUserById(Long id);
37 | }
38 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/TOrderService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity.TOrder;
19 |
20 | import java.util.List;
21 |
22 | public interface TOrderService {
23 | List findAll();
24 |
25 | List addAll();
26 | }
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.shardingsphere.jdbc.v5.spring.service;
17 |
18 | import com.baomidou.samples.shardingsphere.jdbc.v5.spring.entity.User;
19 |
20 | import java.util.List;
21 |
22 | public interface UserService {
23 | List selectUsersFromMaster();
24 |
25 | List selectUsersFromShardingSlave();
26 |
27 | void addUser(User user);
28 |
29 | void deleteUserById(Long id);
30 | }
31 |
--------------------------------------------------------------------------------
/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/db/schema.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS `t_user`;
2 | DROP TABLE IF EXISTS `t_order0`;
3 | DROP TABLE IF EXISTS `t_order1`;
4 |
5 | CREATE TABLE `t_user`
6 | (
7 | `id` BIGINT PRIMARY KEY,
8 | `name` VARCHAR(255),
9 | `age` BIGINT
10 | );
11 |
12 | CREATE TABLE `t_order0`
13 | (
14 | `order_id` BIGINT PRIMARY KEY,
15 | `name` VARCHAR(255),
16 | `user_id` BIGINT
17 | );
18 |
19 | CREATE TABLE `t_order1`
20 | (
21 | `order_id` BIGINT PRIMARY KEY,
22 | `name` VARCHAR(255),
23 | `user_id` BIGINT
24 | );
--------------------------------------------------------------------------------
/tx-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dynamic-datasource-samples
7 | com.baomidou
8 | 1.0.0
9 |
10 |
11 | 4.0.0
12 |
13 | tx-samples
14 | pom
15 |
16 |
17 | tx-local-sample
18 | tx-seata-sample
19 |
20 |
21 |
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/LocalTxApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.localtx.mapper")
26 | public class LocalTxApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(LocalTxApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/common/OrderStatus.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.common;
17 |
18 | /**
19 | * 订单状态
20 | */
21 | public enum OrderStatus {
22 | /**
23 | * INIT
24 | */
25 | INIT,
26 | /**
27 | * SUCCESS
28 | */
29 | SUCCESS,
30 | /**
31 | * FAIL
32 | */
33 | FAIL
34 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/config/GlobalExceptionHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.config;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.http.HttpStatus;
20 | import org.springframework.web.bind.annotation.ExceptionHandler;
21 | import org.springframework.web.bind.annotation.ResponseStatus;
22 | import org.springframework.web.bind.annotation.RestControllerAdvice;
23 |
24 | @Slf4j
25 | @RestControllerAdvice
26 | public class GlobalExceptionHandler {
27 |
28 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
29 | @ExceptionHandler(RuntimeException.class)
30 | public String runtimeException(RuntimeException e) {
31 | return e.getMessage();
32 | }
33 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/dto/PlaceOrderRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class PlaceOrderRequest {
28 |
29 | private Long userId;
30 |
31 | private Long productId;
32 |
33 | private Integer amount;
34 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/dto/ReduceBalanceRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class ReduceBalanceRequest {
28 |
29 | private Long userId;
30 |
31 | private Integer price;
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/dto/ReduceStockRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class ReduceStockRequest {
28 |
29 | private Long productId;
30 |
31 | private Integer amount;
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/entity/Account.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import lombok.Builder;
21 | import lombok.Data;
22 |
23 | import java.util.Date;
24 |
25 | @Data
26 | @Builder
27 | public class Account {
28 |
29 | @TableId(type = IdType.AUTO)
30 | private Long id;
31 |
32 | /**
33 | * 余额
34 | */
35 | private Double balance;
36 |
37 | private Date lastUpdateTime;
38 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/entity/Order.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import com.baomidou.mybatisplus.annotation.TableName;
21 | import com.baomidou.samples.localtx.common.OrderStatus;
22 | import lombok.Builder;
23 | import lombok.Data;
24 |
25 | @Builder
26 | @Data
27 | @TableName("p_order")
28 | public class Order {
29 |
30 | @TableId(type = IdType.AUTO)
31 | private Integer id;
32 |
33 | /**
34 | * 用户ID
35 | */
36 | private Long userId;
37 | /**
38 | * 商品ID
39 | */
40 | private Long productId;
41 | /**
42 | * 订单状态
43 | */
44 | private OrderStatus status;
45 | /**
46 | * 数量
47 | */
48 | private Integer amount;
49 | /**
50 | * 总金额
51 | */
52 | private Double totalPrice;
53 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/entity/Product.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import lombok.Builder;
21 | import lombok.Data;
22 |
23 | import java.util.Date;
24 |
25 | @Data
26 | @Builder
27 | public class Product {
28 |
29 | @TableId(type = IdType.AUTO)
30 | private Integer id;
31 | /**
32 | * 价格
33 | */
34 | private Double price;
35 | /**
36 | * 库存
37 | */
38 | private Integer stock;
39 |
40 | private Date lastUpdateTime;
41 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/mapper/AccountMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.localtx.entity.Account;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface AccountMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/mapper/OrderMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.localtx.entity.Order;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface OrderMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/java/com/baomidou/samples/localtx/mapper/ProductMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.localtx.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.localtx.entity.Product;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface ProductMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | dynamic:
4 | primary: order
5 | datasource:
6 | order:
7 | username: postgres
8 | password: nknUPjQsY2uz6WST
9 | url: jdbc:postgresql://110.40.253.205:5432/local
10 | hikari:
11 | schema: local_order
12 | init:
13 | schema: classpath:db/postgresql/schema-order.sql
14 | account:
15 | username: postgres
16 | password: nknUPjQsY2uz6WST
17 | url: jdbc:postgresql://110.40.253.205:5432/local
18 | hikari:
19 | schema: local_account
20 | init:
21 | schema: classpath:db/postgresql/schema-account.sql
22 | product:
23 | username: postgres
24 | password: nknUPjQsY2uz6WST
25 | url: jdbc:postgresql://110.40.253.205:5432/local
26 | hikari:
27 | schema: local_product
28 | init:
29 | schema: classpath:db/postgresql/schema-product.sql
30 | logging:
31 | level:
32 | com.baomidou: debug
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/resources/db/postgresql/schema-account.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS account;
2 | CREATE TABLE account
3 | (
4 | id SERIAL PRIMARY KEY,
5 | balance DOUBLE PRECISION DEFAULT NULL,
6 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7 | );
8 |
9 | insert into account (id, balance)
10 | VALUES (1, 50);
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/resources/db/postgresql/schema-order.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS p_order;
2 | CREATE TABLE p_order
3 | (
4 | id SERIAL PRIMARY KEY,
5 | user_id BIGINT DEFAULT NULL,
6 | product_id BIGINT DEFAULT NULL,
7 | amount BIGINT DEFAULT NULL,
8 | total_price DOUBLE PRECISION DEFAULT NULL,
9 | status VARCHAR(100) DEFAULT NULL,
10 | add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
12 | );
--------------------------------------------------------------------------------
/tx-samples/tx-local-sample/src/main/resources/db/postgresql/schema-product.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS product;
2 | CREATE TABLE product
3 | (
4 | id SERIAL PRIMARY KEY,
5 | price DOUBLE PRECISION DEFAULT NULL,
6 | stock BIGINT DEFAULT NULL,
7 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
8 | );
9 |
10 | insert into product (id, price, stock)
11 | VALUES (1, 10, 20);
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/README.md:
--------------------------------------------------------------------------------
1 | docker run --name mysql -v /etc/localtime:/etc/localtime -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.30
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/SeataApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.mybatis.spring.annotation.MapperScan;
20 | import org.springframework.boot.SpringApplication;
21 | import org.springframework.boot.autoconfigure.SpringBootApplication;
22 |
23 | @Slf4j
24 | @SpringBootApplication
25 | @MapperScan("com.baomidou.samples.seata.mapper")
26 | public class SeataApplication {
27 |
28 | public static void main(String[] args) {
29 | SpringApplication.run(SeataApplication.class, args);
30 | log.info("open http://localhost:8080/swagger-ui.html");
31 | }
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/common/OrderStatus.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.common;
17 |
18 | /**
19 | * 订单状态
20 | */
21 | public enum OrderStatus {
22 | /**
23 | * INIT
24 | */
25 | INIT,
26 | /**
27 | * SUCCESS
28 | */
29 | SUCCESS,
30 | /**
31 | * FAIL
32 | */
33 | FAIL
34 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/config/GlobalExceptionHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.config;
17 |
18 | import lombok.extern.slf4j.Slf4j;
19 | import org.springframework.http.HttpStatus;
20 | import org.springframework.web.bind.annotation.ExceptionHandler;
21 | import org.springframework.web.bind.annotation.ResponseStatus;
22 | import org.springframework.web.bind.annotation.RestControllerAdvice;
23 |
24 | @Slf4j
25 | @RestControllerAdvice
26 | public class GlobalExceptionHandler {
27 |
28 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
29 | @ExceptionHandler(RuntimeException.class)
30 | public String runtimeException(RuntimeException e) {
31 | return e.getMessage();
32 | }
33 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/dto/PlaceOrderRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class PlaceOrderRequest {
28 |
29 | private Long userId;
30 |
31 | private Long productId;
32 |
33 | private Integer amount;
34 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/dto/ReduceBalanceRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class ReduceBalanceRequest {
28 |
29 | private Long userId;
30 |
31 | private Integer price;
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/dto/ReduceStockRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.dto;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @Builder
25 | @AllArgsConstructor
26 | @NoArgsConstructor
27 | public class ReduceStockRequest {
28 |
29 | private Long productId;
30 |
31 | private Integer amount;
32 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/entity/Account.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import lombok.Builder;
21 | import lombok.Data;
22 |
23 | import java.util.Date;
24 |
25 | @Data
26 | @Builder
27 | public class Account {
28 |
29 | @TableId(type = IdType.AUTO)
30 | private Long id;
31 |
32 | /**
33 | * 余额
34 | */
35 | private Double balance;
36 |
37 | private Date lastUpdateTime;
38 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/entity/Order.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import com.baomidou.mybatisplus.annotation.TableName;
21 | import com.baomidou.samples.seata.common.OrderStatus;
22 | import lombok.Builder;
23 | import lombok.Data;
24 |
25 | @Builder
26 | @Data
27 | @TableName("p_order")
28 | public class Order {
29 |
30 | @TableId(type = IdType.AUTO)
31 | private Integer id;
32 |
33 | /**
34 | * 用户ID
35 | */
36 | private Long userId;
37 | /**
38 | * 商品ID
39 | */
40 | private Long productId;
41 | /**
42 | * 订单状态
43 | */
44 | private OrderStatus status;
45 | /**
46 | * 数量
47 | */
48 | private Integer amount;
49 | /**
50 | * 总金额
51 | */
52 | private Double totalPrice;
53 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/entity/Product.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.IdType;
19 | import com.baomidou.mybatisplus.annotation.TableId;
20 | import lombok.Builder;
21 | import lombok.Data;
22 |
23 | import java.util.Date;
24 |
25 | @Data
26 | @Builder
27 | public class Product {
28 |
29 | @TableId(type = IdType.AUTO)
30 | private Integer id;
31 | /**
32 | * 价格
33 | */
34 | private Double price;
35 | /**
36 | * 库存
37 | */
38 | private Integer stock;
39 |
40 | private Date lastUpdateTime;
41 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/mapper/AccountMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.seata.entity.Account;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface AccountMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/mapper/OrderMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.seata.entity.Order;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface OrderMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/mapper/ProductMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.mapper;
17 |
18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
19 | import com.baomidou.samples.seata.entity.Product;
20 | import org.apache.ibatis.annotations.Mapper;
21 |
22 | @Mapper
23 | public interface ProductMapper extends BaseMapper {
24 |
25 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/service/AccountService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.service;
17 |
18 | public interface AccountService {
19 |
20 | /**
21 | * @param userId 用户 ID
22 | * @param price 扣减金额
23 | */
24 | void reduceBalance(Long userId, Double price);
25 |
26 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/service/OrderService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.service;
17 |
18 | import com.baomidou.samples.seata.dto.PlaceOrderRequest;
19 |
20 | public interface OrderService {
21 |
22 | /**
23 | * 下单
24 | *
25 | * @param placeOrderRequest 订单请求参数
26 | */
27 | void placeOrder(PlaceOrderRequest placeOrderRequest);
28 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/java/com/baomidou/samples/seata/service/ProductService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © ${project.inceptionYear} organization baomidou
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baomidou.samples.seata.service;
17 |
18 | public interface ProductService {
19 |
20 | /**
21 | * 扣减库存
22 | *
23 | * @param productId 商品 ID
24 | * @param amount 扣减数量
25 | * @return 商品总价
26 | */
27 | Double reduceStock(Long productId, Integer amount);
28 | }
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/application-mysql.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | application:
3 | name: dynamic
4 | datasource:
5 | dynamic:
6 | primary: order
7 | strict: true
8 | seata: true #开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭
9 | seata-mode: AT #支持XA及AT模式,默认AT
10 | datasource:
11 | order:
12 | username: root
13 | password: 123456
14 | url: jdbc:mysql://110.40.253.205:3306/seata_order
15 | hikari:
16 | init:
17 | schema: classpath:db/mysql/schema-order.sql
18 | account:
19 | username: root
20 | password: 123456
21 | url: jdbc:mysql://110.40.253.205:3306/seata_account
22 | hikari:
23 | init:
24 | schema: classpath:db/mysql/schema-account.sql
25 | product:
26 | username: root
27 | password: 123456
28 | url: jdbc:mysql://110.40.253.205:3306/seata_product
29 | init:
30 | schema: classpath:db/mysql/schema-product.sql
31 | test:
32 | username: sa
33 | password: ""
34 | url: jdbc:h2:mem:test;MODE=MySQL
35 | driver-class-name: org.h2.Driver
36 | seata: false #这个数据源不需要seata
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/application-postgresql.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | application:
3 | name: dynamic
4 | datasource:
5 | dynamic:
6 | primary: order
7 | strict: true
8 | seata: true #开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭
9 | seata-mode: AT #支持XA及AT模式,默认AT
10 | datasource:
11 | order:
12 | username: postgres
13 | password: nknUPjQsY2uz6WST
14 | url: jdbc:postgresql://110.40.253.205:5432/seata
15 | hikari:
16 | schema: seata_order
17 | init:
18 | schema: classpath:db/postgresql/schema-order.sql
19 | account:
20 | username: postgres
21 | password: nknUPjQsY2uz6WST
22 | url: jdbc:postgresql://110.40.253.205:5432/seata
23 | hikari:
24 | schema: seata_account
25 | init:
26 | schema: classpath:db/postgresql/schema-account.sql
27 | product:
28 | username: postgres
29 | password: nknUPjQsY2uz6WST
30 | url: jdbc:postgresql://110.40.253.205:5432/seata
31 | hikari:
32 | schema: seata_product
33 | init:
34 | schema: classpath:db/postgresql/schema-product.sql
35 | test:
36 | username: sa
37 | password: ""
38 | url: jdbc:h2:mem:test;MODE=MySQL
39 | driver-class-name: org.h2.Driver
40 | seata: false #这个数据源不需要seata
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | profiles:
3 | active: postgresql
4 | seata:
5 | enabled: true
6 | application-id: applicationName
7 | tx-service-group: my_test_tx_group
8 | #一定要是false
9 | enable-auto-data-source-proxy: false
10 | service:
11 | vgroup-mapping:
12 | #key与上面的tx-service-group的值对应
13 | my_test_tx_group: default
14 | grouplist:
15 | #seata-server地址仅file注册中心需要
16 | default: 110.40.253.205:8091
17 | config:
18 | type: file
19 | registry:
20 | type: file
21 | logging:
22 | level:
23 | org.springframework.jdbc.datasource: debug
24 | com.baomidou.dynamic: debug
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/mysql/schema-account.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS account;
2 | CREATE TABLE account
3 | (
4 | id INT(11) NOT NULL AUTO_INCREMENT,
5 | balance DOUBLE DEFAULT NULL,
6 | last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
7 | PRIMARY KEY (id)
8 | ) AUTO_INCREMENT = 1
9 | DEFAULT CHARSET = utf8;
10 |
11 |
12 | insert into account (id, balance)
13 | VALUES (1, 50);
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/mysql/schema-order.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS p_order;
2 | CREATE TABLE p_order
3 | (
4 | id INT(11) NOT NULL AUTO_INCREMENT,
5 | user_id INT(11) DEFAULT NULL,
6 | product_id INT(11) DEFAULT NULL,
7 | amount INT(11) DEFAULT NULL,
8 | total_price DOUBLE DEFAULT NULL,
9 | status VARCHAR(100) DEFAULT NULL,
10 | add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
11 | last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
12 | PRIMARY KEY (id)
13 | ) AUTO_INCREMENT = 1
14 | DEFAULT CHARSET = utf8;
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/mysql/schema-product.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS product;
2 | CREATE TABLE product
3 | (
4 | id INT(11) NOT NULL AUTO_INCREMENT,
5 | price DOUBLE DEFAULT NULL,
6 | stock INT(11) DEFAULT NULL,
7 | last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
8 | PRIMARY KEY (id)
9 | ) AUTO_INCREMENT = 1
10 | DEFAULT CHARSET = utf8;
11 |
12 | insert into product (id, price, stock)
13 | VALUES (1, 10, 20);
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/mysql/undo_log.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS undo_log;
2 | CREATE TABLE undo_log
3 | (
4 | id BIGINT(20) NOT NULL AUTO_INCREMENT,
5 | branch_id BIGINT(20) NOT NULL,
6 | xid VARCHAR(100) NOT NULL,
7 | context VARCHAR(128) NOT NULL,
8 | rollback_info LONGBLOB NOT NULL,
9 | log_status INT(11) NOT NULL,
10 | log_created DATETIME NOT NULL,
11 | log_modified DATETIME NOT NULL,
12 | PRIMARY KEY (id),
13 | UNIQUE KEY ux_undo_log (xid, branch_id)
14 | ) AUTO_INCREMENT = 1
15 | DEFAULT CHARSET = utf8;
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/postgresql/schema-account.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS account;
2 | CREATE TABLE account
3 | (
4 | id SERIAL PRIMARY KEY,
5 | balance DOUBLE PRECISION DEFAULT NULL,
6 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7 | );
8 |
9 | insert into account (id, balance)
10 | VALUES (1, 50);
11 |
12 | DROP TABLE IF EXISTS undo_log;
13 | CREATE TABLE undo_log
14 | (
15 | id SERIAL PRIMARY KEY,
16 | branch_id BIGINT NOT NULL,
17 | xid VARCHAR(100) NOT NULL,
18 | context VARCHAR(128) NOT NULL,
19 | rollback_info BYTEA NOT NULL,
20 | log_status INT NOT NULL,
21 | log_created TIMESTAMP NOT NULL,
22 | log_modified TIMESTAMP NOT NULL,
23 | CONSTRAINT ux_undo_log UNIQUE (xid, branch_id)
24 | );
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/postgresql/schema-order.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS p_order;
2 | CREATE TABLE p_order
3 | (
4 | id SERIAL PRIMARY KEY,
5 | user_id BIGINT DEFAULT NULL,
6 | product_id BIGINT DEFAULT NULL,
7 | amount BIGINT DEFAULT NULL,
8 | total_price DOUBLE PRECISION DEFAULT NULL,
9 | status VARCHAR(100) DEFAULT NULL,
10 | add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
12 | );
13 |
14 | DROP TABLE IF EXISTS undo_log;
15 | CREATE TABLE undo_log
16 | (
17 | id SERIAL PRIMARY KEY,
18 | branch_id BIGINT NOT NULL,
19 | xid VARCHAR(100) NOT NULL,
20 | context VARCHAR(128) NOT NULL,
21 | rollback_info BYTEA NOT NULL,
22 | log_status INT NOT NULL,
23 | log_created TIMESTAMP NOT NULL,
24 | log_modified TIMESTAMP NOT NULL,
25 | CONSTRAINT ux_undo_log UNIQUE (xid, branch_id)
26 | );
--------------------------------------------------------------------------------
/tx-samples/tx-seata-sample/src/main/resources/db/postgresql/schema-product.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS product;
2 | CREATE TABLE product
3 | (
4 | id SERIAL PRIMARY KEY,
5 | price DOUBLE PRECISION DEFAULT NULL,
6 | stock BIGINT DEFAULT NULL,
7 | last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
8 | );
9 |
10 | insert into product (id, price, stock)
11 | VALUES (1, 10, 20);
12 |
13 | DROP TABLE IF EXISTS undo_log;
14 | CREATE TABLE undo_log
15 | (
16 | id SERIAL PRIMARY KEY,
17 | branch_id BIGINT NOT NULL,
18 | xid VARCHAR(100) NOT NULL,
19 | context VARCHAR(128) NOT NULL,
20 | rollback_info BYTEA NOT NULL,
21 | log_status INT NOT NULL,
22 | log_created TIMESTAMP NOT NULL,
23 | log_modified TIMESTAMP NOT NULL,
24 | CONSTRAINT ux_undo_log UNIQUE (xid, branch_id)
25 | );
--------------------------------------------------------------------------------