1
00:00:01,820 --> 00:00:03,730
层超类型这个模式
2
00:00:03,740 --> 00:00:07,590
它是属于基本模式之一了
3
00:00:07,880 --> 00:00:10,270
第18章的基本模式之一
4
00:00:13,560 --> 00:00:14,590
定义是这样的
5
00:00:15,160 --> 00:00:16,070
(译文)改得比较多
6
00:00:16,200 --> 00:00:19,240
因为之前的翻译到后面了之后
7
00:00:19,550 --> 00:00:21,030
可能有点倦怠
8
00:00:21,360 --> 00:00:24,960
所以有些翻译就有点乱掉了
9
00:00:27,560 --> 00:00:28,680
某一类型
10
00:00:29,050 --> 00:00:34,610
就是充当一层里面
11
00:00:34,620 --> 00:00:36,370
所有类型的超类型
12
00:00:40,680 --> 00:00:42,250
所有的里面,最顶上
13
00:00:44,160 --> 00:00:46,110
平时我们见到最多像什么
14
00:00:46,120 --> 00:00:47,950
我们框架里面的object
15
00:00:47,960 --> 00:00:50,150
就是类似这样一个
16
00:00:50,880 --> 00:00:54,500
一会我们这里面也有一个domainobject
17
00:00:56,500 --> 00:00:59,740
领域对象,或者说,抽象映射器,一样的
18
00:00:59,750 --> 00:01:05,120
就是说有一个类充当所有类型的超类
19
00:01:06,010 --> 00:01:07,070
最顶上的超类
20
00:01:12,530 --> 00:01:14,730
作用就是如果说
21
00:01:14,740 --> 00:01:16,450
某一层里面所有对象
22
00:01:16,460 --> 00:01:17,730
都有某些方法
23
00:01:18,630 --> 00:01:21,260
然后把这些共同的东西
24
00:01:21,390 --> 00:01:24,850
把它移到超类里面去
25
00:01:29,690 --> 00:01:31,240
常见的就是这个了
26
00:01:31,250 --> 00:01:32,400
你看,这里也说了
27
00:01:32,850 --> 00:01:34,890
共有的特性常见
28
00:01:34,900 --> 00:01:40,410
主要是一个标识字段的存储
29
00:01:42,310 --> 00:01:50,990
这是一个,这是领域对象
30
00:01:51,580 --> 00:01:52,960
也是domainobject
31
00:01:52,970 --> 00:01:55,940
主要是干这个
32
00:01:56,980 --> 00:01:59,260
还有一个层超类型是什么
33
00:01:59,270 --> 00:02:00,740
抽象映射器的类型
34
00:02:01,440 --> 00:02:02,960
一会我们会看到
35
00:02:02,970 --> 00:02:09,160
它里面有负责一个SQL语句
36
00:02:10,140 --> 00:02:16,170
还有参数集合,这两个,由它在里面定义
37
00:02:20,620 --> 00:02:22,850
比如说,这是一个层超类型
38
00:02:23,410 --> 00:02:26,950
所有的领域对象都从这里继承下来
39
00:02:33,560 --> 00:02:34,360
这也是一个
40
00:02:38,670 --> 00:02:40,060
然后上面定义了一个
41
00:02:41,050 --> 00:02:45,340
大家都有的loadedmap
42
00:02:45,710 --> 00:02:49,720
就是映像的集合
43
00:02:52,770 --> 00:02:54,250
还定义了很多共同的东西
44
00:02:54,260 --> 00:02:57,250
查询的SQL语句
45
00:02:57,630 --> 00:03:01,930
插入的,等等
46
00:03:02,260 --> 00:03:03,970
然后由下面这个来实现
1
00:00:00,450 --> 00:00:04,900
接下来,我们来看插入的示例
2
00:00:09,530 --> 00:00:14,600
首先,它这里先new一个
3
00:00:15,010 --> 00:00:16,160
Person的实例
4
00:00:17,740 --> 00:00:20,230
这里的ID暂时设为0
5
00:00:20,790 --> 00:00:24,430
后面就是那几个属性的值了
6
00:00:26,640 --> 00:00:27,710
实例new完之后
7
00:00:27,720 --> 00:00:31,440
再把实例作为参数
8
00:00:31,830 --> 00:00:36,000
给mapper执行insert操作
9
00:00:37,710 --> 00:00:45,370
我们来看一下,领域类在这里
10
00:00:45,380 --> 00:00:51,760
然后实例化一个对象,ID等于0
11
00:00:56,800 --> 00:00:59,400
然后就到insert这里了
12
00:01:03,860 --> 00:01:09,260
这里参数是一个领域对象的类型
13
00:01:10,740 --> 00:01:14,160
就是说,不管你是Person也可以
14
00:01:14,170 --> 00:01:16,200
或者其他领域对象也可以
15
00:01:16,210 --> 00:01:17,800
因为它们的共同超类
16
00:01:17,810 --> 00:01:19,200
就是domainobject
17
00:01:22,750 --> 00:01:24,900
创建并打开数据库连接,这一样的
18
00:01:29,340 --> 00:01:32,950
好,下面这个跟前面类似
19
00:01:33,300 --> 00:01:38,210
就是说,它这个SQL语句等等
20
00:01:38,420 --> 00:01:41,770
也是通过一个方法来构造的
21
00:01:45,160 --> 00:01:51,130
然后用它来创建数据库命令实例
22
00:01:52,850 --> 00:01:56,980
而这个也跟前面一样,在超类里面定义
23
00:01:57,370 --> 00:01:58,940
然后在子类里面实现
24
00:01:59,280 --> 00:02:01,260
所以你看,我们下面这个
25
00:02:02,730 --> 00:02:09,810
通过这个来构造SQL
26
00:02:09,820 --> 00:02:11,610
作为参数传进去
27
00:02:12,150 --> 00:02:13,800
来构造 command
28
00:02:16,250 --> 00:02:18,700
首先是这个了
29
00:02:22,710 --> 00:02:23,920
那么这个的定义
30
00:02:23,930 --> 00:02:32,600
在抽象的这里
31
00:02:32,610 --> 00:02:35,360
作为一个抽象方法在这里定义
32
00:02:37,350 --> 00:02:42,170
然后具体怎么构造
33
00:02:42,900 --> 00:02:44,740
就放在各个子类里面
34
00:02:46,050 --> 00:02:49,750
所以在这里面,你看,这里构造
35
00:02:51,540 --> 00:02:54,940
覆盖了超类的方法
36
00:02:54,950 --> 00:03:01,710
然后这里有4个参数
37
00:03:07,010 --> 00:03:07,890
好,得到这个了
38
00:03:08,190 --> 00:03:10,810
然后构造一个command
39
00:03:11,290 --> 00:03:14,600
然后下面这个是一样的
40
00:03:17,340 --> 00:03:18,980
之前的ID是0
41
00:03:19,540 --> 00:03:24,400
现在又把它设成我们算出来ID了
42
00:03:24,410 --> 00:03:28,110
ID这里面就是跟之前一样了
43
00:03:28,120 --> 00:03:30,750
就是领域对象的下一个ID也是这样
44
00:03:30,760 --> 00:03:32,270
用这种方法来获得的
45
00:03:35,960 --> 00:03:39,180
这个地方跟前面行数据入口
46
00:03:39,190 --> 00:03:44,450
和活动记录示例是一样的,没有区别
47
00:03:55,980 --> 00:03:57,410
Id有了,然后把这ID
48
00:03:57,880 --> 00:04:01,410
先把它添加到command的参数
49
00:04:02,460 --> 00:04:03,690
创建参数
50
00:04:03,700 --> 00:04:05,050
然后给参数赋值
51
00:04:05,720 --> 00:04:07,060
类型是整形
52
00:04:07,070 --> 00:04:09,820
值是这个,添加进去
53
00:04:12,310 --> 00:04:12,800
好
54
00:04:18,180 --> 00:04:26,050
添加了这个了,然后再把这个领域对象
55
00:04:26,060 --> 00:04:27,730
和Command作为参数
56
00:04:28,440 --> 00:04:31,140
调用这个操作
57
00:04:34,740 --> 00:04:40,610
我们来看这个操作
58
00:04:40,620 --> 00:04:42,370
也是在超类里面定义的
59
00:04:50,910 --> 00:04:53,390
参数就是一个领域对象
60
00:04:53,400 --> 00:04:58,370
然后一个DBCommand,在这里定义
61
00:05:01,880 --> 00:05:04,690
然后这里,具体实现放在这里
62
00:05:06,740 --> 00:05:08,330
首先把这个领域对象
63
00:05:08,340 --> 00:05:10,280
变成Person对象
64
00:05:11,740 --> 00:05:19,180
把它的值
65
00:05:19,730 --> 00:05:21,920
这里定义参数
66
00:05:22,950 --> 00:05:26,260
然后把它的参数的值
67
00:05:26,270 --> 00:05:28,620
设成Person的各个属性的值
68
00:05:29,150 --> 00:05:33,030
一个一个这样把它,参数一参数二参数三
69
00:05:34,290 --> 00:05:36,720
一个一个插入
70
00:05:43,940 --> 00:05:44,210
好
71
00:05:45,300 --> 00:05:47,860
就说给Command的参数
72
00:05:48,570 --> 00:05:50,680
一个个添加到参数集合里面去
73
00:05:55,670 --> 00:06:01,020
这个只是构造
74
00:06:01,190 --> 00:06:03,860
给Command设置参数等等
75
00:06:04,030 --> 00:06:05,660
没有执行,下面才是执行
76
00:06:06,220 --> 00:06:11,840
到这一步才真正执行,执行完了
77
00:06:12,310 --> 00:06:14,320
相当于这条记录
78
00:06:14,890 --> 00:06:17,740
这条记录,这个新的人
79
00:06:17,750 --> 00:06:21,340
已经添加到数据库里面去了
80
00:06:22,040 --> 00:06:25,910
然后这时候还要把它添加到标识映射
81
00:06:25,920 --> 00:06:30,070
loadedmap这个,添加进去
82
00:06:31,700 --> 00:06:32,940
键就是ID
83
00:06:32,950 --> 00:06:34,940
然后这个是对象
84
00:06:34,950 --> 00:06:39,310
然后返回ID