liweifan преди 2 години
родител
ревизия
12e4539971

+ 0 - 1
cooleshow-auth/auth-server/pom.xml

@@ -17,7 +17,6 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
     <dependencies>
-
         <dependency>
             <groupId>com.yonge.toolset</groupId>
             <artifactId>toolset-mybatis</artifactId>

+ 0 - 7
cooleshow-user/user-biz/pom.xml

@@ -46,13 +46,6 @@
             <artifactId>swagger-spring-boot-starter</artifactId>
         </dependency>
 
-        <!-- mybatis-plus -->
-        <dependency>
-            <groupId>com.baomidou</groupId>
-            <artifactId>mybatis-plus-boot-starter</artifactId>
-            <version>3.0.7.1</version>
-        </dependency>
-
         <dependency>
             <groupId>commons-lang</groupId>
             <artifactId>commons-lang</artifactId>

+ 7 - 0
toolset/toolset-mybatis/pom.xml

@@ -41,6 +41,13 @@
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
+
+        <!-- mybatis-plus -->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.0.7.1</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 133 - 0
toolset/toolset-mybatis/src/main/java/com/yonge/toolset/mybatis/support/PageUtil.java

@@ -0,0 +1,133 @@
+package com.yonge.toolset.mybatis.support;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.yonge.toolset.base.page.PageInfo;
+import com.yonge.toolset.base.page.QueryInfo;
+import com.yonge.toolset.utils.string.StringUtil;
+import org.springframework.beans.BeanUtils;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * @author hgw
+ * Created by 2021-12-03
+ */
+public class PageUtil {
+
+    private static final String ASC = "ASC";
+    private static final String DESC = "DESC";
+
+    private static Integer toInt(Integer num, Integer bak) {
+        if (null == num || 0 == num) {
+            return bak;
+        }
+        return num;
+    }
+
+    /**
+     * 转化成mybatis plus中的Page
+     *
+     * @param query 查询条件
+     * @return IPage
+     */
+    public static <T> IPage<T> getPage(QueryInfo query) {
+        Page<T> page = new Page<T>(toInt(query.getPage(), 1), toInt(query.getRows(), 10));
+        if (ASC.equals(query.getOrder()) && !StringUtil.isEmpty(query.getSort())) {
+            page.setAsc(StringUtil.toStrArray(SqlKeyword.filter(query.getSort())));
+        }
+        if (DESC.equals(query.getOrder()) && !StringUtil.isEmpty(query.getSort())) {
+            page.setDesc(StringUtil.toStrArray(SqlKeyword.filter(query.getSort())));
+        }
+        return page;
+    }
+
+    /**
+     * 获取mybatis plus中的QueryWrapper
+     *
+     * @param entity 实体
+     * @param <T>    类型
+     * @return QueryWrapper
+     */
+    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
+        return new QueryWrapper<>(entity);
+    }
+
+    /**
+     * 获取mybatis plus中的QueryWrapper
+     *
+     * @param query   查询条件
+     * @param exclude 排除的查询条件
+     * @param clazz   实体类
+     * @param <T>     类型
+     * @return QueryWrapper
+     */
+    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Map<String, Object> exclude, Class<T> clazz) {
+        exclude.forEach((k, v) -> query.remove(k));
+        QueryWrapper<T> qw = new QueryWrapper<>();
+        qw.setEntity(BeanUtils.instantiateClass(clazz));
+        SqlKeyword.buildCondition(query, qw);
+        return qw;
+    }
+
+    /**
+     * 获取mybatisPlus的分页模型
+     *
+     * @param page 页数
+     * @param rows 查询数
+     * @param <T>  返回的类
+     */
+    public static <T> Page<T> getPage(Integer page, Integer rows) {
+        Integer pageIndex = Optional.ofNullable(page).orElse(1);
+        Integer pageSize = Optional.ofNullable(rows).orElse(20);
+        return new Page<T>(pageIndex, pageSize);
+    }
+
+    /**
+     * 将mybatisPlus的分页模型 转换为本项目的分页模型
+     *
+     * @param source  mybatisPlus的分页模型
+     * @param <T>返回的类
+     */
+    public static <T> PageInfo<T> pageInfo(IPage<T> source) {
+        if (Objects.isNull(source)) {
+            return new PageInfo<>(1, 20);
+        }
+        int total = Integer.parseInt(String.valueOf(source.getTotal()));
+        int limit = Integer.parseInt(String.valueOf(source.getSize()));
+
+        PageInfo<T> resultPage = new PageInfo<T>(
+                ((Long) source.getCurrent()).intValue(),
+                ((Long) source.getSize()).intValue());
+
+        resultPage.setRows(source.getRecords());
+        resultPage.setLimit(limit);
+        resultPage.setTotal(total);
+        return resultPage;
+    }
+
+
+    /**
+     * 获取Map中的关键字获取分页数据
+     *
+     * @param param Map<String, Object> param
+     * @param str   关键字
+     * @return Optional
+     */
+    public static <O> Optional<Integer> getPage(Map<String, O> param, String str) {
+        return Optional.ofNullable(param)
+                .map(p -> p.get(str))
+                .map(String::valueOf)
+                .map(Integer::valueOf);
+    }
+
+    public static <O, T> Page<T> getPageInfo(Map<String, O> param) {
+        int pageSize = getPage(param, "rows").orElse(20);
+        int pageIndex = getPage(param, "page").orElse(1);
+        return new Page<>(pageIndex, pageSize);
+    }
+
+}

+ 129 - 0
toolset/toolset-mybatis/src/main/java/com/yonge/toolset/mybatis/support/SqlKeyword.java

