Skip to content

Commit 9ee2a2e

Browse files
committed
完善文档
1 parent a5e5881 commit 9ee2a2e

File tree

4 files changed

+82
-94
lines changed

4 files changed

+82
-94
lines changed

wiki/mapper3/1.Changes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
##细化接口,拆分为一
1414

15-
`Mapper<T>`包含了很多通用的方法,但并不是所有人都需要这些方法,也行其中的某些方法不需要,不想用,这在Mapper2.x是没法解决的。
15+
`Mapper<T>`包含了很多通用的方法,但并不是所有人都需要这些方法,也许其中的某些方法不需要,不想用,这在Mapper2.x是没法解决的。
1616

1717
还有一种情况就是,如果我的业务中需要一个我自己通用的接口方法,如果我开发了一个自己的接口,我可能还要给所有已经存在的Mapper接口(如`CountryMapper`)去继承该接口。
1818

1919
Mapper3的主要目标就是解决上面两个问题。
2020

2121
首先是将`Mapper<T>`接口细化,拆分,每一个原接口的方法,都独立为一个接口。
2222

23-
例如`List<T> select(T record);`方法原来只是`Mapper<T>`中的一个方法,现成成了`SelectMapper<T>`接口中的唯一方法:
23+
例如`List<T> select(T record);`方法原来只是`Mapper<T>`中的一个方法,现成了`SelectMapper<T>`接口中的唯一方法:
2424

2525
```java
2626
public interface SelectMapper<T> {
@@ -93,7 +93,7 @@ public interface Mapper<T> extends
9393

9494
##继承接口自动注册,只需要配置基础接口
9595

96-
这句话说着也不顺,举个例子
96+
这个标题看着不顺,举个例子说明
9797

9898
###第一种
9999

@@ -105,7 +105,7 @@ public interface Mapper<T> extends
105105

106106
###第二种
107107

108-
如果我创建了自己的`com.xxx.MyMapper<T>`,并且项目中只用到了自己的`com.xxx.MyMapper<T>`,那么只需要配置`mappers=com.xxx.MyMapper<T>`即可。
108+
如果我创建了自己的`com.xxx.MyMapper<T>`,并且项目中只用到了自己的`com.xxx.MyMapper<T>`,那么只需要配置`mappers=com.xxx.MyMapper`即可。
109109

110110
从这一点应该很容易看出来,项目中的代码和通用Mapper完全解耦,如果你不喜欢代码中包含`com.github.abel533`奇怪的包名,你可以自己创建一个基础接口。
111111

wiki/mapper3/3.Use.md

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,7 @@ public interface UserInfoMapper extends Mapper<UserInfo> {
1515
}
1616
```
1717

18-
一旦继承了`Mapper<T>`,继承的`Mapper`就拥有了以下通用的方法:
19-
20-
```java
21-
//根据实体类不为null的字段进行查询,条件全部使用=号and条件
22-
List<T> select(T record);
23-
24-
//根据实体类不为null的字段查询总数,条件全部使用=号and条件
25-
int selectCount(T record);
26-
27-
//根据主键进行查询,必须保证结果唯一
28-
//单个字段做主键时,可以直接写主键的值
29-
//联合主键时,key可以是实体类,也可以是Map
30-
T selectByPrimaryKey(Object key);
31-
32-
//插入一条数据
33-
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
34-
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
35-
int insert(T record);
36-
37-
//插入一条数据,只插入不为null的字段,不会影响有默认值的字段
38-
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
39-
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
40-
int insertSelective(T record);
41-
42-
//根据实体类中字段不为null的条件进行删除,条件全部使用=号and条件
43-
int delete(T key);
44-
45-
//通过主键进行删除,这里最多只会删除一条数据
46-
//单个字段做主键时,可以直接写主键的值
47-
//联合主键时,key可以是实体类,也可以是Map
48-
int deleteByPrimaryKey(Object key);
49-
50-
//根据主键进行更新,这里最多只会更新一条数据
51-
//参数为实体类
52-
int updateByPrimaryKey(T record);
53-
54-
//根据主键进行更新
55-
//只会更新不是null的数据
56-
int updateByPrimaryKeySelective(T record);
57-
58-
//根据Exmaple条件查询总数
59-
int selectCountByExample(Object example);
60-
61-
//根据Exmaple条件删除
62-
int deleteByExample(Object example);
63-
64-
//根据Exmaple条件查询
65-
List<T> selectByExample(Object example);
66-
67-
//根据Exmaple条件更新非空(null)字段
68-
int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
69-
70-
//根据Exmaple条件更新全部字段
71-
int updateByExample(@Param("record") T record, @Param("example") Object example);
72-
73-
```
18+
一旦继承了`Mapper<T>`,继承的`Mapper`就拥有了`Mapper<T>`所有的通用方法。
7419

