一、Mybatis-Plus配置
1. 創(chuàng)建數(shù)據(jù)源配置類
在項目的"application.yml"或"application.properties"文件中配置兩個數(shù)據(jù)源,一個用于讀操作,另一個用于寫操作。
spring:
datasource:
primary:
url: jdbc:mysql://localhost:3306/write_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: write_user
password: write_password
driver-class-name: com.mysql.cj.jdbc.Driver
secondary:
url: jdbc:mysql://localhost:3306/read_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: read_user
password: read_password
driver-class-name: com.mysql.cj.jdbc.Driver2. 創(chuàng)建Mybatis-Plus配置類
在項目的"application.yml"或"application.properties"文件中配置Mybatis-Plus的分頁插件,以支持分頁查詢。同時,配置Mybatis-Plus的SQL攔截器,以支持SQL日志輸出。
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto # 根據(jù)實際情況設置主鍵類型,如Long、Integer等
logic-delete-value: 1 # 根據(jù)實際情況設置邏輯刪除值,如0、1等
optim-cache-enable: false # 是否啟用優(yōu)化緩存,默認開啟
mapper-locations: classpath*:/*Mapper.xml # Mybatis-Plus自動掃描的Mapper文件路徑二、Mycat配置
1. 創(chuàng)建數(shù)據(jù)源配置文件
在項目的"resources"目錄下創(chuàng)建一個名為"mycat.properties"的文件,用于配置Mycat的數(shù)據(jù)源。
# Mycat數(shù)據(jù)源配置文件示例 datasources=WriteDB,ReadDB # 定義數(shù)據(jù)源名稱,多個數(shù)據(jù)源用逗號分隔 WriteDB.driverClassName=com.mysql.cj.jdbc.Driver # 定義WriteDB數(shù)據(jù)源驅動類名 WriteDB.url=jdbc:mysql://localhost:3306/write_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false # 定義WriteDB數(shù)據(jù)源連接URL WriteDB.username=write_user # 定義WriteDB數(shù)據(jù)源用戶名 WriteDB.password=write_password # 定義WriteDB數(shù)據(jù)源密碼 ReadDB.driverClassName=com.mysql.cj.jdbc.Driver # 定義ReadDB數(shù)據(jù)源驅動類名 ReadDB.url=jdbc:mysql://localhost:3306/read_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false # 定義ReadDB數(shù)據(jù)源連接URL ReadDB.username=read_user # 定義ReadDB數(shù)據(jù)源用戶名 ReadDB.password=read_password # 定義ReadDB數(shù)據(jù)源密碼
2. 在SpringBoot項目中添加Mycat依賴
在項目的pom.xml文件中添加Mycat的依賴。
<dependency> <groupId>org.mycat</groupId> <artifactId>mycat-core</artifactId> <!-- 請根據(jù)實際情況選擇合適的版本 --> <version>5.1.34</version> </dependency>
三、配置Mycat代理服務器和路由器(可選)
如果需要使用Mycat的代理服務器和路由器功能,可以在"mycat.properties"文件中進行相應的配置。具體配置方法請參考官方文檔。