• Sharding-JDBC 公共表


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

    (1)创建数据库

    分别在user_dborder_db_1order_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;

    (2)Sharding-JDBC规则中修改 

    # 指定t_dict为公共表
    spring.shardingsphere.sharding.broadcast-tables=t_dict

    (3)数据操作

    新增DictDao: 

    @Mapper
    @Component
    public interface DictDao {
        /**
         * 新增字典
         *
         * @param type  字典类型
         * @param code  字典编码
         * @param value 字典值
         * @return
         */
        @Insert("insert into t_dict(dict_id,type,code,value) value(#{dictId},#{type},#{code},#{value})")
        int insertDict(@Param("dictId") Long dictId, @Param("type") String type, @Param("code") String
                code, @Param("value") String value);
    
        /**
         * 删除字典
         *
         * @param dictId 字典id
         * @return
         */
        @Delete("delete from t_dict where dict_id = #{dictId}")
        int deleteDict(@Param("dictId") Long dictId);
    }

    (4)字典操作测试

    新增单元测试方法: 

    @Test
    public void testInsertDict() {
        dictDao.insertDict(1L, "user_type", "0", "管理员");
        dictDao.insertDict(2L, "user_type", "1", "操作员");
    }
    
    @Test
    public void testDeleteDict() {
        dictDao.deleteDict(1L);
        dictDao.deleteDict(2L);
    }

    执行testInsertDict

    通过日志可以看出,对t_dict的表的操作被广播至所有数据源。

    测试删除字典,观察是否把所有数据源中该 公共表的记录删除。

    (5)字典关联查询测试

    字典表已在各各分库存在,各业务表即可和字典表关联查询。

    定义用户关联查询dao

    UserDao中定义:

    /**
     * 根据id列表查询多个用户,关联查询字典表
     *
     * @param userIds 用户id列表
     * @return
     */
    @Select({"<script>",
            " select",
            " * ",
            " from t_user t ,t_dict b",
            " where t.user_type = b.code and t.user_id in",
            "<foreach collection='userIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<Map> selectUserInfobyIds(@Param("userIds") List<Long> userIds);

    定义测试方法:

    @Test
    public void testSelectUserInfobyIds(){
        List<Long> userIds = new ArrayList<>();
        userIds.add(1L);
        userIds.add(2L);
        List<Map> users = userDao.selectUserInfobyIds(userIds);
        JSONArray jsonUsers = new JSONArray(users);
        System.out.println(jsonUsers);
    }

    执行测试方法,查看日志,成功关联查询字典表:

  • 相关阅读:
    【EntityFramework系列教程十,翻译】ASP.NET MVC程序中的一些高级应用
    对不含数据源的DataGridView实现自定义排序
    poj 1584 A Round Peg in a Ground Hole(叉积判断凸多边形)
    大整数运算
    poj 1408 Fishnet(计算几何)
    poj 1201 Intervals(第一道差分约束题)
    poj 2983 Is the Information Reliable?(差分约束)
    poj 2187 Beauty Contest(凸包+旋转卡壳)
    poj 2031 Building a Space Station(prim)
    poj 3007 Organize Your Train part II
  • 原文地址:https://www.cnblogs.com/jwen1994/p/13173505.html
Copyright © 2020-2023  润新知