基础架构是springboot +mybatis-plus 实现动态数据源步骤
步骤1:pom文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.33</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.0</version> </dependency> <!-- 可选:如果使用Lombok提高代码简洁性 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
步骤二、springboot配置文件 application.yml
spring: datasource: dynamic: #设置默认的数据源或者数据源组,默认值即为master primary: master #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 strict: false datasource: master: url: jdbc:mysql://127.0.0.1:3306/farm?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver slave_1: url: jdbc:mysql://127.0.0.1:3306/from1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver slave_2: url: jdbc:mysql://127.0.0.1:3306/farm2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: # ???? global-config: db-config: # ????????mysql/oracle/h2/postgresql? db-type: mysql # ???????not_null/commented/underline_to_camel/to_lowercase? field-strategy: not_null # ?????????? capital-mode: true # ???????? column-underline: true # ??? table-prefix: # ID?????id_WORKER/AUTO id-type: auto # SQL??? logic-delete-value: 1 logic-not-delete-value: 0 logic-delete-field: deleted mapper-locations: classpath:/mapper/*.xml type-aliases-package: com.example.mulittenantdemo.domain configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
步骤3 service
public interface AppVersionService extends IService<AppVersion> { AppVersion queryById(Long id); AppVersion queryById1(Long id); AppVersion queryById2(Long id); }
步骤4、serviceImpl
@Service public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService { @Autowired private AppVersionMapper appVersionMapper; @Override @DS("slave_1") public AppVersion queryById1(Long id) { return appVersionMapper.selectById(id); } @Override @DS("slave_2") public AppVersion queryById2(Long id) { return appVersionMapper.selectById(id); } @Override @DS("primary") public AppVersion queryById(Long id) { return appVersionMapper.myqueryById111(id); } }
步骤5、mapper
public interface AppVersionMapper extends BaseMapper<AppVersion> { AppVersion myqueryById(Long id); @Select("SELECT * FROM app_version WHERE id = #{id}") AppVersion myqueryById111(@Param("id") Long id); }
步骤6、controller
@RestController @RequestMapping("/app") @Validated public class AppController { @Autowired private AppVersionService appVersionService; @GetMapping("/get/{id}") public AppVersion sayHello(@PathVariable("id") Long id) { AppVersion byId = appVersionService.queryById(id); System.out.println(byId); return byId; } @GetMapping("/get1/{id}") public AppVersion sayHello1(@PathVariable("id") Long id) { AppVersion byId = appVersionService.queryById1(id); System.out.println(byId); return byId; } @GetMapping("/get2/{id}") public AppVersion sayHello2(@PathVariable("id") Long id) { AppVersion byId = appVersionService.queryById2(id); System.out.println(byId); return (byId); } }