From a90c851caf05ead3b31142416da263d7c7fdcc4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E9=87=91=E9=BE=99?= Date: Mon, 6 Jul 2020 21:44:46 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E4=BF=AE=E6=94=B9=20ID=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/zhengjie/base/BaseRepository.java | 107 +++++++++++++++++- .../java/me/zhengjie/utils/QueryHelp.java | 29 ++++- .../repository/ColumnInfoRepository.java | 3 +- .../repository/GenConfigRepository.java | 15 +++ .../core/metadata/TableInfoHelper.java | 10 +- .../system/repository/JobRepository.java | 8 +- 6 files changed, 158 insertions(+), 14 deletions(-) diff --git a/eladmin-common/src/main/java/me/zhengjie/base/BaseRepository.java b/eladmin-common/src/main/java/me/zhengjie/base/BaseRepository.java index b5b6f8ab..4278e656 100644 --- a/eladmin-common/src/main/java/me/zhengjie/base/BaseRepository.java +++ b/eladmin-common/src/main/java/me/zhengjie/base/BaseRepository.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019-2020 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.base; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -39,7 +54,13 @@ public class BaseRepository, J extends JpaRepository, J extends JpaRepository saveAll(List entities) { List result; switch (dbType) { @@ -72,6 +121,11 @@ public class BaseRepository, J extends JpaRepository, J extends JpaRepository, J extends JpaRepository entities) { switch (dbType) { case JPA: @@ -124,6 +193,12 @@ public class BaseRepository, J extends JpaRepository, J extends JpaRepository findById(ID id) { return Optional.of(selectById(id)); } + /** + * 依据ID 批量查询 + * + * @param ids + * @return / + */ public List selectAllById(Iterable ids) { List t; switch (dbType) { @@ -158,6 +245,12 @@ public class BaseRepository, J extends JpaRepository findAll(@Nullable Specification spec) { List t = null; switch (dbType) { @@ -226,6 +319,12 @@ public class BaseRepository, J extends JpaRepository mpFindAll(Specification spec) { ElSpecification specifications = (ElSpecification) spec; final QueryWrapper queryWrapper = specifications.getQueryWrapper(); diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java b/eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java index f1c79328..1a8b6602 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java @@ -198,6 +198,14 @@ public class QueryHelp { return cb.and(list.toArray(new Predicate[size])); } + /** + * Mybatis Plus 查询构建 + * + * @param criteria + * @param clazz + * @param + * @return / + */ public static QueryWrapper getQueryWrapper(Object criteria, Class clazz) { QueryWrapper queryWrapper = Wrappers.query(); final List allFields = getAllFields(criteria.getClass()); @@ -229,9 +237,15 @@ public class QueryHelp { } if (Objects.nonNull(query)) { String propName = query.propName(); - String joinName = query.joinName(); String blurry = query.blurry(); attributeName = isBlank(propName) ? attributeName : propName; + if (StringUtils.isNotBlank(blurry)) { + String[] blurrys = blurry.split(","); + for (String item : blurrys) { + queryWrapper.like(item, value); + } + } + List between = null; switch (query.type()) { case EQUAL: @@ -302,7 +316,14 @@ public class QueryHelp { return true; } - private static String getTableColumnFromField(TableInfo tableInfo, Field field) { + /** + * 依据Mybatis Plus 获取 Database 真实Column 字段 + * + * @param tableInfo + * @param field + * @return / + */ + public static String getTableColumnFromField(TableInfo tableInfo, Field field) { String columnName = null; if (FIELD_CACHE.containsKey(field)) { final ElField elField = COLUMN_CACHE.get(field); @@ -348,6 +369,10 @@ public class QueryHelp { } } +/** + * @author liaojinlong + * @since 2020/7/6 21:04 + */ class ElField { private String column; private boolean status; diff --git a/eladmin-generator/src/main/java/me/zhengjie/repository/ColumnInfoRepository.java b/eladmin-generator/src/main/java/me/zhengjie/repository/ColumnInfoRepository.java index dd0bb03f..97251a3d 100644 --- a/eladmin-generator/src/main/java/me/zhengjie/repository/ColumnInfoRepository.java +++ b/eladmin-generator/src/main/java/me/zhengjie/repository/ColumnInfoRepository.java @@ -20,7 +20,6 @@ import me.zhengjie.base.BaseRepository; import me.zhengjie.domain.ColumnInfo; import me.zhengjie.repository.jpa.ColumnInfoJpaRepository; import me.zhengjie.repository.mp.ColumnInfoMpService; -import me.zhengjie.utils.enums.DbType; import org.springframework.stereotype.Repository; import java.util.List; @@ -36,7 +35,7 @@ public class ColumnInfoRepository extends BaseRepository findByTableNameOrderByIdAsc(String tableName) { diff --git a/eladmin-generator/src/main/java/me/zhengjie/repository/GenConfigRepository.java b/eladmin-generator/src/main/java/me/zhengjie/repository/GenConfigRepository.java index 54dce666..f83ec87b 100644 --- a/eladmin-generator/src/main/java/me/zhengjie/repository/GenConfigRepository.java +++ b/eladmin-generator/src/main/java/me/zhengjie/repository/GenConfigRepository.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019-2020 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.repository; import com.baomidou.mybatisplus.core.toolkit.Wrappers; diff --git a/eladmin-system/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java b/eladmin-system/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java index 47669295..d02fb465 100644 --- a/eladmin-system/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java +++ b/eladmin-system/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java @@ -52,6 +52,9 @@ import static java.util.stream.Collectors.toList; * @since 2020/6/29 18:07 */ public class TableInfoHelper { + static { + System.out.println("MyBatis Plus (Jpa Patch)"); + } private static final Log logger = LogFactory.getLog(TableInfoHelper.class); @@ -136,9 +139,6 @@ public class TableInfoHelper { // 兼容测试场景 globalConfig = GlobalConfigUtils.defaults(); } - if (globalConfig.isBanner()) { - System.out.println("MyBatis Plus (Jpa Patch)"); - } /* 初始化表名相关 */ final String[] excludeProperty = initTableName(clazz, globalConfig, tableInfo); @@ -320,8 +320,8 @@ public class TableInfoHelper { } GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class); if (Objects.nonNull(generatedValue)) { - tableIdImp.setType(IdType.ASSIGN_ID); - logger.warn("JPA compatible mode, []com.baomidou.mybatisplus.annotation.IdType.ASSIGN_ID] is the only way to generate primary key"); + tableIdImp.setType(IdType.AUTO); + logger.warn("JPA compatible mode, []com.baomidou.mybatisplus.annotation.IdType.AUTO] is the only way to generate primary key"); } tableId = tableIdImp; jpaReadPK = true; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/JobRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/JobRepository.java index dcee6a8a..c72cd96b 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/JobRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/JobRepository.java @@ -17,7 +17,6 @@ package me.zhengjie.modules.system.repository; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import me.zhengjie.base.BaseRepository; -import me.zhengjie.domain.ColumnInfo; import me.zhengjie.modules.system.domain.Job; import me.zhengjie.modules.system.repository.jpa.JobJpaRepository; import me.zhengjie.modules.system.repository.mp.JobService; @@ -39,6 +38,9 @@ public class JobRepository extends BaseRepository ids) { switch (dbType) { case JPA: @@ -52,6 +54,10 @@ public class JobRepository extends BaseRepository