真实表
-
数据库中真实的表
数据节点
-
t_product(product_id),t_product_descrition(product_id),
-
指定分片规则一致的主表和从表
非绑定表:进行联表查询的时候会出现笛卡尔积 3 x 3 = 9
绑定表: 进行联表查询的时候不会出现笛卡尔积 3 x 3 = 3
新建一个springboot 项目
配置application.properties 文件
server.port=43210 spring.application.name = sharding-jdbc-quickstart-demo server.servlet.context-path = /sharding-jdbc-quickstart-demo spring.http.encoding.enabled = true spring.http.encoding.charset = UTF-8 spring.http.encoding.force = true mybatis.configuration.map-underscore-to-camel-case = true #spring.profiles.active=masterslave # 自定义数据源 spring.shardingsphere.datasource.names = m1 spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.m1.driver-class-name = com.mysql.jdbc.Driver spring.shardingsphere.datasource.m1.url = jdbc:mysql://localhost:3306/user_db?useUnicode=true&useSSL=false spring.shardingsphere.datasource.m1username = root spring.shardingsphere.datasource.m1.password = root # 以下是分片规则配置 # 指定t_order表的数据分布情况,配置数据节点 # t_order 逻辑表名 # insert into t_order () values () # $->{1..2} => 1, 2 => t_order_1,t_order_2 spring.shardingsphere.sharding.tables.t_order.actual-data-nodes = m1.t_order_$->{1..2} #spring.shardingsphere.sharding.tables.t_user.actual-data-nodes =ds0.t_user # 指定t_order表的主键生成策略为SNOWFLAKE spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id #雪花片算法,另一种 spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE # 指定t_order表的分片策略,分片策略包括分片键和分片算法 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 % 2 + 1} # 打开sql输出日志 spring.shardingsphere.props.sql.show = true logging.level.root = info #logging.level.org.springframework.web=info #logging.level.com.itheima.dbsharding=debug #logging.level.druid.sql=debug
mapper 文件
@Insert("insert into t_order(price,user_id,status)values(#{price},#{userId},#{status})") int insertOrderInfo(@Param("price") BigDecimal price, @Param("userId")Long userId, @Param("status")String status);
test 测试类
@Autowired public OrderMapper orderMapper; @Test public void testInsertOrder(){ for (int i = 0 ; i<10; i++){ orderMapper.insertOrderInfo(new BigDecimal((i+1)*4),1L,"Success"); } }
成功界面:
数据库中含有t_order_1 和 t_order_1 两张表
DROP TABLE IF EXISTS `t_order_1`; CREATE TABLE `t_order_1` ( `order_id` bigint(20) NOT NULL COMMENT '订单id', `price` decimal(10, 2) NOT NULL COMMENT '订单价格', `user_id` bigint(20) NOT NULL COMMENT '下单用户id', `status` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '订单状态', PRIMARY KEY (`order_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for t_order_2 -- ---------------------------- DROP TABLE IF EXISTS `t_order_2`; CREATE TABLE `t_order_2` ( `order_id` bigint(20) NOT NULL COMMENT '订单id', `price` decimal(10, 2) NOT NULL COMMENT '订单价格', `user_id` bigint(20) NOT NULL COMMENT '下单用户id', `status` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '订单状态', PRIMARY KEY (`order_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;