Skip to content

Commit fe8bb71

Browse files
committed
更新文档,增加 wrapper 介绍
1 parent 98f778b commit fe8bb71

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

README.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
![logo](logo.png)
2+
23
# MyBatis Mapper
34

45
基于 **mybatis-mapper/provider**( [gitee](https://gitee.com/mybatis-mapper/provider)
@@ -22,7 +23,8 @@
2223
### 1.2 系统要求
2324

2425
MyBatis Mapper 要求 MyBatis 最低版本为
25-
3.5.1,推荐使用最新版本 <a href="https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis"><img src="https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis/badge.svg"/></a>。
26+
3.5.1,推荐使用最新版本 <a href="https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis"><img src="https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis/badge.svg"/></a>
27+
2628

2729
和 MyBatis 框架一样,最低需要 Java 8。
2830

@@ -32,6 +34,7 @@ MyBatis Mapper 要求 MyBatis 最低版本为
3234
<CodeGroupItem title="Maven" active>
3335

3436
```xml
37+
3538
<dependencies>
3639
<dependency>
3740
<groupId>io.mybatis</groupId>
@@ -58,11 +61,11 @@ MyBatis Mapper 要求 MyBatis 最低版本为
5861

5962
```groovy
6063
dependencies {
61-
compile("io.mybatis:mybatis-mapper:1.2.0")
62-
// 使用 Service 层封装时
63-
compile("io.mybatis:mybatis-service:1.2.0")
64-
// 使用 ActiveRecord 模式时
65-
compile("io.mybatis:mybatis-activerecord:1.2.0")
64+
compile("io.mybatis:mybatis-mapper:1.2.0")
65+
// 使用 Service 层封装时
66+
compile("io.mybatis:mybatis-service:1.2.0")
67+
// 使用 ActiveRecord 模式时
68+
compile("io.mybatis:mybatis-activerecord:1.2.0")
6669
}
6770
```
6871

@@ -71,12 +74,12 @@ dependencies {
7174

7275
### 1.4 快速设置
7376

74-
MyBatis Mapper 的基本原理是将实体类映射为数据库中的表和字段信息,因此实体类需要通过注解配置基本的元数据,配置好实体后,
75-
只需要创建一个继承基础接口的 Mapper 接口就可以开始使用了。
77+
MyBatis Mapper 的基本原理是将实体类映射为数据库中的表和字段信息,因此实体类需要通过注解配置基本的元数据,配置好实体后, 只需要创建一个继承基础接口的 Mapper 接口就可以开始使用了。
7678

7779
#### 1.4.1 实体类配置
7880

7981
假设有一个表:
82+
8083
```sql
8184
create table user
8285
(
@@ -85,7 +88,9 @@ create table user
8588
sex VARCHAR(2)
8689
);
8790
```
91+
8892
对应的实体类:
93+
8994
```java
9095
import io.mybatis.provider.Entity;
9196

@@ -102,11 +107,11 @@ public class User {
102107
}
103108
```
104109

105-
实体类上 **必须添加** `@Entity.Table` 注解指定实体类对应的表名,建议明确指定表名,不提供表名的时候,使用类名作为表名。
106-
所有属于表中列的字段,**必须添加** `@Entity.Column` 注解,不指定列名时,使用字段名(不做任何转换),通过 `id=true` 可以标记字段为主键。
110+
实体类上 **必须添加** `@Entity.Table` 注解指定实体类对应的表名,建议明确指定表名,不提供表名的时候,使用类名作为表名。 所有属于表中列的字段,**必须添加** `@Entity.Column`
111+
注解,不指定列名时,使用字段名(不做任何转换),通过 `id=true` 可以标记字段为主键。
107112

108-
>`@Entity` 中包含的这两个注解提供了大量的配置属性,想要使用更多的配置,参考下面 **3. @Entity 注解** 的内容,
109-
>下面是一个简单示例:
113+
> `@Entity` 中包含的这两个注解提供了大量的配置属性,想要使用更多的配置,参考下面 **3. @Entity 注解** 的内容,
114+
> 下面是一个简单示例:
110115
>```java
111116
>@Entity.Table(value = "sys_user", remark = "系统用户", autoResultMap = true)
112117
>public class User {
@@ -121,12 +126,14 @@ public class User {
121126
#### 1.4.2 Mapper接口定义
122127
123128
有了 `User` 实体后,直接创建一个继承了 `Mapper` 的接口即可:
129+
124130
```java
125131
//io.mybatis.mapper.Mapper
126132
public interface UserMapper extends Mapper<User, Long> {
127-
133+
128134
}
129135
```
136+
130137
这个接口只要被 MyBatis 扫描到即可直接使用。
131138

132139
> 下面是几种常见的扫描配置:
@@ -165,24 +172,45 @@ public interface UserMapper extends Mapper<User, Long> {
165172
#### 1.4.3 使用
166173
167174
定义好接口后,就可以获取 `UserMapper` 使用,下面是简单示例:
175+
168176
```java
169-
User user = new User();
170-
user.setUserName("测试");
171-
userMapper.insert(user);
177+
User user=new User();
178+
user.setUserName("测试");
179+
userMapper.insert(user);
172180
//保存后自增id回写,不为空
173-
Assert.assertNotNull(user.getId());
181+
Assert.assertNotNull(user.getId());
174182
//根据id查询
175-
user = userMapper.selectByPrimaryKey(user.getId());
183+
user=userMapper.selectByPrimaryKey(user.getId());
176184
//删除
177-
Assert.assertEquals(1, userMapper.deleteByPrimaryKey(user.getId()));
185+
Assert.assertEquals(1,userMapper.deleteByPrimaryKey(user.getId()));
178186
```
179187
180-
看到这里,可以发现除了 MyBatis 自身的配置外,MyBatis Mapper 只需要配置实体类注解,
181-
创建对应的 Mapper 接口就可以直接使用,没有任何繁琐的配置。
188+
看到这里,可以发现除了 MyBatis 自身的配置外,MyBatis Mapper 只需要配置实体类注解, 创建对应的 Mapper 接口就可以直接使用,没有任何繁琐的配置。
189+
190+
上面的示例只是简单的使用了 MyBatis Mapper,还有很多开箱即用的功能没有涉及, 建议在上述示例运行成功后,继续查看本项目其他模块的详细文档,熟悉各部分文档后, 在使用 MyBatis Mapper 时会更得心应手,随心所欲。
191+
192+
### 1.4.4 wrapper 用法
193+
194+
在 1.2.0 版本之后,针对 Example 封装了一个 ExampleWrapper,可以通过链式调用方便的使用 Example 方法。
195+
196+
```java
197+
mapper.wrapper()
198+
.eq(User::getSex,"")
199+
.or(c->c.gt(User::getId,40),c->c.lt(User::getId,10))
200+
.or()
201+
.startsWith(User::getUserName,"").list();
202+
```
203+
204+
对应的 SQL 如下:
205+
206+
```sql
207+
SELECT id, name AS userName, sex
208+
FROM user
209+
WHERE (sex = ? AND ((id > ?) OR (id < ?)))
210+
OR (name LIKE ?)
211+
```
182212

183-
上面的示例只是简单的使用了 MyBatis Mapper,还有很多开箱即用的功能没有涉及,
184-
建议在上述示例运行成功后,继续查看本项目其他模块的详细文档,熟悉各部分文档后,
185-
在使用 MyBatis Mapper 时会更得心应手,随心所欲。
213+
详细的介绍可以查看 [1.2.0 更新日志](https://mapper.mybatis.io/releases/1.2.0.html)
186214

187215
## 2. 示例项目
188216

0 commit comments

Comments
 (0)