您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關Sharding中怎么使用Sphere實現數據分庫分表操作),小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${sharding-sphere.version}</version> </dependency> <!-- for spring boot --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding-sphere.version}</version> </dependency>
我這里用的是最新的版本
<!-- 分庫分表 --> <sharding-sphere.version>4.0.0-RC3-SNAPSHOT</sharding-sphere.version>
當前測試例子是2庫各10表,外加默認庫的t_user表。結果如下:
創建數據庫稍微注意一點,命名不能帶下劃線。 sharding0庫腳本如下:
/* Navicat Premium Data Transfer Source Server : Source Server Type : MySQL Source Server Version : 50727 Source Host : Source Schema : sharding0 Target Server Type : MySQL Target Server Version : 50727 File Encoding : 65001 Date: 17/09/2019 10:43:09 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_order0 -- ---------------------------- DROP TABLE IF EXISTS `t_order0`; CREATE TABLE `t_order0` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order1 -- ---------------------------- DROP TABLE IF EXISTS `t_order1`; CREATE TABLE `t_order1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order2 -- ---------------------------- DROP TABLE IF EXISTS `t_order2`; CREATE TABLE `t_order2` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order3 -- ---------------------------- DROP TABLE IF EXISTS `t_order3`; CREATE TABLE `t_order3` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order4 -- ---------------------------- DROP TABLE IF EXISTS `t_order4`; CREATE TABLE `t_order4` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order5 -- ---------------------------- DROP TABLE IF EXISTS `t_order5`; CREATE TABLE `t_order5` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order6 -- ---------------------------- DROP TABLE IF EXISTS `t_order6`; CREATE TABLE `t_order6` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order7 -- ---------------------------- DROP TABLE IF EXISTS `t_order7`; CREATE TABLE `t_order7` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order8 -- ---------------------------- DROP TABLE IF EXISTS `t_order8`; CREATE TABLE `t_order8` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order9 -- ---------------------------- DROP TABLE IF EXISTS `t_order9`; CREATE TABLE `t_order9` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `user_id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, `sex` int(4) NULL DEFAULT NULL, `phone` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`user_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
sharding1數據庫腳本如下:
/* Navicat Premium Data Transfer Source Server : 139.196.229.195 Source Server Type : MySQL Source Server Version : 50727 Source Host : 139.196.229.195:3306 Source Schema : sharding1 Target Server Type : MySQL Target Server Version : 50727 File Encoding : 65001 Date: 17/09/2019 10:43:20 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_order0 -- ---------------------------- DROP TABLE IF EXISTS `t_order0`; CREATE TABLE `t_order0` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order1 -- ---------------------------- DROP TABLE IF EXISTS `t_order1`; CREATE TABLE `t_order1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order2 -- ---------------------------- DROP TABLE IF EXISTS `t_order2`; CREATE TABLE `t_order2` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order3 -- ---------------------------- DROP TABLE IF EXISTS `t_order3`; CREATE TABLE `t_order3` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order4 -- ---------------------------- DROP TABLE IF EXISTS `t_order4`; CREATE TABLE `t_order4` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order5 -- ---------------------------- DROP TABLE IF EXISTS `t_order5`; CREATE TABLE `t_order5` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order6 -- ---------------------------- DROP TABLE IF EXISTS `t_order6`; CREATE TABLE `t_order6` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order7 -- ---------------------------- DROP TABLE IF EXISTS `t_order7`; CREATE TABLE `t_order7` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order8 -- ---------------------------- DROP TABLE IF EXISTS `t_order8`; CREATE TABLE `t_order8` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order9 -- ---------------------------- DROP TABLE IF EXISTS `t_order9`; CREATE TABLE `t_order9` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(32) NULL DEFAULT NULL, `order_id` bigint(32) NULL DEFAULT NULL, `title` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `content` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
#數據源名稱,多數據源以逗號分隔 spring.shardingsphere.datasource.names=sharding0,sharding1 #sharding0是數據源名 spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.sharding0.url=jdbc:mysql://***:3306/sharding0?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL spring.shardingsphere.datasource.sharding0.username=root spring.shardingsphere.datasource.sharding0.password=*** # spring.shardingsphere.datasource.<data-source-name>.xxx= #數據庫連接池的其它屬性 # sharding1 是數據源名稱 spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.sharding1.url=jdbc:mysql://***:3306/sharding1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL spring.shardingsphere.datasource.sharding1.username=root spring.shardingsphere.datasource.sharding1.password=*** # 默認數據源,沒有分片的走這個數據源 spring.shardingsphere.sharding.default-data-source-name=sharding0 spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=sharding$->{user_id % 2} # t_order是表明 spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=sharding$->{0..1}.t_order$->{0..9} spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 10} # 分布式主鍵 內置的支持這三種 SNOWFLAKE/UUID/LEAF_SEGMENT spring.shardingsphere.sharding.tables.t_order.key-generator.column=id spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
因為上面是根據 user_id、order_id 兩個字段分庫分表的,所以必須插入查詢都得帶上user_id、order_id兩個字段。
@Test public void testSharding(){ Order order = new Order(); order.setUserId(1l); order.setOrderId(1l); order.setTitle("測試,userId:"+order.getUserId() + " orderId:" + order.getOrderId()); order.setContent(order.getTitle()); Assert.assertEquals(1,orderMapper.insert(order)); }
查看數據庫,確實插入了
@Test public void testShardingRead(){ OrderExample ex = new OrderExample(); ex.createCriteria().andUserIdEqualTo(1l).andOrderIdEqualTo(1l); List<Order> orders = orderMapper.selectByExample(ex); Assert.assertNotNull(orders); Assert.assertEquals(1, orders.size()); orders.stream().forEach(o->{ System.out.println("userId:"+o.getUserId() + " orderId:" + o.getOrderId()); }); }
@Test public void testUserWrite(){ User user = new User(); user.setAge(10); user.setName("張三"); user.setPhone("15157181986"); user.setSex(1); user.setUpdateTime(new Date()); user.setCreateTime(new Date()); userMapper.insert(user); System.out.println("userId:"+user.getUserId()); }
@Test public void testShardingReadCount(){ OrderExample ex = new OrderExample(); long count = orderMapper.countByExample(ex); System.out.println("count:"+count); }
@Test public void testShardingReadLimit(){ OrderExample ex = new OrderExample(); ex.setLimit(2); ex.setOffset(2l); ex.setOrderByClause(" user_id desc "); List<Order> orders = orderMapper.selectByExample(ex); orders.stream().forEach(o->{ System.out.println("userId:"+o.getUserId() + " orderId:" + o.getOrderId()); }); }
@Getter @Setter public class GroupResult implements Serializable { private Long userId; private Integer cnt; } <resultMap id="GroupResultMap" type="com.zero.sharding.dal.dto.GroupResult"> <result column="user_id" jdbcType="BIGINT" property="userId" /> <result column="cnt" jdbcType="INTEGER" property="cnt" /> </resultMap> <select id="getUserOrderCount" resultMap="GroupResultMap"> select user_id , count(1) as cnt from t_order group by user_id </select> @Test public void testShardingReadGroupBy(){ List<GroupResult> userOrderCounts = orderMapper.getUserOrderCount(); userOrderCounts.forEach(u->System.out.println("userId:"+u.getUserId() + " count:" + u.getCnt())); }
單表:不做分庫分表的邏輯表 (如本示例中的t_user) 分表:做分庫分表的邏輯表(如本示例中的 t_order) 廣播表:指所有的分片數據源中都存在的表,表結構和表中的數據在每個數據庫中均完全一致。適用于數據量不大且需要與海量數據的表進行關聯查詢的場景,例如:字典表。(這個概念參考:廣播表)
這里 單表 跟 分表關聯查詢,必須每一個庫中有一份單表(也就是廣播表),否則不能關聯查詢。如下面的語句,本示例中是不能執行的。因為本示例 t_order 邏輯表分散在兩個庫中,t_user表必須要在兩個庫中都有一份才能關聯查詢。
<select id="getOrders" parameterType="java.lang.Integer" resultType="OrderUnionUser"> select id, u.user_id as userId, order_id as orderId , title, content, u.create_time as createTime , u.update_time as updateTime , `name`, age, sex, phone from t_user u join t_order o on u.user_id = o.user_id where u.age > #{age,jdbcType=INTEGER} </select>
以上就是Sharding中怎么使用Sphere實現數據分庫分表操作),小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。