The simplest example in the history of shared-jdbc

Introduction to shared-jdbc

Sharding-JDBC is a database horizontal sharding framework separated from the relational database module dd-rdb in the Dangdang application framework ddframe , and realizes transparent database sub-database and sub-table access. Sharding-JDBC is the third open source project in the ddframe series after dubbox and elastic-job .

Sharding-JDBC directly encapsulates the JDBC protocol, which can be understood as an enhanced version of the JDBC driver, and the migration cost of old code is almost zero.

Sharding-JDBC is positioned as a lightweight java framework, using the client to directly connect to the database, providing services in the form of jar packages, no proxy layer, no additional deployment, no other dependencies, and DBA does not need to change the original operation and maintenance method.


1. Import dependencies

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.0</version>

<scope>test</scope>

</dependency>

 

<dependency>

<groupId>io.shardingjdbc</groupId>

<artifactId>sharding-jdbc-core</artifactId>

<version>2.0.0</version>

</dependency>

 

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.0.13</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.28</version>

</dependency>

</dependencies>

2.表的创建语句

CREATE TABLE `t_order_x` (

  `order_id` int(11) NOT NULL,

  `user_id` int(11) NOT NULL,

  PRIMARY KEY (`order_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

3.获取数据源

package com.irisian.sharedjdbc;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

import io.shardingjdbc.core.api.ShardingDataSourceFactory;

import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;

import io.shardingjdbc.core.api.config.TableRuleConfiguration;

import io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration;

public class DateSourceUtils {

public static DataSource getDataSource() throws SQLException {

 

// 配置真实数据源

Map<String, DataSource> dataSourceMap = new HashMap<>();

 

// 配置第一个数据源

DruidDataSource dataSource1 = new DruidDataSource();

dataSource1.setDriverClassName("com.mysql.jdbc.Driver");

dataSource1.setUrl("jdbc:mysql://localhost:3306/db0");

dataSource1.setUsername("root");

dataSource1.setPassword("123456");

// 将数据库放入到数据库map集合中

dataSourceMap.put("db0", dataSource1);

 

// 配置第二个数据源

DruidDataSource dataSource2 = new DruidDataSource();

dataSource2.setDriverClassName("com.mysql.jdbc.Driver");

dataSource2.setUrl("jdbc:mysql://localhost:3306/db1");

dataSource2.setUsername("root");

dataSource2.setPassword("123456");

// 将数据库放入到map集合中

dataSourceMap.put("db1", dataSource2);

 

// 配置Order表规则

TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();

orderTableRuleConfig.setLogicTable("t_order");

//下面这两种是等价的,表示在两个库均匀分布

/**

 * db0

  ├── t_order_0

  └── t_order_1

db1

  ├── t_order_0

  └── t_order_1

 */

orderTableRuleConfig.setActualDataNodes("db0.t_order_0, db0.t_order_1, db1.t_order_0, db1.t_order_1");

//orderTableRuleConfig.setActualDataNodes("db${0..1}.t_order_${0..1}");

 

// 配置分库策略

orderTableRuleConfig.setDatabaseShardingStrategyConfig(

new InlineShardingStrategyConfiguration("user_id", "db${user_id % 2}"));

 

// 配置分表策略

orderTableRuleConfig.setTableShardingStrategyConfig(

new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));

 

// 配置分片规则

ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();

shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

 

// 获取数据源对象

DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,

new ConcurrentHashMap(), new Properties());

return dataSource;

 

}

 

}

4.操作数据库

package com.irisian.sharedjdbc;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import javax.sql.DataSource;

public class Test {

public static void main(String[] args) throws SQLException {

DataSource dataSource = DateSourceUtils.getDataSource();

Connection conn = dataSource.getConnection();

String sql="insert into t_order(order_id,user_id) values(?,?)";

PreparedStatement prep = conn.prepareStatement(sql);

prep.setInt(1, 3);

prep.setInt(2, 1);

prep.execute();

}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597744&siteId=291194637