7520
##2. 泛型(实体类)`<T>`的类型必须符合要求
7621

@@ -188,13 +133,8 @@ private Integer id;
188133
```xml
189134
<mappers>
190135
<mapper class="com.github.abel533.mapper.CountryMapper" />
191-
<mapper class="com.github.abel533.mapper.Country2Mapper" />
192-
<mapper class="com.github.abel533.mapper.CountryTMapper" />
193-
<mapper class="com.github.abel533.mapper.CountryUMapper" />
194-
<mapper class="com.github.abel533.mapper.CountryIMapper" />
195136
<mapper class="com.github.abel533.mapper.UserInfoMapper" />
196137
<mapper class="com.github.abel533.mapper.UserLoginMapper" />
197-
<mapper class="com.github.abel533.mapper.UserLogin2Mapper" />
198138
</mappers>
199139
```
200140

wiki/mapper3/4.Professional.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,53 @@ public interface AllMapper<T> extends
110110

111111
然后配置`mappers=com.xxx.xxx.AllMapper`即可。
112112

113+
##高级重定义方法
114+
115+
这里举一个复杂的例子。在<b>special特殊接口</b>中有一个批量插入的方法。
116+
117+
```java
118+
public interface InsertListMapper<T> {
119+
120+
/**
121+
* 批量插入,支持数据库自增字段,支持回写
122+
*
123+
* @param recordList
124+
* @return
125+
*/
126+
@Options(useGeneratedKeys = true, keyProperty = "id")
127+
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
128+
int insertList(List<T> recordList);
129+
130+
/**
131+
* ======如果主键不是id怎么用?==========
132+
* 假设主键的属性名是uid,那么新建一个Mapper接口如下
133+
* <pre>
134+
public interface InsertUidListMapper<T> {
135+
@Options(useGeneratedKeys = true, keyProperty = "uid")
136+
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
137+
int insertList(List<T> recordList);
138+
}
139+
* 只要修改keyProperty = "uid"就可以
140+
*
141+
* 然后让你自己的Mapper继承InsertUidListMapper<T>即可
142+
*
143+
* </pre>
144+
*/
145+
}
146+
```
147+
148+
这里注释提到了一种情况,那就是<b>如果主键不是id怎么用?</b>
149+
150+
按照注释中所说的,你可以自己创建一个Mapper接口,然后修改`keyProperty`属性,如下:
151+
152+
```java
153+
public interface InsertUidListMapper<T> {
154+
@Options(useGeneratedKeys = true, keyProperty = "uid")
155+
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
156+
int insertList(List<T> recordList);
157+
}
158+
```
159+
113160
##Mapper3通用接口大全
114161

