Browse Source

新增分库分表 以及 广播表 单表

赵冬冬 3 năm trước cách đây
mục cha
commit
59445db5c3

+ 25 - 6
db/sharding0.sql

@@ -11,7 +11,7 @@
  Target Server Version : 50734
  File Encoding         : 65001
 
- Date: 18/02/2022 17:08:45
+ Date: 21/02/2022 10:34:50
 */
 
 SET NAMES utf8mb4;
@@ -23,13 +23,21 @@ SET FOREIGN_KEY_CHECKS = 0;
 DROP TABLE IF EXISTS `t_dict`;
 CREATE TABLE `t_dict`  (
   `id` bigint(18) NOT NULL COMMENT '主键',
-  `t_name` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
-  `t_key` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'key',
-  `t_value` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'value',
+  `tindex` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
+  `tkey` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'key',
+  `tvalue` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'value',
   `create_time` datetime(0) NULL DEFAULT NULL COMMENT '新增时间',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  INDEX `idx_tindex`(`tindex`) USING BTREE,
+  INDEX `idx_tkey`(`tkey`) USING BTREE
 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
 
+-- ----------------------------
+-- Records of t_dict
+-- ----------------------------
+INSERT INTO `t_dict` VALUES (1, '成功', 'SUCCESS', '成功', '2022-02-21 10:12:36');
+INSERT INTO `t_dict` VALUES (2, '失败', 'ERROR', '失败', '2022-02-21 10:13:07');
+
 -- ----------------------------
 -- Table structure for t_user
 -- ----------------------------
@@ -41,6 +49,17 @@ CREATE TABLE `t_user`  (
   `year` int(10) NULL DEFAULT NULL COMMENT '新增年份',
   `create_time` datetime(0) NULL DEFAULT NULL COMMENT '新增时间',
   PRIMARY KEY (`id`) USING BTREE
-) ENGINE = InnoDB AUTO_INCREMENT = 773 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
+) ENGINE = InnoDB AUTO_INCREMENT = 790 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
+
+-- ----------------------------
+-- Records of t_user
+-- ----------------------------
+INSERT INTO `t_user` VALUES (15, 'New Ellsworthport', 1000, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (320, 'New Jarvisside', 692, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (351, 'Jenkinschester', 436, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (424, 'East Lafayette', 256, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (714, 'Nitzschemouth', 987, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (772, 'Kilbackburgh', 695, 2022, '2022-02-03 00:00:00');
+INSERT INTO `t_user` VALUES (789, 'Rasheedshire', 688, 2022, '2022-02-03 00:00:00');
 
 SET FOREIGN_KEY_CHECKS = 1;

+ 30 - 7
db/sharding1.sql

@@ -11,7 +11,7 @@
  Target Server Version : 50734
  File Encoding         : 65001
 
- Date: 18/02/2022 17:08:55
+ Date: 21/02/2022 10:35:01
 */
 
 SET NAMES utf8mb4;
@@ -23,13 +23,21 @@ SET FOREIGN_KEY_CHECKS = 0;
 DROP TABLE IF EXISTS `t_dict`;
 CREATE TABLE `t_dict`  (
   `id` bigint(18) NOT NULL COMMENT '主键',
-  `t_name` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
-  `t_key` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'key',
-  `t_value` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'value',
+  `tindex` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
+  `tkey` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'key',
+  `tvalue` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'value',
   `create_time` datetime(0) NULL DEFAULT NULL COMMENT '新增时间',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  INDEX `idx_tindex`(`tindex`) USING BTREE,
+  INDEX `idx_tkey`(`tkey`) USING BTREE
 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
 
+-- ----------------------------
+-- Records of t_dict
+-- ----------------------------
+INSERT INTO `t_dict` VALUES (1, '成功', 'SUCCESS', '成功', '2022-02-21 10:12:36');
+INSERT INTO `t_dict` VALUES (2, '失败', 'ERROR', '失败', '2022-02-21 10:13:07');
+
 -- ----------------------------
 -- Table structure for t_order_2022
 -- ----------------------------
@@ -42,9 +50,21 @@ CREATE TABLE `t_order_2022`  (
   `dict_key` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据字典表',
   `year` int(10) NULL DEFAULT NULL COMMENT '新增年份',
   `create_time` datetime(0) NULL DEFAULT NULL COMMENT '新增时间',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  INDEX `idx_user_id`(`user_id`) USING BTREE,
+  INDEX `idx_dict_key`(`dict_key`) USING BTREE,
+  INDEX `idx_year`(`year`) USING BTREE
 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
 
+-- ----------------------------
+-- Records of t_order_2022
+-- ----------------------------
+INSERT INTO `t_order_2022` VALUES (1494596441083518978, 1, '364', 529, 'SUCCESS', 2022, '2022-01-03 00:00:00');
+INSERT INTO `t_order_2022` VALUES (1494600027712569346, 772, '61', 357, 'SUCCESS', 2022, '2022-01-03 00:00:00');
+INSERT INTO `t_order_2022` VALUES (1495570510310334466, 772, '602', 842, 'SUCCESS', 2022, '2022-01-03 00:00:00');
+INSERT INTO `t_order_2022` VALUES (1495570512940163074, 772, '693', 854, 'SUCCESS', 2022, '2022-01-03 00:00:00');
+INSERT INTO `t_order_2022` VALUES (1495583705104150530, 772, '759', 406, 'SUCCESS', 2022, '2022-01-03 00:00:00');
+
 -- ----------------------------
 -- Table structure for t_order_2023
 -- ----------------------------
@@ -57,7 +77,10 @@ CREATE TABLE `t_order_2023`  (
   `dict_key` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据字典表',
   `year` int(10) NULL DEFAULT NULL COMMENT '新增年份',
   `create_time` datetime(0) NULL DEFAULT NULL COMMENT '新增时间',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  INDEX `idx_user_id`(`user_id`) USING BTREE,
+  INDEX `idx_dict_key`(`dict_key`) USING BTREE,
+  INDEX `idx_year`(`year`) USING BTREE
 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
 
 SET FOREIGN_KEY_CHECKS = 1;

+ 3 - 3
src/main/java/com/xin/shardingspherejdbcdemo/entity/Dict.java

@@ -9,8 +9,8 @@ import java.util.Date;
 @Data
 public class Dict {
     private Long id;
-    private String tName;
-    private String tKey;
-    private String tValue;
+    private String tindex;
+    private String tkey;
+    private String tvalue;
     private Date createTime;
 }

+ 1 - 1
src/main/java/com/xin/shardingspherejdbcdemo/mapper/OrderMapper.java

@@ -10,5 +10,5 @@ import java.util.Map;
 
 @Mapper
 public interface OrderMapper extends BaseMapper<Order> {
-    List<Map<String, String>> selectOrderByid(@Param("userId") Long userId);
+    List<Map<String, String>> selectOrderByid(@Param("userId") Long userId, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize);
 }

+ 2 - 1
src/main/java/com/xin/shardingspherejdbcdemo/service/impl/UserServiceImpl.java

@@ -103,7 +103,8 @@ public class UserServiceImpl implements UserService {
     }
     public List<Map<String, String>> list(Long userId,int pageNum, int pageSize){
         //PageHelper.startPage(pageNum, pageSize);
-        List<Map<String, String>> list = orderMapper.selectOrderByid(userId);
+        pageNum = (pageNum - 1) * pageSize;
+        List<Map<String, String>> list = orderMapper.selectOrderByid(userId,pageNum,pageSize);
         return list;
     }
 

+ 6 - 6
src/main/java/com/xin/shardingspherejdbcdemo/test/MainTest.java

@@ -53,9 +53,9 @@ public class MainTest {
         Date now = new Date();
         int year = EntityUtils.dateTostr(now);
         dict.setId(id);
-        dict.setTName(value);
-        dict.setTKey(key);
-        dict.setTValue(value);
+        dict.setTindex(value);
+        dict.setTkey(key);
+        dict.setTvalue(value);
         dict.setCreateTime(now);
         dictMapper.insert(dict);
     }
@@ -67,9 +67,9 @@ public class MainTest {
         Date now = new Date();
         int year = EntityUtils.dateTostr(now);
         dict.setId(snowFlake.nextId());
-        dict.setTName("成功");
-        dict.setTKey("SUCCESS");
-        dict.setTValue("成功");
+        dict.setTindex("成功");
+        dict.setTkey("SUCCESS");
+        dict.setTvalue("成功");
         dict.setCreateTime(now);
         dictMapper.updateById(dict);
 

+ 4 - 2
src/main/resources/mapper/OrderMapper.xml

@@ -7,11 +7,13 @@
         SELECT distinct
             b.order_num,
             b.money,
-            b.create_time
+            b.create_time,
+            c.tvalue
         FROM
             t_order_ b
+            JOIN t_dict c on c.tkey = b.dict_key
         WHERE
-            b.user_id=#{userId}  order by b.create_time desc limit 1,10
+            b.user_id=#{userId}  order by b.create_time desc limit #{pageNum},#{pageSize}
 
     </select>
 </mapper>