springboot sharingJdbc jpa 分库分表

 1.整合所需要的包


     io.shardingsphere
     sharding-jdbc-spring-boot-starter
     3.1.0


     org.springframework.boot
     spring-boot-starter-data-jpa


    mysql
    mysql-connector-java


    com.alibaba
    druid-spring-boot-starter
    1.1.10

2.这里采用的yml配置的形式

sharding:
  jdbc:
    dataSource:
      names: ds0,ds1
      ds0: #数据库1
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/demo0?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
      ds1: #数据库2
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/demo1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
    config:
      sharding:
        tables:
          demo_user:#表名
            #分表策略
            actual-data-nodes: ds${0..1}.demo_user${0..1}
            table-strategy:
              inline:
                shardingColumn: id
                algorithm-expression: demo_user${id% 2}
            #分库策略
            database-strategy: 
              inline:
                sharding-column: id
                algorithm-expression: ds${id% 2}
            actual-data-nodes: ds${0..1}.demo_user
      #广播表
      broadcast-tables: demo_new  
      props: #输出日志 默认false
        sql:
          show: true
spring:
  jpa: #jpa数据操作
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
    show-sql: true
    database: mysql
    generate-ddl: true
  main:
    allow-bean-definition-overriding: true

 

你可能感兴趣的:(Java)