Skip to content

Commit 2f58136

Browse files
committed
ApiBoot Mybatis Enhance 示例 & 文档更新
1 parent 265bf26 commit 2f58136

File tree

4 files changed

+503
-0
lines changed

4 files changed

+503
-0
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
## ApiBoot Mybatis Enhance
2+
3+
`Enhance`是对于原生的`MyBatis`的增强编写,不影响任何原生的使用,使用后完全替代`mybatis-core``mybatis-spring`以及`mybatis-spring-boot-starter`,可以使用`SpringBoot`配置文件的形式进行配置相关的内容,尽可能强大的方便快速的集成`MyBatis`
4+
5+
- 增强CRUD
6+
7+
`Mybatis Enhance`提供了`单表基础数据``CRUD`操作以及部分`批量数据`的操作,可以不再使用`MyBatis`提供的自动生成的方式对单个数据表进行数据操作,当然如果你想使用也是可以的。
8+
9+
- 动态查询、更新、删除
10+
11+
`Mybatis Enhance`还规划了多个数据表之间的动态查询方式,这种方式可以让你体验到你在使用`Java代码`编写`SQL语句`,极大方便的关联、聚合、多表查询字段等常用数据动作。
12+
13+
### 添加依赖
14+
15+
```xml
16+
<!--ApiBoot Mybatis Enhance-->
17+
<dependency>
18+
<groupId>org.minbox.framework</groupId>
19+
<artifactId>api-boot-starter-mybatis-enhance</artifactId>
20+
</dependency>
21+
<dependencyManagement>
22+
<dependencies>
23+
<!--ApiBoot Dependencies-->
24+
<dependency>
25+
<groupId>org.minbox.framework</groupId>
26+
<artifactId>api-boot-dependencies</artifactId>
27+
<version>2.0.6.RELEASE</version>
28+
<scope>import</scope>
29+
<type>pom</type>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
```
34+
35+
36+
37+
### 该怎么使用呢?
38+
39+
#### 实体的创建
40+
41+
根据对应数据库内的表来创建实体,`Enhance`采用的是`Spring Data JPA`的形式来管理实体类,并且已经预先提供的一些`Annotation``数据实体(Entity)`对应数据库内的`数据表(Table)`,下面是一个简单的实体代码:
42+
```java
43+
/**
44+
* 用户数据实体
45+
*
46+
* @author:于起宇 <br/>
47+
* ===============================
48+
* Created with IDEA.
49+
* Date:2018/5/13
50+
* Time:8:53
51+
* 简书:http://www.jianshu.com/u/092df3f77bca
52+
* ================================
53+
*/
54+
@Data
55+
@Table(name = "test_user_info")
56+
public class UserInfoEntity implements Serializable {
57+
/**
58+
* 用户编号
59+
*/
60+
@Id(generatorType = KeyGeneratorTypeEnum.AUTO)
61+
@Column(name = "TUI_ID")
62+
private Integer userId;
63+
/**
64+
* 用户名
65+
*/
66+
@Column(name = "TUI_NAME")
67+
private String userName;
68+
/**
69+
* 年龄
70+
*/
71+
@Column(name = "tui_age")
72+
private Integer age;
73+
/**
74+
* 地址
75+
*/
76+
@Column(name = "tui_address")
77+
private String address;
78+
}
79+
```
80+
我采用了跟`Spring Data JPA`相同命名方式的注解,这样也方便大家在使用`Enhance`时可以快速的转换注解的使用。
81+
#### Mapper的创建
82+
83+
创建`Mapper`跟我们使用原生`MyBatis`创建方式一样,不过使用`Enhance`后不需要添加`@Mapper`注解,你创建的`Mapper`只需要继承`EnhanceMapper<T,PK>`接口就可以被扫描到,并且同时可以获取内部提供的`CRUD`方法!!!
84+
如下所示:
85+
```java
86+
/**
87+
* 用户基本信息数据接口
88+
*
89+
* @author:于起宇 <br/>
90+
* ===============================
91+
* Created with IDEA.
92+
* Date:2018/5/13
93+
* Time:9:00
94+
* 简书:http://www.jianshu.com/u/092df3f77bca
95+
* ================================
96+
*/
97+
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {
98+
}
99+
```
100+
`EnhanceMapper`需要两个泛型,第一个是实体类的类型,第二个则是实体类主键的类型,这样方便我们在传参或者返回值时做到统一,否则还需要进行`Object`类型的转换,那样不仅麻烦还会提高运行成本。
101+
102+
#### 暂时内置的方法
103+
104+
```java
105+
// 统计数据
106+
Long countAll() throws EnhanceFrameworkException;
107+
// 清空数据
108+
void deleteAll() throws EnhanceFrameworkException;
109+
// 根据主键数组删除指定数据
110+
void deleteArray(Id... ids) throws EnhanceFrameworkException;
111+
// 根据自定义sql删除数据
112+
void deleteBySql(String sql, Map<String, Object> params) throws EnhanceFrameworkException;
113+
// 根据主键集合删除指定数据
114+
void deleteCollection(Collection<Id> collection) throws EnhanceFrameworkException;
115+
// 删除一条数据
116+
void deleteOne(Id id) throws EnhanceFrameworkException;
117+
// 数据保存
118+
void insert(T t) throws EnhanceFrameworkException;
119+
// 保存数组内的所有数据
120+
void insertArray(T... array) throws EnhanceFrameworkException;
121+
// 保存集合内的所有数据
122+
void insertCollection(Collection<T> collection) throws EnhanceFrameworkException;
123+
// 查询全部数据
124+
List<T> selectAll() throws EnhanceFrameworkException;
125+
// 根据主键数组查询指定数据
126+
List<T> selectArray(Id... ids) throws EnhanceFrameworkException;
127+
// 分页查询数据
128+
List<T> selectByPageable(Pageable pageable) throws EnhanceFrameworkException;
129+
// 自定义sql查询数据
130+
List<Map> selectBySql(String sql, Map<String, Object> params) throws EnhanceFrameworkException;
131+
// 根据主键集合查询指定数据
132+
List<T> selectCollection(Collection<Id> ids) throws EnhanceFrameworkException;
133+
// 根据主键查询单条数据
134+
T selectOne(Id id) throws EnhanceFrameworkException;
135+
// 根据主键更新数据实体
136+
void update(T t) throws EnhanceFrameworkException;
137+
// 自定义sql更新数据
138+
void updateBySql(String sql, Map<String, Object> params) throws EnhanceFrameworkException;
139+
```
140+
以上是`1.0.3.RELEASE`版本提供的内置方法列表,都是在平时开发中比较常用到对单表数据操作的方法。
141+
#### 方法命名规则的使用
142+
143+
`方法命名规则``Spring Data JPA`中的提供的一种数据操作的方式,主要适用于`查询``统计``删除`等数据操作动作,其主要原理是根据方法的名称来自动生成`SQL`,使用正则表达式来进行方法匹配。
144+
145+
#### 方法规则查询
146+
147+
方法规则查询简单示例如下所示:
148+
```java
149+
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {
150+
/**
151+
* 只根据一个字段查询
152+
* findBy userName
153+
* @param name 查询条件的值
154+
* @return
155+
*/
156+
UserInfoEntity findByUserName(@Param("userName") String name);
157+
158+
/**
159+
* 可以根据多个查询条件进行查询
160+
* 中间使用And进行连接
161+
* findBy userName and age
162+
* @param name 第一个查询条件的值
163+
* @param age 第二个查询条件的值
164+
* @return
165+
*/
166+
UserInfoEntity findByUserNameAndAge(@Param("userName") String name, @Param("age") Integer age);
167+
}
168+
```
169+
#### 方法规则统计
170+
171+
方法规则统计简单示例如下所示:
172+
```java
173+
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {
174+
/**
175+
* 只根据一个字段统计数据
176+
* 语法分析:countBy userName
177+
* @param name 统计条件的值
178+
* @return
179+
*/
180+
Long countByUserName(@Param("userName") String name);
181+
/**
182+
* 根据多个条件进行统计数据
183+
* 语法分析:countBy userName and age
184+
* @param name 第一个统计条件的值
185+
* @param age 第二个统计条件的值
186+
* @return
187+
*/
188+
Long countByUserNameAndAge(@Param("userName") String name, @Param("age") Integer age);
189+
}
190+
```
191+
#### 方法规则删除
192+
193+
方法规则删除简单示例如下所示:
194+
```java
195+
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {
196+
/**
197+
* 只根据一个字段删除
198+
* 语法分析:removeBy userName
199+
* @param name 查询条件的值
200+
*/
201+
void removeByUserName(@Param("userName") String name);
202+
203+
/**
204+
* 根据多个条件进行删除数据
205+
* 中间使用And进行连接
206+
* 语法分析:removeBy userName and userId
207+
* @param name 第一个删除条件的值
208+
* @param id 第二个删除条件的值
209+
*/
210+
void removeByUserNameAndUserId(@Param("userName") String name, @Param("userId") String id);
211+
}
212+
```
213+
214+
### 动态查询
215+
216+
`Mybatis Enhance`支持动态查询,可以将返回结果映射到任何可对应的类型内,比如:基础数据类型、集合、实体类等,编写动态查询时与`SQL`语法几乎一致。
217+
218+
如下简单示例:
219+
220+
```java
221+
/**
222+
* Mybatis Enhance Dsl Factory
223+
*/
224+
@Autowired
225+
private EnhanceDslFactory dslFactory;
226+
227+
/**
228+
* 示例:动态条件查询用户信息
229+
*
230+
* @param userId 用户编号
231+
* @return 用户基本信息
232+
*/
233+
public UserEntity dynamicSelectOne(String userId) {
234+
235+
DUserEntity dUserEntity = DUserEntity.DSL();
236+
237+
return dslFactory.createSearchable()
238+
.selectFrom(dUserEntity)
239+
.where(dUserEntity.uiId.eq(userId))
240+
.resultType(UserEntity.class)
241+
.fetchOne();
242+
}
243+
```
244+
245+
> 在上面示例中,`DUserEntity``MybatisEnhance`所需要的动态查询实体,该实体会由专门的代码生成工具生成。
246+
247+
#### 动态实体
248+
249+
`DUserEntity`实体类内容如下所示:
250+
251+
```java
252+
public class DUserEntity extends TableExpression<UserEntity> {
253+
public DUserEntity(String root) {
254+
super(root);
255+
}
256+
257+
public static final DUserEntity DSL() {
258+
return new DUserEntity("local_user_info");
259+
}
260+
261+
public ColumnExpression uiId = new ColumnExpression("ui_id", this);
262+
public ColumnExpression uiPhone = new ColumnExpression("ui_phone", this);
263+
public ColumnExpression uiPassword = new ColumnExpression("ui_password", this);
264+
public ColumnExpression uiStatus = new ColumnExpression("ui_status", this);
265+
266+
@Override
267+
public ColumnExpression[] getColumns() {
268+
return new ColumnExpression[]{
269+
uiId,
270+
uiPhone,
271+
uiPassword,
272+
uiStatus
273+
};
274+
}
275+
}
276+
```
277+
278+
279+
280+
### 动态更新
281+
282+
`Mybatis Enhance`支持动态更新,可以根据条件进行更新任意一个、多个字段,如下所示:
283+
284+
```java
285+
/**
286+
* 示例:动态更新手机号
287+
*
288+
* @param userId 用户编号
289+
* @param phone 手机号码
290+
*/
291+
public void dynamicUpdateAge(String userId, String phone) {
292+
DUserEntity dUserEntity = DUserEntity.DSL();
293+
dslFactory.createUpdateable()
294+
.update(dUserEntity)
295+
.set(SetFilter.set(dUserEntity.uiPhone, phone))
296+
.where(dUserEntity.uiId.eq(userId))
297+
.execute();
298+
}
299+
```
300+
301+
动态更新时的条件可以是一个、也可以是多个,多个可以使用`and``or`进行连接。
302+
303+
### 动态删除
304+
305+
`Mybatis Enhance`支持动态删除,可以根据单个、多个条件进行筛选删除,如下所示:
306+
307+
```java
308+
/**
309+
* 实例:动态根据手机号删除
310+
*
311+
* @param phone 手机号
312+
*/
313+
public void dynamicDeleteAge(String phone) {
314+
DUserEntity dUserEntity = DUserEntity.DSL();
315+
dslFactory.createDeleteable()
316+
.delete(dUserEntity)
317+
.where(dUserEntity.uiPhone.eq(phone))
318+
.execute();
319+
}
320+
```
321+
322+
323+
324+
### 多条件 And
325+
326+
`Mybatis Enhance`支持动态组装查询条件,比如我现在根据用户名、手机号进行定位删除用户,如下所示:
327+
328+
```java
329+
/**
330+
* 实例:动态根据手机号、用户编号删除
331+
*
332+
* @param userId 用户编号
333+
* @param phone 手机号
334+
*/
335+
public void dynamicDeleteUser(String userId, String phone) {
336+
DUserEntity dUserEntity = DUserEntity.DSL();
337+
dslFactory.createDeleteable()
338+
.delete(dUserEntity)
339+
// 手机号条件
340+
.where(dUserEntity.uiPhone.eq(phone))
341+
// and 用户编号
342+
.and(dUserEntity.uiId.eq(userId))
343+
.execute();
344+
}
345+
```
346+

api-boot-samples/api-boot-sample-mybatis-enhance/src/main/java/org/minbox/framework/api/boot/sample/mybatis/enhance/ApiBootMybatisEnhanceApplication.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public static void main(String[] args) {
4949
@Autowired
5050
private UserMapper userMapper;
5151

52+
@Autowired
53+
private UserService userService;
54+
5255
/**
5356
* 查询全部用户
5457
*
@@ -69,4 +72,15 @@ public List<UserEntity> list() {
6972
public UserEntity one(@PathVariable String userId) {
7073
return userMapper.selectOne(userId);
7174
}
75+
76+
/**
77+
* 示例:动态查询用户详情
78+
*
79+
* @param userId 用户编号
80+
* @return
81+
*/
82+
@GetMapping(value = "/dynamic/{userId}")
83+
public UserEntity dynamic(@PathVariable String userId) {
84+
return userService.dynamicSelectOne(userId);
85+
}
7286
}

0 commit comments

Comments
 (0)