115162
Mapper3提供的全部接口,一可以看源码,二可以看[Mapper3通用接口大全](http://git.oschina.net/free/Mapper/blob/master/wiki/mapper3/5.Mappers.md)

wiki/mapper3/5.Mappers.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,143 @@
22

33
Mapper3接口有两种形式,一种是提供了一个方法的接口。还有一种是不提供方法,但是继承了多个单方法的接口,一般是某类方法的集合。
44

5-
例如`SelectMapper`是一个单方法的接口,`BaseSelectMapper`是一个继承了4个基础查询方法的接口。
5+
例如`SelectMapper<T>`是一个单方法的接口,`BaseSelectMapper<T>`是一个继承了4个基础查询方法的接口。
66

77
##基础接口
88

99
###Select
1010

11-
接口:`SelectMapper`<br>
11+
接口:`SelectMapper<T>`<br>
1212
方法:`List<T> select(T record);`<br>
1313
说明:根据实体中的属性值进行查询,查询条件使用等号<br><br>
1414

15-
接口:`SelectByPrimaryKeyMapper`<br>
15+
接口:`SelectByPrimaryKeyMapper<T>`<br>
1616
方法:`T selectByPrimaryKey(Object key);`<br>
1717
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号<br>
1818
<br>
1919

20-
接口:`SelectOneMapper`<br>
20+
接口:`SelectOneMapper<T>`<br>
2121
方法:`T selectOne(T record);`<br>
2222
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号<br><br>
2323

24-
接口:`SelectCountMapper`<br>
24+
接口:`SelectCountMapper<T>`<br>
2525
方法:`int selectCount(T record);`<br>
2626
说明:根据实体中的属性查询总数,查询条件使用等号<br><br>
2727

2828
###Insert
2929

30-
接口:`InsertMapper`<br>
30+
接口:`InsertMapper<T>`<br>
3131
方法:`int insert(T record);`<br>
3232
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值。<br><br>
3333

34-
接口:`InsertSelectiveMapper`<br>
34+
接口:`InsertSelectiveMapper<T>`<br>
3535
方法:`int insertSelective(T record);`<br>
3636
说明:保存一个实体,null的属性不会保存,会使用数据库默认值。<br><br>
3737

3838
###Update
3939

40-
接口:`UpdateByPrimaryKeyMapper`<br>
40+
接口:`UpdateByPrimaryKeyMapper<T>`<br>
4141
方法:`int updateByPrimaryKey(T record);`<br>
4242
说明:根据主键更新实体,null值会被更新。<br><br>
4343

44-
接口:`UpdateByPrimaryKeySelectiveMapper`<br>
44+
接口:`UpdateByPrimaryKeySelectiveMapper<T>`<br>
4545
方法:`int updateByPrimaryKeySelective(T record);`<br>
4646
说明:根据主键更新属性不为null的值。<br><br>
4747

4848
###Delete
4949

50-
接口:`DeleteMapper`<br>
50+
接口:`DeleteMapper<T>`<br>
5151
方法:`int delete(T record);`<br>
5252
说明:根据实体属性作为条件进行删除,查询条件使用等号<br><br>
5353

54-
接口:`DeleteByPrimaryKeyMapper`<br>
54+
接口:`DeleteByPrimaryKeyMapper<T>`<br>
5555
方法:`int deleteByPrimaryKey(Object key);`<br>
5656
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性<br><br>
5757

5858
###base组合接口
5959

60-
接口:`BaseSelectMapper`<br>
60+
接口:`BaseSelectMapper<T>`<br>
6161
方法:包含上面Select的4个方法<br><br>
6262

63-
接口:`BaseInsertMapper`<br>
63+
接口:`BaseInsertMapper<T>`<br>
6464
方法:包含上面Insert的2个方法<br><br>
6565

66-
接口:`BaseUpdateMapper`<br>
66+
接口:`BaseUpdateMapper<T>`<br>
6767
方法:包含上面Update的2个方法<br><br>
6868

69-
接口:`BaseDeleteMapper`<br>
69+
接口:`BaseDeleteMapper<T>`<br>
7070
方法:包含上面Delete的2个方法<br><br>
7171

7272
###CRUD组合接口
7373

74-
接口:`BaseMapper`<br>
74+
接口:`BaseMapper<T>`<br>
7575
方法:继承了base组合接口中的4个组合接口,包含完整的CRUD方法<br><br>
7676

7777
##Example方法
7878

79-
接口:`SelectByExampleMapper`<br>
79+
接口:`SelectByExampleMapper<T>`<br>
8080
方法:`List<T> selectByExample(Object example);`<br>
8181
说明:根据Example条件进行查询<br><br>
8282

83-
接口:`SelectCountByExampleMapper`<br>
83+
接口:`SelectCountByExampleMapper<T>`<br>
8484
方法:`int selectCountByExample(Object example);`<br>
8585
说明:根据Example条件进行查询总数<br><br>
8686

87-
接口:`UpdateByExampleMapper`<br>
87+
接口:`UpdateByExampleMapper<T>`<br>
8888
方法:`int updateByExample(@Param("record") T record, @Param("example") Object example);`<br>
8989
说明:根据Example条件更新实体`record`包含的属性,null值会被更新<br><br>
9090

91-
接口:`UpdateByExampleSelectiveMapper`<br>
91+
接口:`UpdateByExampleSelectiveMapper<T>`<br>
9292
方法:`int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);`<br>
9393
说明:根据Example条件更新实体`record`包含的不是null的属性值<br><br>
9494

95-
接口:`DeleteByExampleMapper`<br>
95+
接口:`DeleteByExampleMapper<T>`<br>
9696
方法:`int deleteByExample(Object example);`<br>
9797
说明:根据Example条件删除数据<br><br>
9898

9999
###Example组合接口
100100

101-
接口:`ExampleMapper`<br>
101+
接口:`ExampleMapper<T>`<br>
102102
方法:包含上面Example中的5个方法<br><br>
103103

104104
##RowBounds
105105

106106
默认为<b>内存分页</b>,可以配合[PageHelper](http://git.oschina.net/free/Mybatis_PageHelper)实现物理分页
107107

108-
接口:`SelectRowBoundsMapper`<br>
108+
接口:`SelectRowBoundsMapper<T>`<br>
109109
方法:`List<T> selectByRowBounds(T record, RowBounds rowBounds);`<br>
110110
说明:根据实体属性和RowBounds进行分页查询<br><br>
111111

112-
接口:`SelectByExampleRowBoundsMapper`<br>
112+
接口:`SelectByExampleRowBoundsMapper<T>`<br>
113113
方法:`List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);`<br>
114114
说明:根据example条件和RowBounds进行分页查询<br><br>
115115

116116
###RowBounds组合接口
117117

118-
接口:`RowBoundsMapper`<br>
118+
接口:`RowBoundsMapper<T>`<br>
119119
方法:包含上面RowBounds中的2个方法<br><br>
120120

121121
##special特殊接口
122122

123123
这些接口针对部分数据库设计,不是所有数据库都支持
124124

125-
接口:`InsertListMapper`<br>
125+
接口:`InsertListMapper<T>`<br>
126126
方法:`int insertList(List<T> recordList);`<br>
127127
说明:批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列<br><br>
128128

129-
接口:`InsertUseGeneratedKeysMapper`<br>
129+
接口:`InsertUseGeneratedKeysMapper<T>`<br>
130130
方法:`int InsertUseGeneratedKeysMapper(T record);`<br>
131131
说明:插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效<br><br>
132132

133133
##MySQL专用
134134

135-
接口:`MySqlMapper`<br>
135+
接口:`MySqlMapper<T>`<br>
136136
继承方法:`int insertList(List<T> recordList);`<br>
137137
继承方法:`int InsertUseGeneratedKeysMapper(T record);`<br>
138-
说明:该接口不包含方法,继承了special中的`InsertListMapper``InsertUseGeneratedKeysMapper`<br><br>
138+
说明:该接口不包含方法,继承了special中的`InsertListMapper<T>``InsertUseGeneratedKeysMapper<T>`<br><br>
139139

140140
##Mapper<T>接口
141141

142+
接口:`Mapper<T>`<br>
142143
该接口兼容Mapper2.x版本,继承了`BaseMapper<T>`, `ExampleMapper<T>`, `RowBoundsMapper<T>`三个组合接口。
143144

0 commit comments

Comments
 (0)