-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(基础框架): 封装 MyBatis Plus;添加 PageHelper 分页插件;扩展 SuperService、SuperM…
…apper
- Loading branch information
Showing
18 changed files
with
970 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 82 additions & 2 deletions
84
mogu_base/src/main/java/com/moxi/mougblog/base/mapper/SuperMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,94 @@ | ||
package com.moxi.mougblog.base.mapper; | ||
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.Wrapper; | ||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.baomidou.mybatisplus.core.metadata.IPage; | ||
import com.baomidou.mybatisplus.core.toolkit.ClassUtils; | ||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; | ||
import com.moxi.mougblog.base.mybatis.page.dto.PageDTO; | ||
import com.moxi.mougblog.base.mybatis.page.vo.PageVO; | ||
import com.moxi.mougblog.base.mybatis.plugin.query.QueryWrapperPlus; | ||
import com.moxi.mougblog.base.mybatis.reflect.GenericTypeUtils; | ||
import com.moxi.mougblog.base.mybatis.util.MybatisUtils; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* mapper 父类,注意这个类不要让 mybatis-plus 扫描到!! | ||
* mapper 父类,在 MyBatis Plus 的 BaseMapper 的基础上拓展,提供更多功能。注意这个类不要让 mybatis-plus 扫描到!! | ||
* @author 陌溪 | ||
* @date 2020年12月31日21:32:33 | ||
*/ | ||
public interface SuperMapper<T> extends BaseMapper<T> { | ||
|
||
// 这里可以放一些公共的方法 | ||
/** | ||
* 查询分页 | ||
*/ | ||
default PageVO<T> selectPage(PageDTO pageDTO, @Param("ew") Wrapper<T> queryWrapper) { | ||
// MyBatis Plus 分页查询 | ||
IPage<T> myBatisPage = MybatisUtils.buildPage(pageDTO); | ||
selectPage(myBatisPage, queryWrapper); | ||
// 转换返回值 | ||
return new PageVO<>(myBatisPage.getRecords(), myBatisPage.getTotal(), myBatisPage.getSize()); | ||
} | ||
|
||
/** | ||
* 查询分页 | ||
* | ||
* @param d 实体类参数对接 | ||
* @param selectColumns 查询返回的列 | ||
*/ | ||
default <D> PageVO<T> selectPage(D d, SFunction<T, ?>... selectColumns) { | ||
// 构造分页,不能强制转 pageDTO,否则如果没有继承的话,会报错 | ||
PageDTO pageDTO = d instanceof PageDTO ? (PageDTO) d : null; | ||
IPage<T> myBatisPage = MybatisUtils.buildPage(pageDTO); | ||
// 查询数据 | ||
selectPage(myBatisPage, buildQueryWrapper(d, selectColumns)); | ||
// 转换返回值 | ||
return new PageVO<>(myBatisPage.getRecords(), myBatisPage.getTotal(), myBatisPage.getSize()); | ||
} | ||
|
||
/** | ||
* 查询列表 | ||
* | ||
* @param d 实体类参数对接 | ||
* @param selectColumns 查询返回的列 | ||
*/ | ||
default <D> List<T> selectList(D d, SFunction<T, ?>... selectColumns) { | ||
return selectList(buildQueryWrapper(d, selectColumns)); | ||
} | ||
|
||
/** | ||
* 查询单条 | ||
* | ||
* @param d 实体类参数对接 | ||
*/ | ||
default <D> T selectOne(D d) { | ||
return selectOne(buildQueryWrapper(d)); | ||
} | ||
|
||
/** | ||
* 查询总记录数 | ||
* | ||
* @param d 实体类参数对接 | ||
*/ | ||
default <D> Long selectCount(D d) { | ||
return selectCount(buildQueryWrapper(d)).longValue(); | ||
} | ||
|
||
/** | ||
* 构建 Wrapper 查询条件 | ||
* | ||
* @param d DTO 实体参数对象 | ||
* @param selectColumns 查询返回的列 | ||
* @return 查询构造器 | ||
*/ | ||
default <D> QueryWrapperPlus<T> buildQueryWrapper(D d, SFunction<T, ?>... selectColumns) { | ||
Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(this.getClass()), SuperMapper.class); | ||
Class<T> entityClass = null == typeArguments ? null : (Class<T>) typeArguments[0]; | ||
return new QueryWrapperPlus<T>().buildQueryWrapper(entityClass, d, selectColumns); | ||
} | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/page/dto/PageDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.moxi.mougblog.base.mybatis.page.dto; | ||
import com.moxi.mougblog.base.mybatis.plugin.annotation.Query; | ||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* 分页对象 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/3/27 | ||
*/ | ||
@Data | ||
public class PageDTO implements Serializable { | ||
|
||
private static final Long serialVersionUID = 1L; | ||
|
||
/** | ||
* 每页显示记录数 | ||
*/ | ||
@Query(ignore = true) | ||
private Long pageSize; | ||
|
||
/** | ||
* 起始页 | ||
*/ | ||
@Query(ignore = true) | ||
private Long currentPage; | ||
|
||
/** | ||
* 排序列,多个用逗号分开 | ||
*/ | ||
@Query(ignore = true) | ||
private String orderColumn; | ||
|
||
/** | ||
* 排序类型(asc 或 desc),多个用逗号分开 | ||
*/ | ||
@Query(ignore = true) | ||
private String orderType; | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/page/util/PageUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.moxi.mougblog.base.mybatis.page.util; | ||
|
||
import com.github.pagehelper.PageHelper; | ||
import com.github.pagehelper.PageInfo; | ||
import com.moxi.mougblog.base.mybatis.page.dto.PageDTO; | ||
import com.moxi.mougblog.base.mybatis.page.vo.PageVO; | ||
import com.moxi.mougblog.base.mybatis.property.PageProperty; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 分页工具类 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/3/27 | ||
*/ | ||
public class PageUtils { | ||
|
||
/** | ||
* 开始分页 | ||
*/ | ||
public static void startPage(PageDTO pageDTO) { | ||
startPage(pageDTO.getCurrentPage(), pageDTO.getPageSize()); | ||
} | ||
|
||
/** | ||
* 开始分页 | ||
* @param pageNum 起始页 | ||
* @param pageSize 分页记录数 | ||
*/ | ||
public static void startPage(Long pageNum, Long pageSize) { | ||
if (pageNum == null) { | ||
pageNum = 1L; | ||
} | ||
if (pageSize == null) { | ||
pageSize = PageProperty.defaultPageSize; | ||
} | ||
// 如果超过最大分页数,则设置为最大分页数 | ||
if (pageSize > PageProperty.maxPageSize) { | ||
pageSize = PageProperty.maxPageSize; | ||
} | ||
PageHelper.startPage(pageNum.intValue(), pageSize.intValue()); | ||
} | ||
|
||
|
||
/** | ||
* 获取分页信息 | ||
*/ | ||
public static <T> PageVO<T> getPage(List<T> list) { | ||
PageInfo pageInfo = new PageInfo(list); | ||
return new PageVO(list, pageInfo.getTotal(), pageInfo.getSize()); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/page/vo/PageVO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.moxi.mougblog.base.mybatis.page.vo; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
/** | ||
* 分页对象 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/3/27 | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class PageVO<T> implements Serializable { | ||
|
||
private static final Long serialVersionUID = 1L; | ||
|
||
/** | ||
* 数据列表 | ||
*/ | ||
private List<T> records; | ||
|
||
/** | ||
* 总记录数 | ||
*/ | ||
private long total; | ||
|
||
/** | ||
* 每页显示记录数 | ||
*/ | ||
private long size; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/plugin/annotation/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.moxi.mougblog.base.mybatis.plugin.annotation; | ||
|
||
import com.moxi.mougblog.base.mybatis.plugin.enums.QueryWay; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 查询注解 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/11/4 | ||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface Query { | ||
|
||
/** | ||
* 查询方式,默认为相等 | ||
*/ | ||
QueryWay value() default QueryWay.EQ; | ||
|
||
/** | ||
* 属性名,如果当前属性名与 PO 不一致,可指定 PO 的属性名 | ||
*/ | ||
String fieldName() default ""; | ||
|
||
/** | ||
* 是否忽略查询,如果当前属性不需要查询,则可设置为 true | ||
*/ | ||
boolean ignore() default false; | ||
|
||
/** | ||
* 是否空查询,当为空时,是否依旧查询,默认不需要查询 | ||
*/ | ||
boolean empty() default false; | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/plugin/constant/FieldConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.moxi.mougblog.base.mybatis.plugin.constant; | ||
|
||
/** | ||
* 字段常量 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/8/7 | ||
*/ | ||
public class FieldConstant { | ||
|
||
/** | ||
* 排序列,多个用逗号分开 | ||
*/ | ||
public static final String ORDER_COLUMN = "orderColumn"; | ||
|
||
/** | ||
* 排序类型(asc 或 desc),多个用逗号分开 | ||
*/ | ||
public static final String ORDER_TYPE = "orderType"; | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
mogu_base/src/main/java/com/moxi/mougblog/base/mybatis/plugin/enums/QueryWay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.moxi.mougblog.base.mybatis.plugin.enums; | ||
|
||
/** | ||
* 查询方式枚举 | ||
* | ||
* @author geshanzsq | ||
* @date 2022/11/4 | ||
*/ | ||
public enum QueryWay { | ||
|
||
/** | ||
* 等于 | ||
*/ | ||
EQ, | ||
|
||
/** | ||
* 不等于 | ||
*/ | ||
NE, | ||
|
||
/** | ||
* 大于 | ||
*/ | ||
GT, | ||
|
||
/** | ||
* 大于等于 | ||
*/ | ||
GE, | ||
|
||
/** | ||
* 小于 | ||
*/ | ||
LT, | ||
|
||
/** | ||
* 小于等于 | ||
*/ | ||
LE, | ||
|
||
/** | ||
* 模糊 | ||
*/ | ||
LIKE, | ||
|
||
/** | ||
* 不模糊 | ||
*/ | ||
NOT_LIKE, | ||
|
||
/** | ||
* 左模糊 | ||
*/ | ||
LIKE_LEFT, | ||
|
||
/** | ||
* 右模糊 | ||
*/ | ||
LIKE_RIGHT, | ||
|
||
/** | ||
* 包含 | ||
*/ | ||
IN, | ||
|
||
/** | ||
* 不包含 | ||
*/ | ||
NOT_IN | ||
|
||
} |
Oops, something went wrong.