执行sql
#在数据库 user_db、order_db_1、order_db_2中均要建表
CREATE TABLE `t_dict` (
`dict_id` BIGINT (20) NOT NULL COMMENT '字典id',
`type` VARCHAR (50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '字典类型',
`code` VARCHAR (50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '字典编码',
`value` VARCHAR (50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '字典值',
PRIMARY KEY (`dict_id`) USING BTREE
) ENGINE = INNODB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
在test2文件中加入
spring.shardingsphere.sharding.broadcast-tables[0]= #广播表规则列表
spring.shardingsphere.sharding.broadcast-tables[1]= #广播表规则列表
spring.shardingsphere.sharding.broadcast-tables[x]= #广播表规则列表
spring.shardingsphere.sharding.broadcast-tables=t_dict
java测试代码
package com.itheima.sharding;
import com.itheima.sharding.entity.TDict;
import com.itheima.sharding.entity.TOrder;
import com.itheima.sharding.mapper.TDictMapper;
import com.itheima.sharding.mapper.TOrderMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
import java.util.Random;
@SpringBootTest
public class MyTest {
@Autowired
private TDictMapper tDictMapper;
/**
* 测试广播表:
* 当对法广播表进行增删改操作时,操作的sql会广播给各个数据源
*/
@Test
public void commonTable(){
TDict build = TDict.builder().dictId(1l).code("666").type("1").value("888")
.build();
tDictMapper.insert(build);
}
}
运行完查看数据库
t_order_1库中t_dict表
t_order_2库中t_dict表