Mybatis学习笔记—高级映射,延迟加载

Posted 愚人节第二天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习笔记—高级映射,延迟加载相关的知识,希望对你有一定的参考价值。

本文主要介绍了如何使用resultMap完成高级映射;分析数据库中表之间的关系(一对一、一对多、多对多)

如何在mapper.xml文件中配置resultMap实现一对一、一对多、多对多;mybatis如何实现延迟加载

1       数据模型

数据库中有已经导入的四个表:items:(商品信息表);orderdetail:(订单明细表);orders:(订单表);user:(用户表)

1 CREATE TABLE items (
 2   id INT NOT NULL  AUTO_INCREMENT,
 3   itemsname VARCHAR(32) NOT NULL COMMENT '商品名称',
 4   price FLOAT(10,1) NOT NULL COMMENT '商品定价',
 5   detail TEXT COMMENT '商品描述',
 6   pic VARCHAR(64) DEFAULT NULL COMMENT '商品图片',
 7   createtime DATETIME NOT NULL COMMENT '生产日期',
 8   PRIMARY KEY (id)
 9 )  DEFAULT CHARSET=utf8;
10 
11 /*Table structure for table `orderdetail` */
12 
13 CREATE TABLE orderdetail (
14   id INT NOT NULL AUTO_INCREMENT,
15  orders_id INT NOT NULL COMMENT '订单id',
16   items_id INT NOT NULL COMMENT '商品id',
17   items_num INT  DEFAULT NULL COMMENT '商品购买数量',
18   PRIMARY KEY (id),
19   KEY `FK_orderdetail_1` (`orders_id`),
20   KEY `FK_orderdetail_2` (`items_id`),
21   CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
22   CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
23 )  DEFAULT CHARSET=utf8;
24 
25 /*Table structure for table `orders` */
26 
27 CREATE TABLE orders (
28   id INT NOT NULL AUTO_INCREMENT,
29   user_id INT NOT NULL COMMENT '下单用户id',
30   number VARCHAR(30) NOT NULL COMMENT '订单号',
31   createtime DATETIME NOT NULL COMMENT '创建订单时间',
32   note VARCHAR(100) DEFAULT NULL COMMENT '备注',
33   PRIMARY KEY (`id`),
34   KEY `FK_orders_1` (`user_id`),
35   CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
36 )  DEFAULT CHARSET=utf8;
37 
38 /*Table structure for table `t_user` */
39 
40 CREATE TABLE user (
41   id INT NOT NULL AUTO_INCREMENT,
42   username VARCHAR(32) NOT NULL COMMENT '用户名称',
43   birthday DATE DEFAULT NULL COMMENT '生日',
44   sex CHAR(1) DEFAULT NULL COMMENT '性别',
45   address  VARCHAR(256) DEFAULT NULL COMMENT '地址',
46   PRIMARY KEY (`id`)
47 ) DEFAULT CHARSET=utf8;

测试数据代码:

 1 /*Data for the table `items` */
 2 
 3 INSERT  INTO items(itemsname,price,detail,pic,createtime) VALUES 
 4 ('台式机',3000.0,'该电脑质量非常好!',NULL,'2015-07-07 13:28:53'),
 5 ('笔记本',6000.0,'笔记本性能好,质量好!',NULL,'2015-07-08 13:22:57'),
 6 ('背包',200.0,'名牌背包,容量大质量好!',NULL,'2015-07-010 13:25:02');
 7 
 8 /*Data for the table `orderdetail` */
 9 
10 INSERT  INTO `orderdetail`(`orders_id`,`items_id`,`items_num`) VALUES
11  (1,1,1),
12  (1,2,3),
13  (2,3,4),
14  (3,2,3);
15 
16 /*Data for the table `orders` */
17 
18 INSERT  INTO `orders`(`user_id`,`number`,`createtime`,`note`) VALUES 
19 (1,'1000010','2015-06-04 13:22:35',NULL),
20 (1,'1000011','2015-07-08 13:22:41',NULL),
21 (2,'1000012','2015-07-17 14:13:23',NULL),
22 (3,'1000012','2015-07-16 18:13:23',NULL),
23 (4,'1000012','2015-07-15 19:13:23',NULL),
24 (5,'1000012','2015-07-14 17:13:23',NULL),
25 (6,'1000012','2015-07-13 16:13:23',NULL);
26 
27 /*Data for the table `user` */
28 
29 INSERT  INTO `user`(`username`,`birthday`,`sex`,`address`) VALUES 
30 ('王五',NULL,'2',NULL),
31 ('张三','2014-07-10','1','北京市'),
32 ('张小明',NULL,'1','河南郑州'),
33 ('陈小明',NULL,'1','河南郑州'),
34 ('张三丰',NULL,'1','河南郑州'),
35 ('陈小明',NULL,'1','河南郑州'),
36 ('王五',NULL,NULL,NULL),
37  ('小A','2015-06-27','2','北京'),
38 ('小B','2015-06-27','2','北京'),
39 ('小C','2015-06-27','1','北京'),
40 ('小D','2015-06-27','2','北京');

以上是关于Mybatis学习笔记—高级映射,延迟加载的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis学习--延迟加载

Mybatis学习总结——延迟加载

MyBatis结果映射与MyBatis缓存初探学习记录

mybatis (高级映射 缓存 延迟加载)

Mybatis学习动态加载一二级缓存

JavaLearn#(28)MyBatis高级:无级联查询级联查询(立即加载结果映射延迟加载)多表连接查询MyBatis注解MyBatis运行原理面试题