提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.core.util; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollectionUtil; |
d9f9ba
|
4 |
import cn.hutool.core.util.StrUtil; |
H |
5 |
import com.baomidou.mybatisplus.annotation.DbType; |
e7c126
|
6 |
import com.baomidou.mybatisplus.core.metadata.OrderItem; |
H |
7 |
import com.baomidou.mybatisplus.core.toolkit.StringPool; |
|
8 |
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
|
9 |
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
|
10 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
d9f9ba
|
11 |
import com.iailab.framework.common.pojo.PageParam; |
H |
12 |
import com.iailab.framework.common.pojo.SortingField; |
|
13 |
import com.iailab.framework.mybatis.core.enums.DbTypeEnum; |
e7c126
|
14 |
import net.sf.jsqlparser.expression.Alias; |
H |
15 |
import net.sf.jsqlparser.schema.Column; |
|
16 |
import net.sf.jsqlparser.schema.Table; |
|
17 |
|
|
18 |
import java.util.ArrayList; |
|
19 |
import java.util.Collection; |
|
20 |
import java.util.List; |
|
21 |
import java.util.stream.Collectors; |
|
22 |
|
|
23 |
/** |
|
24 |
* MyBatis 工具类 |
|
25 |
*/ |
|
26 |
public class MyBatisUtils { |
|
27 |
|
|
28 |
private static final String MYSQL_ESCAPE_CHARACTER = "`"; |
|
29 |
|
|
30 |
public static <T> Page<T> buildPage(PageParam pageParam) { |
|
31 |
return buildPage(pageParam, null); |
|
32 |
} |
|
33 |
|
|
34 |
public static <T> Page<T> buildPage(PageParam pageParam, Collection<SortingField> sortingFields) { |
|
35 |
// 页码 + 数量 |
|
36 |
Page<T> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize()); |
|
37 |
// 排序字段 |
|
38 |
if (!CollectionUtil.isEmpty(sortingFields)) { |
|
39 |
page.addOrder(sortingFields.stream().map(sortingField -> SortingField.ORDER_ASC.equals(sortingField.getOrder()) ? |
|
40 |
OrderItem.asc(sortingField.getField()) : OrderItem.desc(sortingField.getField())) |
|
41 |
.collect(Collectors.toList())); |
|
42 |
} |
|
43 |
return page; |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* 将拦截器添加到链中 |
|
48 |
* 由于 MybatisPlusInterceptor 不支持添加拦截器,所以只能全量设置 |
|
49 |
* |
|
50 |
* @param interceptor 链 |
|
51 |
* @param inner 拦截器 |
|
52 |
* @param index 位置 |
|
53 |
*/ |
|
54 |
public static void addInterceptor(MybatisPlusInterceptor interceptor, InnerInterceptor inner, int index) { |
|
55 |
List<InnerInterceptor> inners = new ArrayList<>(interceptor.getInterceptors()); |
|
56 |
inners.add(index, inner); |
|
57 |
interceptor.setInterceptors(inners); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* 获得 Table 对应的表名 |
d9f9ba
|
62 |
* <p> |
e7c126
|
63 |
* 兼容 MySQL 转义表名 `t_xxx` |
H |
64 |
* |
|
65 |
* @param table 表 |
|
66 |
* @return 去除转移字符后的表名 |
|
67 |
*/ |
|
68 |
public static String getTableName(Table table) { |
|
69 |
String tableName = table.getName(); |
|
70 |
if (tableName.startsWith(MYSQL_ESCAPE_CHARACTER) && tableName.endsWith(MYSQL_ESCAPE_CHARACTER)) { |
|
71 |
tableName = tableName.substring(1, tableName.length() - 1); |
|
72 |
} |
|
73 |
return tableName; |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* 构建 Column 对象 |
|
78 |
* |
|
79 |
* @param tableName 表名 |
|
80 |
* @param tableAlias 别名 |
|
81 |
* @param column 字段名 |
|
82 |
* @return Column 对象 |
|
83 |
*/ |
|
84 |
public static Column buildColumn(String tableName, Alias tableAlias, String column) { |
|
85 |
if (tableAlias != null) { |
|
86 |
tableName = tableAlias.getName(); |
|
87 |
} |
|
88 |
return new Column(tableName + StringPool.DOT + column); |
|
89 |
} |
|
90 |
|
d9f9ba
|
91 |
/** |
H |
92 |
* 跨数据库的 find_in_set 实现 |
|
93 |
* |
|
94 |
* @param column 字段名称 |
|
95 |
* @param value 查询值(不带单引号) |
|
96 |
* @return sql |
|
97 |
*/ |
|
98 |
public static String findInSet(String column, Object value) { |
|
99 |
// 这里不用SqlConstants.DB_TYPE,因为它是使用 primary 数据源的 url 推断出来的类型 |
|
100 |
DbType dbType = JdbcUtils.getDbType(); |
|
101 |
return DbTypeEnum.getFindInSetTemplate(dbType) |
|
102 |
.replace("#{column}", column) |
|
103 |
.replace("#{value}", StrUtil.toString(value)); |
|
104 |
} |
|
105 |
|
e7c126
|
106 |
} |