Sharding-JDBC 操作公共表
1 、公共表
( 1 )存储固定数据的表,表数据很少发生变化,查询时候经常进行关联
( 2 )在每个数据库中创建出相同结构公共表
2 、在多个数据库都创建相同结构公共表
3个表分别添加
use user_db; #use edu_db_1; #use edu_db_2; create table t_dict( `dict_id` bigint(20) primary key, `status` varchar(100) not null, `value` varchar(100) not null );
3 、在项目配置文件application.properties进行公共表配置
配置公共表
spring.shardingsphere.sharding.broadcast-tables=t_udict spring.shardingsphere.sharding.tables.t_udict.key-generator.column=dictid spring.shardingsphere.sharding.tables.t_udict.key-generator.type=SNOWFLAKE
整体:
# sharding-jdbc 水平分库分表策略 # 配置数据源,给数据源起别名 # 水平分库需要配置多个数据库 # m0为用户数据库 spring.shardingsphere.datasource.names=m1,m2,m0 # 一个实体类对应两张表,覆盖 spring.main.allow-bean-definition-overriding=true # 配置第一个数据源的具体内容,包含连接池,驱动,地址,用户名,密码 spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m1.username=root spring.shardingsphere.datasource.m1.password=root # 配置第二个数据源的具体内容,包含连接池,驱动,地址,用户名,密码 spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m2.username=root spring.shardingsphere.datasource.m2.password=root # 配置user数据源的具体内容,包含连接池,驱动,地址,用户名,密码 spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m0.username=root spring.shardingsphere.datasource.m0.password=root # 配置user_db数据库里面t_user 专库专表 spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m0.t_user # 配置主键的生成策略 spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE # 指定分表策略 spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user # 指定数据库分布的情况和数据表分布的情况 # m1 m2 course_1 course_2 spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2} # 指定 course 表里面主键 cid 的生成策略 SNOWFLAKE spring.shardingsphere.sharding.tables.course.key-generator.column=cid spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 指定分库策略 约定 user_id 值偶数添加到 m1 库,如果 user_id 是奇数添加到 m2 库 # 默认写法(所有的表的user_id) #spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id #spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1} # 指定只有course表的user_id spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1} # 指定分表策略 约定 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到 course_2 表 spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1} # 公共表配置 spring.shardingsphere.sharding.broadcast-tables=t_dict # 配置主键的生成策略 spring.shardingsphere.sharding.tables.t_dict.key-generator.column=dict_id spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE # 打开 sql 输出日志 spring.shardingsphere.props.sql.show=true
4 、编写测试代码
( 1 )创建新实体类和mapper
@Data @TableName("t_dict") public class Dict { private Long dictId; private String status; private String value; }
@Repository public interface DictMapper extends BaseMapper<Dict> { }
( 2 )编写添加和删除方法进行测试
//todo ==================================公共表========================================== @Autowired private DictMapper dictMapper; @Test public void addDict1() { Dict dict = new Dict(); dict.setStatus("Normal"); dict.setValue("启用"); dictMapper.insert(dict); } @Test public void deleteDict1() { QueryWrapper<Dict> wrapper = new QueryWrapper<>(); wrapper.eq("dict_id", 765734748386492417L); dictMapper.delete(wrapper); }
全部删除了