ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

sharding-jdbc与springboot结合使用-单库分表简单使用

2019-08-01 09:53:19  阅读:422  来源: 互联网

标签:jdbc springboot shardingsphere 单库 strategy spring sharding table


sharding-jdbc官网:

https://shardingsphere.apache.org/document/current/cn/quick-start/sharding-jdbc-quick-start/

sharding-jdbc springboot配置:

https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/

1、添加依赖

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
</dependency>
<dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
</dependency>
View Code

2、配置文件方式

spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.names=master
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.shardingsphere.datasource.master.url=jdbc:oracle:thin:@XXXX:1521:XXX
spring.shardingsphere.datasource.master.username=XXX
spring.shardingsphere.datasource.master.password=XXX

spring.shardingsphere.sharding.tables.student.actual-data-nodes=master.student_${0..1}
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_${age % 2}

#spring.shardingsphere.sharding.tables.STUDENT.table-strategy.standard.sharding-column=AGE
#spring.shardingsphere.sharding.tables.STUDENT.table-strategy.standard.precise-algorithm-class-name=com.chamc.rics.basics.test.algorithm.MyPreciseShardingAlgorithm


#spring.sharding.jdbc.config.sharding.default-table-strategy.standard.sharding-column=USER_ID
##精确分片算法,用于=和IN,实现类
#sharding.jdbc.config.sharding.default-table-strategy.standard.precise-algorithm-class-name=com.solider76.oo.service.chat.config.MyShardingConfig
##范围分片算法,用于BETWEEN,实现类
#sharding.jdbc.config.sharding.default-table-strategy.standard.range-algorithm-class-name=com.solider76.oo.service.chat.config.MyShardingConfig
View Code

3、自定义方式

  参照官网及大牛的文章:http://wuwenliang.net/categories/Sharding-JDBC/

4、异常解决

对于保存数据操作,报错。原因是配置文件中:

spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_${age % 2}

student写成了大写,出现 不能对null 取mod()操作。

 

标签:jdbc,springboot,shardingsphere,单库,strategy,spring,sharding,table
来源: https://www.cnblogs.com/shore-life/p/11274305.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有