在写代码的时候经常有这种需求,一个订单对应多个商品,需要对订单以及商品进行分页模糊搜索,
在首页展示多个满足要求的订单,并且同时展示出订单中的商品信息,
此时需要返回一个list,并且list中对象是一对多的关系,就是对1对多种的多进行分页.这个时候的思路.
我们以一为主表,多为副表进行分析
1.实现:使用mybatis的一对多,就是resultMap中的collections进行数据的接收.
2.思路:
主表(一)为场景表 t_scene.sql
副表(二)为数据表 t_data.sql
映射关系为 t_scene_data_mapper.sql 表 一个场景对应多个数据
其中code都是唯一的,不使用id.
========
t_data.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_data
-- ----------------------------
DROP TABLE IF EXISTS `t_data`;
CREATE TABLE `t_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`data_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_data
-- ----------------------------
INSERT INTO `t_data` VALUES (1, 'data0001', '111');
INSERT INTO `t_data` VALUES (2, 'data0002', '233');
INSERT INTO `t_data` VALUES (3, 'data0003', '34q53');
INSERT INTO `t_data` VALUES (4, 'data0004', 'cvzxgf');
INSERT INTO `t_data` VALUES (5, 'data0005', '42rg');
SET FOREIGN_KEY_CHECKS = 1;
==============
t_scene.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_scene
-- ----------------------------
DROP TABLE IF EXISTS `t_scene`;
CREATE TABLE `t_scene` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`scene_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`scene_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_scene
-- ----------------------------
INSERT INTO `t_scene` VALUES (1, 'scene0001', 'adf');
INSERT INTO `t_scene` VALUES (2, 'scene0002', 'adf');
INSERT INTO `t_scene` VALUES (3, 'scene0003', 'er');
INSERT INTO `t_scene` VALUES (4, 'scene0004', '43');
SET FOREIGN_KEY_CHECKS = 1;
============
t_scene_data_mappe
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_scene_data_mapper
-- ----------------------------
DROP TABLE IF EXISTS `t_scene_data_mapper`;
CREATE TABLE `t_scene_data_mapper` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`scene_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`data_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_scene_data_mapper
-- ----------------------------
INSERT INTO `t_scene_data_mapper` VALUES (1, 'scene0001', 'data0001');
INSERT INTO `t_scene_data_mapper` VALUES (2, 'scene0001', 'data0002');
INSERT INTO `t_scene_data_mapper` VALUES (3, 'scene0001', 'data0003');
INSERT INTO `t_scene_data_mapper` VALUES (4, 'scene0002', 'data0003');
INSERT INTO `t_scene_data_mapper` VALUES (5, 'scene0002', 'data0005');
INSERT INTO `t_scene_data_mapper` VALUES (6, 'scene0003', 'data0004');
INSERT INTO `t_scene_data_mapper` VALUES (7, 'scene0003', 'data0005');
INSERT INTO `t_scene_data_mapper` VALUES (8, 'scene0004', 'data0005');
SET FOREIGN_KEY_CHECKS = 1;
@lombok.Data
public class Data {
private int id;
private String dataCode;
private String dataName;
}
=======
@Data
public class Scene {
private int id;
private String sceneCode;
private String sceneName;
}
=======
@Data
public class SceneDataMapper {
private int id;
private String sceneCode;
private List dataList;
}
@RequestMapping("/page22")
public String page22() {
List sceneList = sceneMapper.sceneDataMapperList();
for (SceneDataMapper scene : sceneList) {
System.out.println(scene);
}
return "555";
}