Sharding-sphere 3.X integration with spring and MyBatis (sub-Library sub-table) demo

Source: Internet
Author: User

Recently in this sharding-sphere, the company's internal sub-database sub-table is in this business code on the logical sub-database sub-table, but this is always bad, but also research several sub-database sub-table middleware, Mycat, NetEase Cetus, Ali DRDs, which is a strong background, Large companies after a lot of actual combat, maturity is very high, and the framework Sharding-sphere relatively lightweight, more recent fire, it is in the form of JAR package service, can seamlessly connect ORM framework, do not need additional deployment, do not need to rely on, operations can not need to change, Many people take sharding-sphere as an enhanced version of the JDBC driver, and the migration code is not that complicated. For giant companies, the company will have its own development of components, middleware, for the entire company to use, for small and medium-sized companies, the need to use open source middleware to support the company's business development.

Sharding-sphere-examples website:
Https://github.com/growup818/spring-learning-examples

Sharding-sphere Documentation:
http://shardingsphere.io/document/cn/features/

Personal advice, or official documents, Official document examples are very full, the document is very clear, the operation is not a problem, do not search the online demo. But a little suggestion, you can look at the official website of the examples, according to the company's code framework to write a simple demo what, you can run.

SQL script:

/*navicat MySQL Data transfersource server:10.1.90.25-devsource Server version:50720source host:10 .1.90.25:3307source database:separate_entity_1target Server type:mysqltarget server Version:50720file Encod ing:65001date:2018-05-28 18:04:42*/set foreign_key_checks=0;--------------------------------Table structure F or t_order_0------------------------------DROP TABLE IF EXISTS ' t_order_0 '; CREATE TABLE ' t_order_0 ' (' order_id ' bigint () not null auto_increment, ' user_id ' int (one) not null, ' status ' varchar ( Default NULL, PRIMARY KEY (' order_id ')) Engine=innodb auto_increment=1 default Charset=utf8;----------------------- ---------table structure for t_order_1------------------------------DROP table IF EXISTS ' t_order_1 '; CREATE TABLE ' t_order_1 ' (' order_id ' bigint () not null auto_increment, ' user_id ' int (one) not null, ' status ' varchar ( Default NULL, PRIMARY KEY (' order_id ')) engine=innodb default Charset=utf8;--------------------------------table structure for t_order_item_0------------------------------DROP table IF EXISTS ' T_order_ Item_0 '; CREATE TABLE ' t_order_item_0 ' (' order_item_id ' bigint () not NULL, ' order_id ' bigint () DEFAULT NULL, ' user_id ' int (one) not NULL, PRIMARY KEY (' order_item_id ')) Engine=innodb auto_increment=1 DEFAULT Charset=utf8;--------------------- -----------table structure for t_order_item_1------------------------------DROP table IF EXISTS ' t_order_item_1 '; CREATE TABLE ' t_order_item_1 ' (' order_item_id ' bigint () not NULL, ' order_id ' bigint () DEFAULT NULL, ' user_id ' int ( One) not NULL, PRIMARY KEY (' order_item_id ')) Engine=innodb auto_increment=1 DEFAULT Charset=utf8;

Database.properties configuration file

sharding.jdbc.datasource.names=separate_entity_0,separate_entity_1sharding.jdbc.datasource.separate_entity_0.url=jdbc:mysql://127.0.0.1:3307/separate_entity_0sharding.jdbc.datasource.separate_entity_0.username=rootsharding.jdbc.datasource.separate_entity_0.password=sharding.jdbc.datasource.separate_entity_1.url=jdbc:mysql://127.0.0.1:3307/separate_entity_1sharding.jdbc.datasource.separate_entity_1.username=rootsharding.jdbc.datasource.separate_entity_1.password=sharding.jdbc.datasource.actual.data.nodes.order=separate_entity_$->{0..1}.t_order_$->{0..1}sharding.jdbc.datasource.actual.data.nodes.orderitem=separate_entity_$->{0..1}.t_order_item_$->{0..1}

``

Sharding-databases.xml configuration file:

<!--different business tables may have different business rules, there will be a good number of sub-libraries, the table strategy--<bean id= "Precisemodulodatabaseshardingalgorithm" class= " Com.sharding.demo.algorithm.DatabaseShardingAlgorithm "/> <bean id=" Precisemodulotableshardingalgorithm " class= "Com.sharding.demo.algorithm.TableShardingAlgorithm"/> <!--possible different business tables have different business rules, there will be a good number of sub-libraries, the sub-table strategy, this block specified- > <sharding:standard-strategy id= "databaseshardingstrategy" sharding-column= "user_id" precise-algorithm-ref= "Precisemodulodatabaseshardingalgorithm"/> <sharding:standard-strategy id= "TableShardingStrategy" Sharding-column= "order_id" precise-algorithm-ref= "Precisemodulotableshardingalgorithm"/> <sharding: Data-source id= "Shardingdatasource" > <sharding:sharding-rule data-source-names= "Separate_entity_0,separate_                     Entity_1 "> <sharding:table-rules> <sharding:table-rule logic-table=" T_order "    Actual-data-nodes= "${sharding.jdbc.datasource.actual.data.nodes.order}"                 database-strategy-ref= "Databaseshardingstrategy" table-strategy-ref= "Tableshardingstrategy"                     Generate-key-column-name= "order_id"/> <sharding:table-rule logic-table= "T_order_item" Actual-data-nodes= "${sharding.jdbc.datasource.actual.data.nodes.orderitem}" database-str ategy-ref= "Databaseshardingstrategy" table-strategy-ref= "Tableshardingstrategy" ge Nerate-key-column-name= "order_item_id"/> </sharding:table-rules> &LT;/SHARDING:SHARDING-RULE&G    T </sharding:data-source>

Sub-database sub-table:

public final class DatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {    @Override    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {        for (String each : availableTargetNames) {            if (each.endsWith(shardingValue.getValue() % 2 + "")) {                return each;            }        }        throw new UnsupportedOperationException();    }}public final class TableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {    @Override    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {        for (String each : availableTargetNames) {            if (each.endsWith(shardingValue.getValue() % 2 + "")) {                return each;            }        }        throw new UnsupportedOperationException();    }}

The specific demo is on the Githup website:
Demo:https://github.com/growup818/spring-learning-examples

Start Step:
1. Need a Tomcat
2. Direct access to Http://ip:port/SSM/demo/test after successful startup
3, the Access class is in Shardingdemocontroller this class

Sharding-sphere 3.X integration with spring and MyBatis (sub-Library sub-table) demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.