使用Spring Boot与达梦数据库(DM)进行多数据源配置及MyBatis Plus集成
在现代企业级应用开发中,处理多个数据源是一个常见的需求。本文将详细介绍如何使用Spring Boot结合达梦数据库(DM),并通过MyBatis Plus来简化数据库操作,同时实现多数据源的动态切换。这不仅能够提高开发效率,还能增强系统的灵活性和可维护性。
引言
随着业务的发展,单一的数据源可能无法满足所有需求。例如,你可能需要从不同的数据库读取数据或写入数据到特定的数据库中。在这种情况下,采用多数据源配置可以提供更灵活的数据访问方式。此外,MyBatis Plus作为一个MyBatis的增强工具,提供了更多便捷的功能,如自动填充、逻辑删除等,极大地方便了开发者。
一、环境准备
在开始之前,请确保你的开发环境中已经安装并配置好了以下组件:
- JDK 1.8 或更高版本
- Maven 3.x
- Spring Boot 2.x
- 达梦数据库客户端及其JDBC驱动
二、项目依赖配置
首先,在pom.xml
文件中添加必要的依赖项:
<dependencies>
<!-- Spring Boot JDBC Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- DM JDBC Driver -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
<version>8.1.1.49</version>
</dependency>
<!-- Dynamic Datasource Starter for MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
</dependencies>
注意:请根据实际情况替换mybatis-plus-boot-starter
的版本号为最新稳定版。
三、YAML配置详解
接下来是关键部分——配置文件application.yml
,它定义了我们的多数据源信息以及MyBatis Plus的相关设置。
spring:
datasource:
dynamic:
primary: db_realname # 默认使用的数据源名称
datasource:
db_realname:
driver-class-name: dm.jdbc.driver.DmDriver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:dm://localhost:5237?schema=smartsitetest2
username: TEST
password: 123456789
druid: # Druid连接池配置
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
initial-size: 5
max-active: 50
min-idle: 5
max-wait: 80000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: true
filters: stat
db_iot:
driver-class-name: dm.jdbc.driver.DmDriver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:dm://localhost:5237?schema=smartsiteiottest
username: TEST
password: 123456789
druid: # Druid连接池配置
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
initial-size: 5
max-active: 50
min-idle: 5
max-wait: 80000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: true
filters: stat
# MyBatis Plus 配置
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
typeAliasesPackage: com.xxx.entity
global-config:
db-config:
id-type: AUTO
field-strategy: NOT_NULL
column-underline: true
logic-delete-value: -1
logic-not-delete-value: 0
banner: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
jdbc-type-for-null: 'null'
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
root: INFO
org.springframework.web.servlet.DispatcherServlet: DEBUG
file:
path: logs
四、注意事项
- 性能优化:对于生产环境,建议调整Druid连接池参数以适应实际负载。
- 安全性:避免直接在配置文件中硬编码数据库用户名和密码,考虑使用加密存储或其他安全措施。
- 异常处理:在代码中加入适当的异常处理机制,确保系统健壮性。
五、总结
通过本文的介绍,我们了解了如何在Spring Boot项目中配置多数据源,并且利用MyBatis Plus简化数据库操作。这种架构不仅提高了代码的可维护性和扩展性,也为后续功能的迭代打下了良好的基础。希望这篇文章能帮助你在未来的项目中更好地管理和利用数据库资源。如果你有任何问题或建议,欢迎留言讨论!