Demand
Sub-Library by line of business ID, one library per line of business.
Sql
-- 建立112业务线库create database sharding_112 ;-- 在112业务库里建立 t_order表CREATE TABLE `t_order` ( `order_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `business_id` int(4) DEFAULT NULL, PRIMARY KEY (`order_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;-- 建立113业务线库create database sharding_113 ;-- 在113业务库里建立 t_order表CREATE TABLE `t_order` ( `order_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `business_id` int(4) DEFAULT NULL, PRIMARY KEY (`order_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
Pom.xml (using SHARDING-JDBC 2.0)
<!--https://mvnrepository.com/artifact/io.shardingjdbc/sharding-jdbc-core --<dependency> <groupId>Io.shardingjdbc</groupId> <artifactId>Sharding-jdbc-core</artifactId> <version>2.0.0.m2</version></dependency><!--https://mvnrepository.com/artifact/com.dangdang/sharding-jdbc-config-spring --<dependency> <groupId>Com.dangdang</groupId> <artifactId>Sharding-jdbc-config-spring</artifactId> <version>1.4.0</version></dependency><dependency> <groupId>Io.shardingjdbc</groupId> <artifactId>Sharding-jdbc-spring-namespace</artifactId> <version>2.0.0.m2</version></dependency>
Spring Related configurations
<beanid="ds_112"class="Com.alibaba.druid.pool.DruidDataSource"init-method="Init"destroy-method="Close"> <propertyname="Driverclassname"value="Com.mysql.jdbc.Driver"/> <propertyname="url"value="${jdbc.url_112}"></property> <propertyname="username"value="${jdbc.username_112}"></property> <propertyname="Password"value="${jdbc.password_112}"></property> <propertyname="Maxactive"value=" the"/> <propertyname="InitialSize"value="Ten"/> <propertyname="Maxwait"value="60000"/> <propertyname="Minidle"value="5"/></bean><beanid="ds_113"class="Com.alibaba.druid.pool.DruidDataSource"init-method="Init"destroy-method="Close"> <propertyname="Driverclassname"value="Com.mysql.jdbc.Driver"/> <propertyname="url"value="${jdbc.url_113}"></property> <propertyname="username"value="${jdbc.username_113}"></property> <propertyname="Password"value="${jdbc.password_113}"></property> <propertyname="Maxactive"value=" the"/> <propertyname="InitialSize"value="Ten"/> <propertyname="Maxwait"value="60000"/> <propertyname="Minidle"value="5"/></bean><sharding:standard-strategyid="Databaseshardingstrategy"sharding-column="business_id"precise-algorithm-class="Com.boothsun.util.sharding.PreciseModuloDatabaseShardingAlgorithm" /><sharding:data-sourceid="Shardingdatasource"> <sharding:sharding-ruledata-source-names="ds_112,ds_113"> <sharding:table-rules> <sharding:table-rulelogic-table="T_order"database-strategy-ref="Databaseshardingstrategy" /> </sharding:table-rules> </sharding:sharding-rule></sharding:data-source>
SHARDING-JDBC related label meanings See official Documentation: Configuration Guide
Official Demo:github
Sub-Library rule classes
/*** Exact Match */ Public Final classPrecisemodulodatabaseshardingalgorithmImplementspreciseshardingalgorithm<integer> {Private Static FinalMap<integer,string> Datasourcemap =NewHashmap<> ();Static{Datasourcemap.put( the,"ds_112"); Datasourcemap.put(113,"ds_113"); }@Override PublicStringdosharding(FinalCollection<string> Availabletargetnames,FinalPreciseshardingvalue<integer> shardingvalue) {returnDatasourcemap.Get(Shardingvalue.GetValue()); }}
Single Test class
@AutowiredOrdermapper Ordermapper;/*** Test Insert * @throws Exception */@Test Public void insertselective()throwsException {Order order =New Order(); Order.Setorderid(1231); Order.Setuserid(222); Order.Setbusinessid(113); Boolean result = Ordermapper.Insert(order) >0; System. out.println(result?)"Insert succeeded":"Insert Failed");}/*** Test in query operation * @throws Exception */@Test Public void SelectByExample2()throwsException {list<integer> values =NewArraylist<> (); Values.Add( the); Values.Add(113); orderexample example =New Orderexample() ; Example.Createcriteria().Andbusinessidin(values); List<order> orderlist = Ordermapper.Selectbyexample(example); System. out.println(Jsonobject.tojsonstring(orderlist));}/*** Test between's query operation * @throws Exception */@Test Public void SelectByExample3()throwsException {Orderexample example =New Orderexample() ; Example.Createcriteria().Andbusinessidbetween( the,113); List<order> orderlist = Ordermapper.Selectbyexample(example); System. out.println(Jsonobject.tojsonstring(orderlist));}
SHARDING-JDBC Realization of Library