Skip to content

Commit f2ee820

Browse files
committed
先添加 v2.x 版本日志(从v1.x复制),后续逐步修改完善
1 parent 5147d69 commit f2ee820

9 files changed

+2490
-0
lines changed

.vuepress/config.js

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ module.exports = {
7575
sidebar: {
7676
'/docs/': [
7777
{
78+
title: 'v2.x 使用指南',
79+
sidebarDepth: 2,
80+
children: [
81+
'v2.x/1.getting-started.md',
82+
'v2.x/2.mybatis-mapper.md',
83+
'v2.x/3.entity.md',
84+
'v2.x/4.common.md',
85+
'v2.x/5.service.md',
86+
'v2.x/6.activerecord.md',
87+
'v2.x/7.generator.md',
88+
'v2.x/8.jpa.md'
89+
]
90+
},{
7891
title: 'v1.x 使用指南',
7992
sidebarDepth: 2,
8093
children: [

docs/v2.x/1.getting-started.md

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
---
2+
title: 1.快速上手
3+
---
4+
5+
## 介绍
6+
7+
这是一个不需要任何配置就可以直接使用的通用 Mapper,通过简单的学习就可以直接在项目中使用。
8+
9+
## 1.1 主要目标
10+
11+
1. 开箱即用,无需任何配置,继承基类 Mapper 即可获得大量通用方法;
12+
2. 随心所欲,通过复制粘贴的方式可以组建自己的基类 Mapper;
13+
3. 全面贴心,提供 Service 层的封装方便业务使用和理解 Mapper;
14+
4. 简单直观,提供 ActiveRecord 模式,结合 Spring Boot 自动配置直接上手用;
15+
5. 自定义方法,简单几行代码即可实现自定义通用方法;
16+
6. 轻松扩展,通过 Java SPI 轻松扩展。
17+
18+
## 1.2 系统要求
19+
20+
MyBatis Mapper 要求 MyBatis 最低版本为
21+
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>。
22+
23+
和 MyBatis 框架一样,最低需要 Java 8。
24+
25+
## 1.3 安装
26+
27+
如果你使用 Maven,在你的 `pom.xml` 添加下面的依赖:
28+
29+
```xml
30+
<dependencies>
31+
<dependency>
32+
<groupId>io.mybatis</groupId>
33+
<artifactId>mybatis-mapper</artifactId>
34+
<version>2.2.1</version>
35+
</dependency>
36+
<!-- TODO 按需选择依赖 -->
37+
<!-- 使用 Service 层封装时 -->
38+
<dependency>
39+
<groupId>io.mybatis</groupId>
40+
<artifactId>mybatis-service</artifactId>
41+
<version>2.2.1</version>
42+
</dependency>
43+
<!-- TODO 按需选择依赖 -->
44+
<!-- 使用 ActiveRecord 模式时 -->
45+
<dependency>
46+
<groupId>io.mybatis</groupId>
47+
<artifactId>mybatis-activerecord</artifactId>
48+
<version>2.2.1</version>
49+
</dependency>
50+
</dependencies>
51+
```
52+
53+
**支持 jakarta.persistence-api**
54+
55+
添加依赖:
56+
```xml
57+
<dependency>
58+
<groupId>io.mybatis</groupId>
59+
<artifactId>mybatis-jakarta-jpa</artifactId>
60+
<version>2.2.1</version>
61+
</dependency>
62+
```
63+
64+
如果使用 Gradle,在 `build.gradle` 中添加:
65+
66+
```groovy
67+
dependencies {
68+
compile("io.mybatis:mybatis-mapper:2.2.1")
69+
// 使用 Service 层封装时
70+
compile("io.mybatis:mybatis-service:2.2.1")
71+
// 使用 ActiveRecord 模式时
72+
compile("io.mybatis:mybatis-activerecord:2.2.1")
73+
}
74+
```
75+
76+
**支持 jakarta.persistence-api**
77+
78+
添加依赖:
79+
```groovy
80+
compile("io.mybatis:mybatis-jakarta-jpa:2.2.1")
81+
```
82+
83+
## 1.4 快速设置
84+
85+
MyBatis Mapper 的基本原理是将实体类映射为数据库中的表和字段信息,因此实体类需要通过注解配置基本的元数据,配置好实体后,
86+
只需要创建一个继承基础接口的 Mapper 接口就可以开始使用了。
87+
88+
### 1.4.1 实体类配置
89+
90+
假设有一个表:
91+
```sql
92+
create table user
93+
(
94+
id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
95+
name VARCHAR(32) DEFAULT 'DEFAULT',
96+
sex VARCHAR(2)
97+
);
98+
```
99+
对应的实体类:
100+
```java
101+
import io.mybatis.provider.Entity;
102+
103+
@Entity.Table("user")
104+
public class User {
105+
@Entity.Column(id = true)
106+
private Long id;
107+
@Entity.Column("name")
108+
private String username;
109+
@Entity.Column
110+
private String sex;
111+
112+
//省略set和get方法
113+
}
114+
```
115+
116+
实体类上 **必须添加** `@Entity.Table` 注解指定实体类对应的表名,建议明确指定表名,不提供表名的时候,使用类名作为表名。
117+
所有属于表中列的字段,**必须添加** `@Entity.Column` 注解,不指定列名时,使用字段名(不做任何转换),通过 `id=true` 可以标记字段为主键。
118+
119+
::: tip 复杂示例
120+
`@Entity` 中包含的这两个注解提供了大量的配置属性,想要使用更多的配置,参考下面 **3. @Entity 注解** 的内容,
121+
下面是一个简单示例:
122+
```java
123+
@Entity.Table(value = "sys_user", remark = "系统用户", autoResultMap = true)
124+
public class User {
125+
@Entity.Column(id = true, remark = "主键", updatable = false, insertable = false)
126+
private Long id;
127+
@Entity.Column(value = "name", remark = "帐号")
128+
private String userName;
129+
//省略其他
130+
}
131+
```
132+
:::
133+
134+
### 1.4.2 Mapper接口定义
135+
136+
有了 `User` 实体后,直接创建一个继承了 `Mapper` 的接口即可:
137+
```java
138+
//io.mybatis.mapper.Mapper
139+
public interface UserMapper extends Mapper<User, Long> {
140+
141+
}
142+
```
143+
这个接口只要被 MyBatis 扫描到即可直接使用。
144+
145+
下面是几种常见的扫描配置:
146+
147+
1. MyBatis 自带的配置文件方式 `mybatis-config.xml`
148+
```xml
149+
<mappers>
150+
<!-- 扫描指定的包 -->
151+
<package name="com.example.mapper"/>
152+
</mappers>
153+
```
154+
155+
2. Spring 中的 `spring.xml` 配置:
156+
```xml
157+
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
158+
<property name="basePackage" value="com.example.mapper"/>
159+
<property name="markerInterface" value="io.mybatis.service.mapper.RoleMarker"/>
160+
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryRole"/>
161+
</bean>
162+
```
163+
164+
3. Spring Boot 配置,启动类注解方式:
165+
```java
166+
@MapperScan(basePackages = "com.example.mapper")
167+
@SpringBootApplication
168+
public class SpringBootDemoApplication {
169+
170+
public static void main(String[] args) {
171+
SpringApplication.run(SpringBootDemoApplication.class, args);
172+
}
173+
174+
}
175+
```
176+
177+
Spring Boot 中,还可以直接给接口添加 `@org.apache.ibatis.annotations.Mapper` 注解,增加注解后可以省略 `@MapperScan` 配置。
178+
179+
::: tip 配置说明
180+
可以看到上面都是 MyBatis 的常规配置方式,配置方式中没有和 mybatis-mapper 有关的内容,
181+
新版本 mybatis-mapper 本身无需任何配置即可使用,没有魔改 MyBatis,
182+
没有拦截器,没有启动时的动态修改 SQL,完全基于 MyBatis 自身逻辑进行实现。
183+
:::
184+
185+
### 1.4.3 使用
186+
187+
定义好接口后,就可以获取 `UserMapper` 使用,下面是简单示例:
188+
```java
189+
User user = new User();
190+
user.setUserName("测试");
191+
userMapper.insert(user);
192+
//保存后自增id回写,不为空
193+
Assert.assertNotNull(user.getId());
194+
//根据id查询
195+
user = userMapper.selectByPrimaryKey(user.getId());
196+
//删除
197+
Assert.assertEquals(1, userMapper.deleteByPrimaryKey(user.getId()));
198+
```
199+
200+
看到这里,可以发现除了 MyBatis 自身的配置外,MyBatis Mapper 只需要配置实体类注解,
201+
创建对应的 Mapper 接口就可以直接使用,没有任何繁琐的配置。
202+
203+
上面的示例只是简单的使用了 MyBatis Mapper,还有很多开箱即用的功能没有涉及,
204+
建议在上述示例运行成功后,继续查看本项目其他模块的详细文档,熟悉各部分文档后,
205+
在使用 MyBatis Mapper 时会更得心应手,随心所欲。
206+
207+
### 1.4.4 wrapper 用法
208+
209+
在 1.2.0 版本之后,针对 Example 封装了一个 ExampleWrapper,可以通过链式调用方便的使用 Example 方法。
210+
211+
```java
212+
mapper.wrapper()
213+
.eq(User::getSex, "")
214+
.or(c -> c.gt(User::getId, 40), c -> c.lt(User::getId, 10))
215+
.or()
216+
.startsWith(User::getUserName, "").list();
217+
```
218+
219+
对应的 SQL 如下:
220+
221+
```sql
222+
SELECT id,name AS userName,sex FROM user
223+
WHERE
224+
( sex = ? AND ( ( id > ? ) OR ( id < ? ) ) )
225+
OR
226+
( name LIKE ? )
227+
```
228+
229+
详细的介绍可以[查看 1.2.0 更新日志](../releases/1.2.0.md)

0 commit comments

Comments
 (0)