sharding-jdbc讀寫分離快速實現
阿新 • • 發佈:2020-12-26
技術標籤:java
首先專案是基於springboot框架為基礎
1.在pom依賴中匯入實現資料庫讀寫分離的sharding依賴
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
2.然後在application.yml配置檔案中加入資料庫連線,其餘相關依賴百度一下就可以了
mybatis-plus:
# 自定義xml檔案路徑
mapper-locations: classpath:/mybatis/mapper/**/*.xml
# 自定義xml檔案中用到的實體類路徑
typeAliasesPackage: com.api.springcloud.entities
configuration:
# 開啟駝峰對映
map-underscore-to-camel-case: true
cache-enabled: false
# 返回map時,true:當查詢資料為空時欄位返回為null;false:不加這個查詢資料為空時,欄位將被隱藏
call- setters-on-nulls: true
# sql日誌列印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
http:
multipart:
enabled: true
max-file-size: 20MB
max-request-size: 20MB
application:
name: microservicecloud-shop
shardingsphere:
datasource:
#主資料配置
master:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:主資料庫連線資訊
username: 主庫賬號
password: 主庫密碼
names: master,slave
#從資料庫配置
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:從資料庫連線資訊
username: 從庫賬號
password: 從庫密碼
masterslave:
master-data-source-name: master
name: ms
slave-data-source-names: slave
props:
sql:
show: false