@@ -0,0 +1,129 @@
+package com.yonge.toolset.mybatis.support;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.yonge.toolset.utils.date.DateUtil;
+import com.yonge.toolset.utils.obj.ObjectUtil;
+import com.yonge.toolset.utils.string.StringUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import java.util.Map;
+
+/**
+ * 定义常用的 sql关键字
+ *
+ * @author Chill
+ */
+@Component
+public class SqlKeyword {
+	private final static String SQL_REGEX = "'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";
+
+	private static final String EQUAL = "_equal";
+	private static final String NOT_EQUAL = "_notequal";
+	private static final String LIKE = "_like";
+	private static final String LIKE_LEFT = "_likeleft";
+	private static final String LIKE_RIGHT = "_likeright";
+	private static final String NOT_LIKE = "_notlike";
+	private static final String GE = "_ge";
+	private static final String LE = "_le";
+	private static final String GT = "_gt";
+	private static final String LT = "_lt";
+	private static final String DATE_GE = "_datege";
+	private static final String DATE_GT = "_dategt";
+	private static final String DATE_EQUAL = "_dateequal";
+	private static final String DATE_LT = "_datelt";
+	private static final String DATE_LE = "_datele";
+	private static final String IS_NULL = "_null";
+	private static final String NOT_NULL = "_notnull";
+	private static final String IGNORE = "_ignore";
+	//是否转驼峰
+	static public Boolean underline;
+
+	/**
+	 * 条件构造器
+	 *
+	 * @param query 查询字段
+	 * @param qw    查询包装类
+	 */
+	public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) {
+		if (CollectionUtils.isEmpty(query)) {
+			return;
+		}
+		query.forEach((k, v) -> {
+			if (ObjectUtil.hasEmpty(k, v) || k.endsWith(IGNORE)) {
+				return;
+			}
+			if (k.endsWith(EQUAL)) {
+				qw.eq(getColumn(k, EQUAL), v);
+			} else if (k.endsWith(NOT_EQUAL)) {
+				qw.ne(getColumn(k, NOT_EQUAL), v);
+			} else if (k.endsWith(LIKE_LEFT)) {
+				qw.likeLeft(getColumn(k, LIKE_LEFT), v);
+			} else if (k.endsWith(LIKE_RIGHT)) {
+				qw.likeRight(getColumn(k, LIKE_RIGHT), v);
+			} else if (k.endsWith(NOT_LIKE)) {
+				qw.notLike(getColumn(k, NOT_LIKE), v);
+			} else if (k.endsWith(GE)) {
+				qw.ge(getColumn(k, GE), v);
+			} else if (k.endsWith(LE)) {
+				qw.le(getColumn(k, LE), v);
+			} else if (k.endsWith(GT)) {
+				qw.gt(getColumn(k, GT), v);
+			} else if (k.endsWith(LT)) {
+				qw.lt(getColumn(k, LT), v);
+			} else if (k.endsWith(DATE_GE)) {
+				qw.ge(getColumn(k, DATE_GE), DateUtil.parse(String.valueOf(v), DateUtil.EXPANDED_DATE_TIME_FORMAT));
+			} else if (k.endsWith(DATE_GT)) {
+				qw.gt(getColumn(k, DATE_GT), DateUtil.parse(String.valueOf(v), DateUtil.EXPANDED_DATE_TIME_FORMAT));
+			} else if (k.endsWith(DATE_EQUAL)) {
+				qw.eq(getColumn(k, DATE_EQUAL), DateUtil.parse(String.valueOf(v), DateUtil.EXPANDED_DATE_TIME_FORMAT));
+			} else if (k.endsWith(DATE_LE)) {
+				qw.le(getColumn(k, DATE_LE), DateUtil.parse(String.valueOf(v), DateUtil.EXPANDED_DATE_TIME_FORMAT));
+			} else if (k.endsWith(DATE_LT)) {
+				qw.lt(getColumn(k, DATE_LT), DateUtil.parse(String.valueOf(v), DateUtil.EXPANDED_DATE_TIME_FORMAT));
+			} else if (k.endsWith(IS_NULL)) {
+				qw.isNull(getColumn(k, IS_NULL));
+			} else if (k.endsWith(NOT_NULL)) {
+				qw.isNotNull(getColumn(k, NOT_NULL));
+			} else {
+				qw.like(getColumn(k, LIKE), v);
+			}
+		});
+	}
+
+	/**
+	 * 获取数据库字段
+	 *
+	 * @param column  字段名
+	 * @param keyword 关键字
+	 * @return
+	 */
+	private static String getColumn(String column, String keyword) {
+		if(underline){
+			return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
+		}else{
+			return StringUtil.removeSuffix(column, keyword);
+		}
+	}
+
+	/**
+	 * 把SQL关键字替换为空字符串
+	 *
+	 * @param param 关键字
+	 * @return string
+	 */
+	public static String filter(String param) {
+		if (param == null) {
+			return null;
+		}
+		return param.replaceAll("(?i)" + SQL_REGEX, StringPool.EMPTY);
+	}
+
+	@Value("${spring.datasource.table-underline}")
+	public void setUnderline(Boolean underline) {
+		SqlKeyword.underline = underline;
+	}
+
+}

+ 16 - 0
toolset/toolset-mybatis/src/main/resources/base-mybatis.yml

@@ -0,0 +1,16 @@
+mybatis:
+  mapperLocations: classpath:config/mybatis/*.xml
+#mybatis-plus配置
+mybatis-plus:
+  configuration:
+    map-underscore-to-camel-case: true
+    auto-mapping-behavior: full
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+  type-enums-package: com.yonge.**.enums
+  global-config:
+    # 逻辑删除配置
+    db-config:
+      # 删除前
+      logic-not-delete-value: 1
+      # 删除后
+      logic-delete-value: 0