配置广播表
原创大约 2 分钟
之前说过,所谓广播表
其实就是一种配置表
或字典表
,这种表数据量不大,但很多地方都需要。
先创建一张配置表
。
-- 依次在`itechthink_order_0`、`itechthink_order_1`和`itechthink_order_2`中执行以下建表语句
> DROP TABLE IF EXISTS t_config;
> CREATE TABLE t_config (
id BIGINT(20) NOT NULL COMMENT '配置编码',
ckey VARCHAR(512) DEFAULT NULL COMMENT '配置key',
cvalue VARCHAR(512) DEFAULT NULL COMMENT '配置value',
type VARCHAR(128) DEFAULT NULL COMMENT '配置类型',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '配置表';
然后增加对应的实体类Config
。
package com.itechthink.model;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import java.util.Objects;
// 指定逻辑表名
@TableName("t_config")
public class Config {
private Long id;
private String ckey;
private String cvalue;
private String type;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCkey() {
return ckey;
}
public void setCkey(String ckey) {
this.ckey = ckey;
}
public String getCvalue() {
return cvalue;
}
public void setCvalue(String cvalue) {
this.cvalue = cvalue;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Config config = (Config) o;
return Objects.equals(id, config.id) && Objects.equals(ckey, config.ckey) && Objects.equals(cvalue, config.cvalue) && Objects.equals(type, config.type);
}
@Override
public int hashCode() {
return Objects.hash(id, ckey, cvalue, type);
}
@Override
public String toString() {
return "Config{" +
"id=" + id +
", ckey='" + ckey + '\'' +
", cvalue='" + cvalue + '\'' +
", type='" + type + '\'' +
'}';
}
}
和之前的Order
一样,也需要给Config
实体类配上相应的mapper
。
package com.itechthink.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itechthink.model.Config;
public interface ConfigMapper extends BaseMapper<Config> {
}
接着修改配置文件application.properties
,加入如下内容(其他地方不用修改)。
......
# 配置广播表,如果有多个表用,分隔
spring.shardingsphere.sharding.broadcast-tables=t_config
spring.shardingsphere.sharding.tables.t_config.key-generator.column=id
spring.shardingsphere.sharding.tables.t_config.key-generator.type=SNOWFLAKE
......
最后修改ShardingJDBCTest
测试类。
package com.itechthink;
import com.itechthink.mapper.ConfigMapper;
import com.itechthink.mapper.OrderMapper;
import com.itechthink.model.Config;
import com.itechthink.model.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingJDBCApplication.class)
public class ShardingJDBCTest {
@Autowired
private OrderMapper orderMapper;
@Autowired
private ConfigMapper configMapper;
@Test
public void testSaveOrder() {
for (int i = 0; i < 10; i++) {
Order order = new Order();
// 分片键
order.setUserid(Long.valueOf(i));
order.setTradeno(UUID.randomUUID().toString().substring(0, 32).replaceAll("-", ""));
order.setState(1);
order.setMoney(168.00);
order.setCreatetime(new Date());
orderMapper.insert(order);
}
}
@Test
public void testSaveConfig(){
Config config = new Config();
config.setCkey("url");
config.setCvalue("itechthink.com");
config.setType("网站");
configMapper.insert(config);
}
}
执行后发现,itechthink_order_0
、itechthink_order_1
和itechthink_order_2
这三个数据库的t_config
表中都多了一条同样的数据。
> SELECT * FROM t_config;
+---------------------+------+----------------+--------+
| id | ckey | cvalue | type |
+---------------------+------+----------------+--------+
| 1822937004065742850 | url | itechthink.com | 网站 |
+---------------------+------+----------------+--------+
1 row in set (0.00 sec)
感谢支持
更多内容,请移步《超级个体》。