提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.core.mapper; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.iailab.framework.common.pojo.PageParam; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.common.pojo.SortablePageParam; |
|
7 |
import com.iailab.framework.common.pojo.SortingField; |
|
8 |
import com.iailab.framework.mybatis.core.enums.SqlConstants; |
|
9 |
import com.iailab.framework.mybatis.core.util.MyBatisUtils; |
|
10 |
import com.baomidou.mybatisplus.annotation.DbType; |
|
11 |
import com.baomidou.mybatisplus.core.conditions.Wrapper; |
|
12 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
13 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
14 |
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
15 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
16 |
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; |
|
17 |
import com.baomidou.mybatisplus.extension.toolkit.Db; |
|
18 |
import com.github.yulichang.base.MPJBaseMapper; |
|
19 |
import com.github.yulichang.interfaces.MPJBaseJoin; |
|
20 |
import com.github.yulichang.wrapper.MPJLambdaWrapper; |
|
21 |
import org.apache.ibatis.annotations.Param; |
|
22 |
|
|
23 |
import java.util.Collection; |
|
24 |
import java.util.List; |
|
25 |
import java.util.Objects; |
|
26 |
|
|
27 |
/** |
|
28 |
* 在 MyBatis Plus 的 BaseMapper 的基础上拓展,提供更多的能力 |
|
29 |
* |
|
30 |
* 1. {@link BaseMapper} 为 MyBatis Plus 的基础接口,提供基础的 CRUD 能力 |
|
31 |
* 2. {@link MPJBaseMapper} 为 MyBatis Plus Join 的基础接口,提供连表 Join 能力 |
|
32 |
*/ |
|
33 |
public interface BaseMapperX<T> extends MPJBaseMapper<T> { |
|
34 |
|
|
35 |
default PageResult<T> selectPage(SortablePageParam pageParam, @Param("ew") Wrapper<T> queryWrapper) { |
|
36 |
return selectPage(pageParam, pageParam.getSortingFields(), queryWrapper); |
|
37 |
} |
|
38 |
|
|
39 |
default PageResult<T> selectPage(PageParam pageParam, @Param("ew") Wrapper<T> queryWrapper) { |
|
40 |
return selectPage(pageParam, null, queryWrapper); |
|
41 |
} |
|
42 |
|
|
43 |
default PageResult<T> selectPage(PageParam pageParam, Collection<SortingField> sortingFields, @Param("ew") Wrapper<T> queryWrapper) { |
|
44 |
// 特殊:不分页,直接查询全部 |
|
45 |
if (PageParam.PAGE_SIZE_NONE.equals(pageParam.getPageSize())) { |
|
46 |
List<T> list = selectList(queryWrapper); |
|
47 |
return new PageResult<>(list, (long) list.size()); |
|
48 |
} |
|
49 |
|
|
50 |
// MyBatis Plus 查询 |
|
51 |
IPage<T> mpPage = MyBatisUtils.buildPage(pageParam, sortingFields); |
|
52 |
selectPage(mpPage, queryWrapper); |
|
53 |
// 转换返回 |
|
54 |
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); |
|
55 |
} |
|
56 |
|
|
57 |
default <D> PageResult<D> selectJoinPage(PageParam pageParam, Class<D> clazz, MPJLambdaWrapper<T> lambdaWrapper) { |
|
58 |
// 特殊:不分页,直接查询全部 |
|
59 |
if (PageParam.PAGE_SIZE_NONE.equals(pageParam.getPageNo())) { |
|
60 |
List<D> list = selectJoinList(clazz, lambdaWrapper); |
|
61 |
return new PageResult<>(list, (long) list.size()); |
|
62 |
} |
|
63 |
|
|
64 |
// MyBatis Plus Join 查询 |
|
65 |
IPage<D> mpPage = MyBatisUtils.buildPage(pageParam); |
|
66 |
mpPage = selectJoinPage(mpPage, clazz, lambdaWrapper); |
|
67 |
// 转换返回 |
|
68 |
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); |
|
69 |
} |
|
70 |
|
|
71 |
default <DTO> PageResult<DTO> selectJoinPage(PageParam pageParam, Class<DTO> resultTypeClass, MPJBaseJoin<T> joinQueryWrapper) { |
|
72 |
IPage<DTO> mpPage = MyBatisUtils.buildPage(pageParam); |
|
73 |
selectJoinPage(mpPage, resultTypeClass, joinQueryWrapper); |
|
74 |
// 转换返回 |
|
75 |
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); |
|
76 |
} |
|
77 |
|
|
78 |
default T selectOne(String field, Object value) { |
|
79 |
return selectOne(new QueryWrapper<T>().eq(field, value)); |
|
80 |
} |
|
81 |
|
|
82 |
default T selectOne(SFunction<T, ?> field, Object value) { |
|
83 |
return selectOne(new LambdaQueryWrapper<T>().eq(field, value)); |
|
84 |
} |
|
85 |
|
|
86 |
default T selectOne(String field1, Object value1, String field2, Object value2) { |
|
87 |
return selectOne(new QueryWrapper<T>().eq(field1, value1).eq(field2, value2)); |
|
88 |
} |
|
89 |
|
|
90 |
default T selectOne(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) { |
|
91 |
return selectOne(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2)); |
|
92 |
} |
|
93 |
|
|
94 |
default T selectOne(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2, |
|
95 |
SFunction<T, ?> field3, Object value3) { |
|
96 |
return selectOne(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2) |
|
97 |
.eq(field3, value3)); |
|
98 |
} |
|
99 |
|
|
100 |
default Long selectCount() { |
|
101 |
return selectCount(new QueryWrapper<>()); |
|
102 |
} |
|
103 |
|
|
104 |
default Long selectCount(String field, Object value) { |
|
105 |
return selectCount(new QueryWrapper<T>().eq(field, value)); |
|
106 |
} |
|
107 |
|
|
108 |
default Long selectCount(SFunction<T, ?> field, Object value) { |
|
109 |
return selectCount(new LambdaQueryWrapper<T>().eq(field, value)); |
|
110 |
} |
|
111 |
|
|
112 |
default List<T> selectList() { |
|
113 |
return selectList(new QueryWrapper<>()); |
|
114 |
} |
|
115 |
|
|
116 |
default List<T> selectList(String field, Object value) { |
|
117 |
return selectList(new QueryWrapper<T>().eq(field, value)); |
|
118 |
} |
|
119 |
|
|
120 |
default List<T> selectList(SFunction<T, ?> field, Object value) { |
|
121 |
return selectList(new LambdaQueryWrapper<T>().eq(field, value)); |
|
122 |
} |
|
123 |
|
|
124 |
default List<T> selectList(String field, Collection<?> values) { |
|
125 |
if (CollUtil.isEmpty(values)) { |
|
126 |
return CollUtil.newArrayList(); |
|
127 |
} |
|
128 |
return selectList(new QueryWrapper<T>().in(field, values)); |
|
129 |
} |
|
130 |
|
|
131 |
default List<T> selectList(SFunction<T, ?> field, Collection<?> values) { |
|
132 |
if (CollUtil.isEmpty(values)) { |
|
133 |
return CollUtil.newArrayList(); |
|
134 |
} |
|
135 |
return selectList(new LambdaQueryWrapper<T>().in(field, values)); |
|
136 |
} |
|
137 |
|
|
138 |
@Deprecated |
|
139 |
default List<T> selectList(SFunction<T, ?> leField, SFunction<T, ?> geField, Object value) { |
|
140 |
return selectList(new LambdaQueryWrapper<T>().le(leField, value).ge(geField, value)); |
|
141 |
} |
|
142 |
|
|
143 |
default List<T> selectList(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) { |
|
144 |
return selectList(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2)); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* 批量插入,适合大量数据插入 |
|
149 |
* |
|
150 |
* @param entities 实体们 |
|
151 |
*/ |
|
152 |
default Boolean insertBatch(Collection<T> entities) { |
|
153 |
// 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 |
|
154 |
if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { |
|
155 |
entities.forEach(this::insert); |
|
156 |
return CollUtil.isNotEmpty(entities); |
|
157 |
} |
|
158 |
return Db.saveBatch(entities); |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* 批量插入,适合大量数据插入 |
|
163 |
* |
|
164 |
* @param entities 实体们 |
|
165 |
* @param size 插入数量 Db.saveBatch 默认为 1000 |
|
166 |
*/ |
|
167 |
default Boolean insertBatch(Collection<T> entities, int size) { |
|
168 |
// 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 |
|
169 |
if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { |
|
170 |
entities.forEach(this::insert); |
|
171 |
return CollUtil.isNotEmpty(entities); |
|
172 |
} |
|
173 |
return Db.saveBatch(entities, size); |
|
174 |
} |
|
175 |
|
|
176 |
default int updateBatch(T update) { |
|
177 |
return update(update, new QueryWrapper<>()); |
|
178 |
} |
|
179 |
|
|
180 |
default Boolean updateBatch(Collection<T> entities) { |
|
181 |
return Db.updateBatchById(entities); |
|
182 |
} |
|
183 |
|
|
184 |
default Boolean updateBatch(Collection<T> entities, int size) { |
|
185 |
return Db.updateBatchById(entities, size); |
|
186 |
} |
|
187 |
|
|
188 |
default Boolean insertOrUpdate(T entity) { |
|
189 |
return Db.saveOrUpdate(entity); |
|
190 |
} |
|
191 |
|
|
192 |
default Boolean insertOrUpdateBatch(Collection<T> collection) { |
|
193 |
return Db.saveOrUpdateBatch(collection); |
|
194 |
} |
|
195 |
|
|
196 |
default int delete(String field, String value) { |
|
197 |
return delete(new QueryWrapper<T>().eq(field, value)); |
|
198 |
} |
|
199 |
|
|
200 |
default int delete(SFunction<T, ?> field, Object value) { |
|
201 |
return delete(new LambdaQueryWrapper<T>().eq(field, value)); |
|
202 |
} |
|
203 |
|
|
204 |
} |