Sharing-JDBC入门

本文内容参考Sharding-JDBC官方文档

因为第一次接触数据库中间件,主要看了一下Sharding-JDBC和MyCat,感觉数据库中间件,在系统中主要起到一个路由分配的作用,跟一般用的路由器的相似。需要通过一系列的配置和规则来规定,数据查询和存储的位置。
与MyCat不同,Sharding-JDBC不需要安装客户端。

Sharding-JDBC系统架构图:

Sharing-JDBC入门_第1张图片

Sharing-JDBC入门_第2张图片
引入maven依赖

    com.dangdang
    sharding-jdbc-core
    ${latest.release.version}

规则配置
    
        
        
        
        
    

    
        
        
        
        
    
    
    
        
        
        
        
    

    
    
    

    
        
            
                
                
            
        
    
sql导入

将该段sql放入到3个库中执行

CREATE SCHEMA IF NOT EXISTS `dbtb`;

CREATE TABLE IF NOT EXISTS `dbtb`.`t_order_0` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_id`));
CREATE TABLE IF NOT EXISTS `dbtb`.`t_order_1` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_id`));
CREATE TABLE IF NOT EXISTS `dbtb`.`t_order_item_0` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_item_id`));
CREATE TABLE IF NOT EXISTS `dbtb`.`t_order_item_1` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_item_id`));

insert()

    @Override
    public void insert() {
        String orderSql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
        String orderItemSql = "INSERT INTO `t_order_item` (`order_item_id`, `order_id`, `user_id`, `status`) VALUES (?, ?, ?, ?)";
        for (int orderId = 1; orderId <= 2; orderId++) {
            for (int userId = 1; userId <= 3; userId++) {
                try (Connection connection = shardingDataSource.getConnection()) {
                    PreparedStatement preparedStatement = connection.prepareStatement(orderSql);
                    preparedStatement.setInt(1, orderId);
                    preparedStatement.setInt(2, userId);
                    preparedStatement.setString(3, "insert");
                    preparedStatement.execute();
                    preparedStatement.close();

                    preparedStatement = connection.prepareStatement(orderItemSql);
                    int orderItemId = orderId + 4;
                    preparedStatement.setInt(1, orderItemId);
                    preparedStatement.setInt(2, orderId);
                    preparedStatement.setInt(3, userId);
                    preparedStatement.setString(4, "insert");
                    preparedStatement.execute();
                    preparedStatement.close();
                    // CHECKSTYLE:OFF
                } catch (final Exception ex) {
                    // CHECKSTYLE:ON
                    ex.printStackTrace();
                }
            }
        }
    }

selectAll()

    @Override
    public void selectAll() {
        String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
        try (Connection conn = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println("orderItemId:" + rs.getInt(1) + ",orderId:" + rs.getInt(2) + ",userId:"
                            + rs.getInt(3) + ",status:" + rs.getString(4));
                }
            }
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }
Sharing-JDBC入门_第3张图片
selectAll.png

count()

    @Override
    public void count() {
        String sql = "SELECT count(1) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
        try (Connection conn = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println("count :" + rs.getInt(1));
                }
            }
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }

count.png

orderby()

    @Override
    public void orderby() {
        String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id order by o.user_id asc,i.order_id asc";
        try (Connection conn = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println("orderItemId:" + rs.getInt(1) + ",orderId:" + rs.getInt(2) + ",userId:"
                            + rs.getInt(3) + ",status:" + rs.getString(4));
                }
            }
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }
Sharing-JDBC入门_第4张图片
orderby.png

groupby()

    @Override
    public void groupby() {
        String sql = "SELECT o.user_id,count(o.user_id) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id group by o.user_id";
        try (Connection conn = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println("userId:" + rs.getInt(1) + ",count:" + rs.getInt(2));
                }
            }
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }
groupby.png

limit()

    @Override
    public void limit() {
        String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id order by o.user_id asc,i.order_id asc limit 1,3";
        try (Connection conn = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println("orderItemId:" + rs.getInt(1) + ",orderId:" + rs.getInt(2) + ",userId:"
                            + rs.getInt(3) + ",status:" + rs.getString(4));
                }
            }
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }
Sharing-JDBC入门_第5张图片
limit.png

delete()

    @Override
    public void delete() {
        String orderSql = "DELETE FROM `t_order`";
        String orderItemSql = "DELETE FROM `t_order_item`";
        try (Connection connection = shardingDataSource.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(orderSql);
            preparedStatement.execute();
            preparedStatement.close();
            preparedStatement = connection.prepareStatement(orderItemSql);
            preparedStatement.execute();
            preparedStatement.close();
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            ex.printStackTrace();
        }
    }

以上根据Sharding-JDBC官方demo修改,详见官方demo。

你可能感兴趣的:(Sharing-JDBC入门)