mybatis中的高级映射一对一、一对多、多对多
2019-04-15 14:58发布
生成海报
学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一、一对多、多对多,希望多有需要的小伙伴有帮助,小编主要从四个方面进行介绍,订单商品数据模型、一对一查询、一对多查询、多对多查询。
一、订单商品数据模型
1、数据库执行脚本,如下所示:
[sql] view
plain copy
print?
-
"font-family:Comic Sans MS;font-size:18px;">CREATE TABLE items (
-
id INT NOT NULL AUTO_INCREMENT,
-
itemsname VARCHAR(32) NOT NULL COMMENT '商品名称',
-
price FLOAT(10,1) NOT NULL COMMENT '商品定价',
-
detail TEXT COMMENT '商品描述',
-
pic VARCHAR(64) DEFAULT NULL COMMENT '商品图片',
-
createtime DATETIME NOT NULL COMMENT '生产日期',
-
PRIMARY KEY (id)
-
) DEFAULT CHARSET=utf8;
-
-
/*Table structure for table `orderdetail` */
-
-
CREATE TABLE orderdetail (
-
id INT NOT NULL AUTO_INCREMENT,
-
orders_id INT NOT NULL COMMENT '订单id',
-
items_id INT NOT NULL COMMENT '商品id',
-
items_num INT DEFAULT NULL COMMENT '商品购买数量',
-
PRIMARY KEY (id),
-
KEY `FK_orderdetail_1` (`orders_id`),
-
KEY `FK_orderdetail_2` (`items_id`),
-
CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
-
CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
-
) DEFAULT CHARSET=utf8;
-
-
/*Table structure for table `orders` */
-
-
CREATE TABLE orders (
-
id INT NOT NULL AUTO_INCREMENT,
-
user_id INT NOT NULL COMMENT '下单用户id',
-
number VARCHAR(30) NOT NULL COMMENT '订单号',
-
createtime DATETIME NOT NULL COMMENT '创建订单时间',
-
note VARCHAR(100) DEFAULT NULL COMMENT '备注',
-
PRIMARY KEY (`id`),
-
KEY `FK_orders_1` (`user_id`),
-
CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
-
) DEFAULT CHARSET=utf8;
-
-
/*Table structure for table `t_user` */
-
-
CREATE TABLE t_user (
-
id INT NOT NULL AUTO_INCREMENT,
-
username VARCHAR(32) NOT NULL COMMENT '用户名称',
-
birthday DATE DEFAULT NULL COMMENT '生日',
-
sex CHAR(1) DEFAULT NULL COMMENT '性别',
-
address VARCHAR(256) DEFAULT NULL COMMENT '地址',
-
PRIMARY KEY (`id`)
-
) DEFAULT CHARSET=utf8;
-
测试数据代码
[sql] view
plain copy
print?
-
"font-family:Comic Sans MS;font-size:18px;">/*Data for the table `items` */
-
-
INSERT INTO items(itemsname,price,detail,pic,createtime) VALUES
-
('台式机',3000.0,'该电脑质量非常好!',NULL,'2015-07-07 13:28:53'),
-
('笔记本',6000.0,'笔记本性能好,质量好!',NULL,'2015-07-08 13:22:57'),
-
('背包',200.0,'名牌背包,容量大质量好!',NULL,'2015-07-010 13:25:02');
-
-
/*Data for the
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