当前位置: 代码迷 >> 综合 >> 【Sharding-JDBC 】(四)整合mybatis-plus 公共表
  详细解决方案

【Sharding-JDBC 】(四)整合mybatis-plus 公共表

热度:15   发布时间:2023-11-17 12:50:28.0

概念:

       公共表属于系统中数据量较小,变动少,而且属于高频联合查询的依赖表。参数表、数据字典表等属于此类型。可 以将这类表在每个数据库都保存一份,所有更新操作都同时发送到所有分库执行。

数据库:

数据库脚本:(两个服务器的user_db库下均新增t_dict表)

(1)创建数据库
分别在user_db、order_db_1、order_db_2中创建t_dict表: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;

实体类:

package com.lucifer.sharding.pojo;import java.io.Serializable;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;/*** t_dict* @author */
@Data
@TableName(value = "t_dict")
public class Dict implements Serializable {/*** 字典id*/private Long dictId;/*** 字典类型*/private String type;/*** 字典编码*/private String code;/*** 字典值*/private String value;private static final long serialVersionUID = 1L;
}

DictDao接口: 

package com.lucifer.sharding.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lucifer.sharding.pojo.Dict;public interface DictDao extends BaseMapper<Dict> {
}

application.yml: 

############################公共表#################################
#服务端口
server:port: 56081
#服务名
spring:application:name: sharding-jdbc-examplesmain:allow-bean-definition-overriding: true#shardingsphere相关配置shardingsphere:datasource:names: m1,m2   #配置库的名字,随意m1:   #配置目前m1库的数据源信息type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.87.133:3306/user_db?useUnicode=trueusername: rootpassword: 123456m2:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.87.134:3306/user_db?useUnicode=trueusername: rootpassword: 123456sharding:broadcast‐tables: t_dict  #公共表tables:t_dict:key-generator:column: dict_idtype: SNOWFLAKEprops:sql:show: true   #打印sql#日志打印
logging:level:root: infoorg.springframework.web: infocom.lucifer.sharding.dao: debugdruid.sql: debug

测试:

package com.lucifer.sharding;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lucifer.sharding.dao.DictDao;
import com.lucifer.sharding.dao.OrderDao;
import com.lucifer.sharding.pojo.Dict;
import com.lucifer.sharding.pojo.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ShardingJdbcExamplesApplication.class})
public class ShardingJdbcExamplesApplicationTests {@ResourceDictDao dictDao;@Testpublic void add() {Dict dict = new Dict();dict.setCode("111");dict.setType("性别");dict.setValue("男");dictDao.insert(dict);}@Testpublic void delete() {QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();queryWrapper.eq("dict_id",418778126634450945L);dictDao.delete(queryWrapper);}}

1.add  测试新增方法,Sharding-JDBC会在分库中都执行一下;

2.delete:测试删除方法,Sharding-JDBC会在分库中都执行一下;

 

  相关解决方案