首先项目是基于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