1.pom引入
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0.M1</version>
</dependency>
2.application.yml
server:
port: 8082
spring:
jpa:
database: MySQL
show-sql: true
properties:
hibernate:
format_sql: true
hibernate:
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
# 将下划线改为驼峰
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# springboot 2.0读写分离
sharding:
jdbc:
dataSource:
names: master,slave0
master:
type: com.alibaba.druid.pool.DruidDataSource
# driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://172.16.56.117:3306/表名?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true&serverTimezone=GMT%2B8&useSSL=false
username: root
password: 密码
slave0:
type: com.alibaba.druid.pool.DruidDataSource
# driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://172.16.56.118:3306/表名?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true&serverTimezone=GMT%2B8&useSSL=false
username: root
password: 密码
config:
masterslave:
name: ms
master-data-source-name: master
slave-data-source-names: slave0
props:
sql:
show: true