diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/impl/resolver/AdapterResolver.java b/dorive-core/src/main/java/com/gitee/dorive/core/impl/resolver/AdapterResolver.java index 88238368308c98ec37266c2538f25b852b2d9e82..2bb96fa5d1314d026637fdcfde0af4ff0d011ab1 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/impl/resolver/AdapterResolver.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/impl/resolver/AdapterResolver.java @@ -43,9 +43,7 @@ public class AdapterResolver { AdapterDef adapterDef = entityType.getAdapterDef(); if (adapterDef != null) { Class adapterClass = adapterDef.getAdapter(); - if (adapterClass == Object.class) { - adapter = new DefaultAdapter(); - } else { + if (adapterClass != Object.class) { ApplicationContext applicationContext = repository.getApplicationContext(); adapter = (Adapter) applicationContext.getBean(adapterClass); } diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractContextRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractContextRepository.java index e83726ec0f059fd43668dc1b546a7a58c422798c..0262bfe105be306720c35002c29f4a75b34cf182 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractContextRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractContextRepository.java @@ -31,10 +31,9 @@ import com.gitee.dorive.core.api.executor.EntityHandler; import com.gitee.dorive.core.api.executor.Executor; import com.gitee.dorive.core.config.RepositoryDefinition; import com.gitee.dorive.core.entity.executor.OrderBy; -import com.gitee.dorive.core.impl.adapter.AliasConverter; -import com.gitee.dorive.core.impl.factory.OperationFactory; import com.gitee.dorive.core.impl.executor.AdaptiveExecutor; import com.gitee.dorive.core.impl.executor.ChainExecutor; +import com.gitee.dorive.core.impl.factory.OperationFactory; import com.gitee.dorive.core.impl.handler.AdaptiveEntityHandler; import com.gitee.dorive.core.impl.handler.BatchEntityHandler; import com.gitee.dorive.core.impl.resolver.AdapterResolver; @@ -49,10 +48,12 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; @Data @EqualsAndHashCode(callSuper = false) @@ -102,29 +103,16 @@ public abstract class AbstractContextRepository extends AbstractRepositor setEntityDef(rootRepository.getEntityDef()); setEntityEle(rootRepository.getEntityEle()); setOperationFactory(rootRepository.getOperationFactory()); - - delegateResolver = new DelegateResolver(this); - adapterResolver = new AdapterResolver(this); - - EntityHandler batchEntityHandler = new BatchEntityHandler(this, rootRepository.getOperationFactory()); - EntityHandler entityHandler = batchEntityHandler; - if (delegateResolver.isDelegated()) { - entityHandler = new AdaptiveEntityHandler(this, entityHandler); - } - Executor executor = new ChainExecutor(this, entityHandler); - if (adapterResolver.isAdaptive()) { - executor = new AdaptiveExecutor(this, executor); - } - setExecutor(executor); - - processEntityClass(batchEntityHandler); + setExecutor(newExecutor(rootRepository)); + setAttachments(new ConcurrentHashMap<>(rootRepository.getAttachments())); } - @SuppressWarnings("unchecked") private CommonRepository newRepository(String accessPath, EntityEle entityEle) { EntityDef entityDef = renewEntityDef(entityEle); OperationFactory operationFactory = new OperationFactory(entityEle); - Object innerRepository = newRepository(entityDef, entityEle, operationFactory); + + AbstractRepository innerRepository = doNewRepository(entityDef, entityEle, operationFactory); + AbstractRepository proxyRepository = processRepository(innerRepository); boolean isRoot = "/".equals(accessPath); boolean isAggregated = entityEle.isAggregated(); @@ -136,13 +124,12 @@ public abstract class AbstractContextRepository extends AbstractRepositor BinderResolver binderResolver = new BinderResolver(this, entityEle); binderResolver.resolve(accessPath, entityDef, entityEle); - AliasConverter aliasConverter = new AliasConverter(entityEle); - CommonRepository repository = new CommonRepository(); repository.setEntityDef(entityDef); repository.setEntityEle(entityEle); repository.setOperationFactory(operationFactory); - repository.setProxyRepository((AbstractRepository) innerRepository); + repository.setProxyRepository(proxyRepository); + repository.setAttachments(new ConcurrentHashMap<>(innerRepository.getAttachments())); repository.setAccessPath(accessPath); repository.setRoot(isRoot); @@ -151,9 +138,8 @@ public abstract class AbstractContextRepository extends AbstractRepositor repository.setAnchorPoint(anchorPoint); repository.setBinderResolver(binderResolver); - repository.setAliasConverter(aliasConverter); - repository.setBoundEntity(false); + return repository; } @@ -170,7 +156,7 @@ public abstract class AbstractContextRepository extends AbstractRepositor } @SuppressWarnings("unchecked") - private Object newRepository(EntityDef entityDef, EntityEle entityEle, OperationFactory operationFactory) { + protected AbstractRepository doNewRepository(EntityDef entityDef, EntityEle entityEle, OperationFactory operationFactory) { Class repositoryClass = entityDef.getRepository(); Object repository; if (repositoryClass == Object.class) { @@ -184,8 +170,9 @@ public abstract class AbstractContextRepository extends AbstractRepositor defaultRepository.setEntityEle(entityEle); defaultRepository.setOperationFactory(operationFactory); defaultRepository.setExecutor(newExecutor(entityDef, entityEle)); + defaultRepository.setAttachments(Collections.emptyMap()); } - return processRepository((AbstractRepository) repository); + return (AbstractRepository) repository; } private OrderBy newDefaultOrderBy(EntityDef entityDef, EntityEle entityEle) { @@ -199,6 +186,24 @@ public abstract class AbstractContextRepository extends AbstractRepositor return null; } + private Executor newExecutor(CommonRepository rootRepository) { + delegateResolver = new DelegateResolver(this); + adapterResolver = new AdapterResolver(this); + + EntityHandler entityHandler = new BatchEntityHandler(this, rootRepository.getOperationFactory()); + processEntityClass(entityHandler); + + if (delegateResolver.isDelegated()) { + entityHandler = new AdaptiveEntityHandler(this, entityHandler); + } + + Executor executor = new ChainExecutor(this, entityHandler); + if (adapterResolver.isAdaptive()) { + executor = new AdaptiveExecutor(this, executor); + } + return executor; + } + protected abstract Executor newExecutor(EntityDef entityDef, EntityEle entityEle); protected abstract AbstractRepository processRepository(AbstractRepository repository); diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractGenericRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractGenericRepository.java index a96cf2d5eec5cdb7eaf646d0996f823736d8c239..dba3faf4c786bf18630c2ee1e1d5dc03c8d94bf4 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractGenericRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractGenericRepository.java @@ -22,7 +22,6 @@ import com.gitee.dorive.core.api.context.Context; import com.gitee.dorive.core.api.context.Selector; import com.gitee.dorive.core.entity.executor.Example; import com.gitee.dorive.core.api.repository.ListableRepository; -import com.gitee.dorive.core.api.common.MetadataHolder; import com.gitee.dorive.core.entity.operation.Operation; import lombok.Data; import lombok.EqualsAndHashCode; @@ -31,7 +30,7 @@ import java.util.List; @Data @EqualsAndHashCode(callSuper = false) -public abstract class AbstractGenericRepository extends AbstractContextRepository implements ListableRepository, MetadataHolder { +public abstract class AbstractGenericRepository extends AbstractContextRepository implements ListableRepository { @Override public int updateByExample(Context context, Object entity, Example example) { @@ -94,9 +93,4 @@ public abstract class AbstractGenericRepository extends AbstractContextRe return entities.stream().mapToInt(entity -> delete(context, entity)).sum(); } - @Override - public Object getMetadata() { - return getRootRepository().getMetadata(); - } - } diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractRepository.java index b1ee2ac208b3b2f13bf3b0ba6676b7ceb3b05b17..71a3f35fc5b1a9a2a74fdc92e27afec0d4882c31 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/AbstractRepository.java @@ -35,6 +35,7 @@ import com.gitee.dorive.core.impl.factory.OperationFactory; import lombok.Data; import java.util.List; +import java.util.Map; @Data public abstract class AbstractRepository implements Repository, Executor { @@ -43,6 +44,7 @@ public abstract class AbstractRepository implements Repository, Ex private EntityEle entityEle; private OperationFactory operationFactory; private Executor executor; + private Map attachments; public Class getEntityClass() { return entityEle.getGenericType(); diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/CommonRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/CommonRepository.java index 45705bd41721816e599384c7717fc64bd1fdfe29..a48649b9009b5aa30dcc43b48aff05ca0bf343cb 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/CommonRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/CommonRepository.java @@ -20,14 +20,12 @@ package com.gitee.dorive.core.repository; import com.gitee.dorive.api.entity.def.BindingDef; import com.gitee.dorive.api.entity.element.PropChain; import com.gitee.dorive.core.api.context.Context; -import com.gitee.dorive.core.api.common.MetadataHolder; import com.gitee.dorive.core.api.context.Selector; import com.gitee.dorive.core.entity.executor.Example; import com.gitee.dorive.core.entity.executor.OrderBy; import com.gitee.dorive.core.entity.executor.Page; import com.gitee.dorive.core.entity.executor.Result; import com.gitee.dorive.core.entity.operation.Query; -import com.gitee.dorive.core.impl.adapter.AliasConverter; import com.gitee.dorive.core.impl.binder.ContextBinder; import com.gitee.dorive.core.impl.binder.PropertyBinder; import com.gitee.dorive.core.impl.resolver.BinderResolver; @@ -39,7 +37,7 @@ import java.util.List; @Data @EqualsAndHashCode(callSuper = false) -public class CommonRepository extends ProxyRepository implements MetadataHolder { +public class CommonRepository extends ProxyRepository { private String accessPath; private boolean root; @@ -47,7 +45,6 @@ public class CommonRepository extends ProxyRepository implements MetadataHolder private OrderBy defaultOrderBy; private PropChain anchorPoint; private BinderResolver binderResolver; - private AliasConverter aliasConverter; private boolean boundEntity; @Override @@ -94,15 +91,6 @@ public class CommonRepository extends ProxyRepository implements MetadataHolder return super.executeQuery(context, query); } - @Override - public Object getMetadata() { - AbstractRepository proxyRepository = getProxyRepository(); - if (proxyRepository instanceof MetadataHolder) { - return ((MetadataHolder) proxyRepository).getMetadata(); - } - return null; - } - public Example newExampleByContext(Context context, Object rootEntity) { Example example = new Example(); for (PropertyBinder propertyBinder : binderResolver.getPropertyBinders()) { diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/DefaultRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/DefaultRepository.java index af8a15f1f4d6ea0b472a4ee056f1ded58e1fbfda..315514e0d3b17eeed617589bdf35f61b8ca04e7d 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/DefaultRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/DefaultRepository.java @@ -17,22 +17,38 @@ package com.gitee.dorive.core.repository; -import com.gitee.dorive.core.api.executor.Executor; -import com.gitee.dorive.core.api.common.MetadataHolder; +import com.gitee.dorive.core.api.common.Adapter; +import com.gitee.dorive.core.api.context.Context; +import com.gitee.dorive.core.entity.executor.Result; +import com.gitee.dorive.core.entity.operation.Operation; +import com.gitee.dorive.core.entity.operation.Query; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; @Data -@EqualsAndHashCode(callSuper = false) -public class DefaultRepository extends AbstractRepository implements MetadataHolder { +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class DefaultRepository extends AbstractRepository { + + private Adapter adapter; + + @Override + public Result executeQuery(Context context, Query query) { + if (adapter != null) { + adapter.adapt(context, query); + } + return super.executeQuery(context, query); + } @Override - public Object getMetadata() { - Executor executor = getExecutor(); - if (executor instanceof MetadataHolder) { - return ((MetadataHolder) executor).getMetadata(); + public int execute(Context context, Operation operation) { + if (adapter != null) { + adapter.adapt(context, operation); } - return null; + return super.execute(context, operation); } } diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/repository/ProxyRepository.java b/dorive-core/src/main/java/com/gitee/dorive/core/repository/ProxyRepository.java index 0793c8fea0e6bb2a3a4ffb33b2c6b95c85996735..667a2e20034a606367246cd11d22c417c3f251b6 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/repository/ProxyRepository.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/repository/ProxyRepository.java @@ -45,11 +45,11 @@ public class ProxyRepository extends AbstractRepository { return proxyRepository; } - public void setProxyRepository(AbstractRepository proxyRepository) { - if (this.proxyRepository instanceof ProxyRepository) { - ((ProxyRepository) this.proxyRepository).setProxyRepository(proxyRepository); + public void setProxyRepository(AbstractRepository repository) { + if (proxyRepository instanceof ProxyRepository) { + ((ProxyRepository) proxyRepository).setProxyRepository(repository); } - this.proxyRepository = proxyRepository; + proxyRepository = repository; } @Override diff --git a/dorive-event/src/main/java/com/gitee/dorive/event/repository/AbstractEventRepository.java b/dorive-event/src/main/java/com/gitee/dorive/event/repository/AbstractEventRepository.java index 06d6e36778b3743533777bb610c713e63c4f7ee6..93373db7718430f632815db36278234c5a0e79ed 100644 --- a/dorive-event/src/main/java/com/gitee/dorive/event/repository/AbstractEventRepository.java +++ b/dorive-event/src/main/java/com/gitee/dorive/event/repository/AbstractEventRepository.java @@ -20,9 +20,12 @@ package com.gitee.dorive.event.repository; import com.gitee.dorive.core.repository.AbstractGenericRepository; import com.gitee.dorive.core.repository.AbstractRepository; import com.gitee.dorive.core.repository.DefaultRepository; +import com.gitee.dorive.core.repository.ProxyRepository; import com.gitee.dorive.event.annotation.EnableEvent; import org.springframework.core.annotation.AnnotationUtils; +import java.util.concurrent.ConcurrentHashMap; + public abstract class AbstractEventRepository extends AbstractGenericRepository { private boolean enableEvent; @@ -36,13 +39,21 @@ public abstract class AbstractEventRepository extends AbstractGenericRepo @Override protected AbstractRepository processRepository(AbstractRepository repository) { - if (enableEvent && (repository instanceof DefaultRepository)) { - DefaultRepository defaultRepository = (DefaultRepository) repository; - EventRepository eventRepository = new EventRepository(getApplicationContext()); - eventRepository.setEntityDef(defaultRepository.getEntityDef()); - eventRepository.setEntityEle(defaultRepository.getEntityEle()); - eventRepository.setProxyRepository(repository); - return eventRepository; + if (enableEvent) { + AbstractRepository innerRepository = repository; + if (repository instanceof ProxyRepository) { + innerRepository = ((ProxyRepository) repository).getProxyRepository(); + } + if (innerRepository instanceof DefaultRepository) { + DefaultRepository defaultRepository = (DefaultRepository) innerRepository; + EventRepository eventRepository = new EventRepository(getApplicationContext()); + eventRepository.setEntityDef(defaultRepository.getEntityDef()); + eventRepository.setEntityEle(defaultRepository.getEntityEle()); + eventRepository.setOperationFactory(defaultRepository.getOperationFactory()); + eventRepository.setProxyRepository(repository); + eventRepository.setAttachments(new ConcurrentHashMap<>(defaultRepository.getAttachments())); + return eventRepository; + } } return repository; } diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/api/common/MetadataHolder.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/api/Keys.java similarity index 83% rename from dorive-core/src/main/java/com/gitee/dorive/core/api/common/MetadataHolder.java rename to dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/api/Keys.java index 33b528c4903246bbf7e1ddff196248a726f53b2f..dfabf8e8579c7ff044bf1c1d08dd064dcdd6ac8f 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/api/common/MetadataHolder.java +++ b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/api/Keys.java @@ -15,10 +15,9 @@ * limitations under the License. */ -package com.gitee.dorive.core.api.common; - -public interface MetadataHolder { - - Object getMetadata(); +package com.gitee.dorive.spring.boot.starter.api; +public interface Keys { + String TABLE_INFO = "TABLE_INFO"; + String ALIAS_ADAPTER = "ALIAS_ADAPTER"; } diff --git a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/entity/Metadata.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/entity/Metadata.java deleted file mode 100644 index ffa049d4ba770713ce2c40fd031e936739e6b1dd..0000000000000000000000000000000000000000 --- a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/entity/Metadata.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 com.gitee.dorive.spring.boot.starter.entity; - -import lombok.AllArgsConstructor; -import lombok.Data; - -@Data -@AllArgsConstructor -public class Metadata { - private Class pojoClass; -} diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/impl/adapter/AliasConverter.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/AliasAdapter.java similarity index 70% rename from dorive-core/src/main/java/com/gitee/dorive/core/impl/adapter/AliasConverter.java rename to dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/AliasAdapter.java index ce0edf307ef302fec6ee26b07f6ed4fe179d9123..d8b5e15c6a591f292d182ebe0701edf90963f88f 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/impl/adapter/AliasConverter.java +++ b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/AliasAdapter.java @@ -15,13 +15,17 @@ * limitations under the License. */ -package com.gitee.dorive.core.impl.adapter; +package com.gitee.dorive.spring.boot.starter.impl; import com.gitee.dorive.api.entity.element.EntityEle; +import com.gitee.dorive.core.api.common.Adapter; +import com.gitee.dorive.core.api.context.Context; import com.gitee.dorive.core.entity.executor.Criterion; import com.gitee.dorive.core.entity.executor.Example; import com.gitee.dorive.core.entity.executor.OrderBy; import com.gitee.dorive.core.entity.executor.UnionExample; +import com.gitee.dorive.core.entity.operation.Condition; +import com.gitee.dorive.core.entity.operation.Operation; import lombok.AllArgsConstructor; import lombok.Data; @@ -29,11 +33,33 @@ import java.util.List; @Data @AllArgsConstructor -public class AliasConverter { +public class AliasAdapter implements Adapter { private EntityEle entityEle; - public void convert(Example example) { + @Override + public void adapt(Context context, Operation operation) { + if (operation instanceof Condition) { + Condition condition = (Condition) operation; + Example example = condition.getExample(); + if (example != null) { + if (example instanceof UnionExample) { + adapt((UnionExample) example); + } else { + adapt(example); + } + } + } + } + + public void adapt(UnionExample unionExample) { + List examples = unionExample.getExamples(); + for (Example example : examples) { + adapt(example); + } + } + + public void adapt(Example example) { List selectColumns = example.getSelectColumns(); if (selectColumns != null && !selectColumns.isEmpty()) { selectColumns = entityEle.toAliases(selectColumns); @@ -57,11 +83,4 @@ public class AliasConverter { } } - public void convert(UnionExample unionExample) { - List examples = unionExample.getExamples(); - for (Example example : examples) { - convert(example); - } - } - } diff --git a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/SQLExampleBuilder.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/SQLExampleBuilder.java index a942e0bb4787faa9f211de6c47c95547fc75ba81..159ea8448296dd2b43bac856376f25df91a70006 100644 --- a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/SQLExampleBuilder.java +++ b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/impl/SQLExampleBuilder.java @@ -21,8 +21,8 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; +import com.gitee.dorive.api.constant.Operator; import com.gitee.dorive.coating.api.ExampleBuilder; import com.gitee.dorive.coating.entity.CoatingObj; import com.gitee.dorive.coating.entity.MergedRepository; @@ -30,21 +30,19 @@ import com.gitee.dorive.coating.entity.RepositoryObj; import com.gitee.dorive.coating.entity.SpecificProperties; import com.gitee.dorive.coating.impl.resolver.CoatingObjResolver; import com.gitee.dorive.coating.repository.AbstractCoatingRepository; -import com.gitee.dorive.api.constant.Operator; import com.gitee.dorive.core.api.context.Context; import com.gitee.dorive.core.entity.executor.Criterion; import com.gitee.dorive.core.entity.executor.Example; import com.gitee.dorive.core.entity.executor.OrderBy; import com.gitee.dorive.core.entity.executor.Page; -import com.gitee.dorive.core.impl.adapter.AliasConverter; import com.gitee.dorive.core.impl.binder.PropertyBinder; import com.gitee.dorive.core.impl.resolver.BinderResolver; import com.gitee.dorive.core.repository.CommonRepository; import com.gitee.dorive.core.util.CriterionUtils; import com.gitee.dorive.core.util.SqlUtils; +import com.gitee.dorive.spring.boot.starter.api.Keys; import com.gitee.dorive.spring.boot.starter.entity.ArgSegment; import com.gitee.dorive.spring.boot.starter.entity.JoinSegment; -import com.gitee.dorive.spring.boot.starter.entity.Metadata; import com.gitee.dorive.spring.boot.starter.entity.SqlSegment; import java.util.ArrayList; @@ -86,16 +84,17 @@ public class SQLExampleBuilder implements ExampleBuilder { CommonRepository executedRepository = mergedRepository.getExecutedRepository(); BinderResolver binderResolver = definedRepository.getBinderResolver(); - AliasConverter aliasConverter = executedRepository.getAliasConverter(); - TableInfo tableInfo = getTableInfo(executedRepository); - String tableName = tableInfo.getTableName(); + Map attachments = executedRepository.getAttachments(); + AliasAdapter aliasAdapter = (AliasAdapter) attachments.get(Keys.ALIAS_ADAPTER); + TableInfo tableInfo = (TableInfo) attachments.get(Keys.TABLE_INFO); + String tableName = tableInfo.getTableName(); String tableAlias = String.valueOf(letter); letter = (char) (letter + 1); Example example = repositoryObj.newExampleByCoating(context, coating); - aliasConverter.convert(example); + aliasAdapter.adapt(example); boolean dirtyQuery = example.isDirtyQuery(); anyDirtyQuery = anyDirtyQuery || dirtyQuery; @@ -160,12 +159,6 @@ public class SQLExampleBuilder implements ExampleBuilder { return example; } - private TableInfo getTableInfo(CommonRepository repository) { - Metadata metadata = (Metadata) repository.getMetadata(); - Class pojoClass = metadata.getPojoClass(); - return TableInfoHelper.getTableInfo(pojoClass); - } - private List newJoinSegments(Map sqlSegmentMap, String lastAccessPath, String absoluteAccessPath, BinderResolver binderResolver, String tableAlias) { List propertyBinders = binderResolver.getPropertyBinders(); List joinSegments = new ArrayList<>(propertyBinders.size()); diff --git a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusExecutor.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusExecutor.java index 22f9478cf26a7e7950773ce04d95e29c017bfdee..e6b3e371753b94eba879694ccabc92b4d0c9fe53 100644 --- a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusExecutor.java +++ b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusExecutor.java @@ -29,9 +29,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.gitee.dorive.api.constant.Order; import com.gitee.dorive.api.entity.def.EntityDef; import com.gitee.dorive.api.entity.element.EntityEle; -import com.gitee.dorive.core.api.context.Context; import com.gitee.dorive.core.api.common.EntityFactory; -import com.gitee.dorive.core.api.common.MetadataHolder; +import com.gitee.dorive.core.api.context.Context; import com.gitee.dorive.core.entity.executor.Criterion; import com.gitee.dorive.core.entity.executor.Example; import com.gitee.dorive.core.entity.executor.OrderBy; @@ -43,10 +42,8 @@ import com.gitee.dorive.core.entity.operation.NullableUpdate; import com.gitee.dorive.core.entity.operation.Operation; import com.gitee.dorive.core.entity.operation.Query; import com.gitee.dorive.core.entity.operation.Update; -import com.gitee.dorive.core.impl.adapter.AliasConverter; import com.gitee.dorive.core.impl.executor.AbstractExecutor; import com.gitee.dorive.spring.boot.starter.api.CriterionAppender; -import com.gitee.dorive.spring.boot.starter.entity.Metadata; import com.gitee.dorive.spring.boot.starter.impl.EntityIndexResult; import lombok.Getter; import lombok.Setter; @@ -64,32 +61,24 @@ import static com.gitee.dorive.spring.boot.starter.impl.AppenderContext.OPERATOR @Getter @Setter @ToString -public class MybatisPlusExecutor extends AbstractExecutor implements MetadataHolder { +public class MybatisPlusExecutor extends AbstractExecutor { private EntityDef entityDef; private EntityEle entityEle; private BaseMapper baseMapper; private Class pojoClass; private EntityFactory entityFactory; - private AliasConverter aliasConverter; public MybatisPlusExecutor(EntityDef entityDef, EntityEle entityEle, BaseMapper baseMapper, Class pojoClass, - EntityFactory entityFactory, - AliasConverter aliasConverter) { + EntityFactory entityFactory) { this.entityDef = entityDef; this.entityEle = entityEle; this.baseMapper = baseMapper; this.pojoClass = pojoClass; this.entityFactory = entityFactory; - this.aliasConverter = aliasConverter; - } - - @Override - public Object getMetadata() { - return new Metadata(pojoClass); } @Override @@ -107,14 +96,12 @@ public class MybatisPlusExecutor extends AbstractExecutor implements MetadataHol if (query.withoutPage()) { if (example instanceof UnionExample) { UnionExample unionExample = (UnionExample) example; - aliasConverter.convert(unionExample); QueryWrapper queryWrapper = buildQueryWrapper(unionExample); List> resultMaps = baseMapper.selectMaps(queryWrapper); List entities = reconstitute(context, resultMaps); return new EntityIndexResult(unionExample, resultMaps, entities); } else { - aliasConverter.convert(example); QueryWrapper queryWrapper = buildQueryWrapper(example); List> resultMaps = baseMapper.selectMaps(queryWrapper); List entities = reconstitute(context, resultMaps); @@ -122,7 +109,6 @@ public class MybatisPlusExecutor extends AbstractExecutor implements MetadataHol } } else { - aliasConverter.convert(example); com.gitee.dorive.core.entity.executor.Page page = example.getPage(); Page> dataPage = new Page<>(page.getCurrent(), page.getSize()); QueryWrapper queryWrapper = buildQueryWrapper(example); @@ -243,9 +229,6 @@ public class MybatisPlusExecutor extends AbstractExecutor implements MetadataHol Update update = (Update) operation; Object primaryKey = update.getPrimaryKey(); Example example = update.getExample(); - if (example != null) { - aliasConverter.convert(example); - } if (update instanceof NullableUpdate) { NullableUpdate nullableUpdate = (NullableUpdate) update; @@ -268,9 +251,7 @@ public class MybatisPlusExecutor extends AbstractExecutor implements MetadataHol Delete delete = (Delete) operation; Object primaryKey = delete.getPrimaryKey(); Example example = delete.getExample(); - if (example != null) { - aliasConverter.convert(example); - } + if (primaryKey != null) { return baseMapper.deleteById((Serializable) primaryKey); diff --git a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusRepository.java b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusRepository.java index 036f310ccb67e9f28149de344c8b2dc6133c18ec..4ffd04a3f9f5545ef6fedd28cc7ddc7d96ce21b5 100644 --- a/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusRepository.java +++ b/dorive-spring-boot-starter/src/main/java/com/gitee/dorive/spring/boot/starter/repository/MybatisPlusRepository.java @@ -19,14 +19,19 @@ package com.gitee.dorive.spring.boot.starter.repository; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; -import com.gitee.dorive.core.api.common.EntityFactory; -import com.gitee.dorive.core.api.executor.Executor; import com.gitee.dorive.api.entity.def.EntityDef; import com.gitee.dorive.api.entity.element.EntityEle; -import com.gitee.dorive.core.impl.adapter.AliasConverter; +import com.gitee.dorive.core.api.common.EntityFactory; +import com.gitee.dorive.core.api.executor.Executor; import com.gitee.dorive.core.impl.factory.DefaultEntityFactory; +import com.gitee.dorive.core.impl.factory.OperationFactory; +import com.gitee.dorive.core.repository.AbstractRepository; +import com.gitee.dorive.core.repository.DefaultRepository; import com.gitee.dorive.simple.repository.AbstractSimpleRepository; +import com.gitee.dorive.spring.boot.starter.api.Keys; +import com.gitee.dorive.spring.boot.starter.impl.AliasAdapter; import com.gitee.dorive.spring.boot.starter.impl.SQLExampleBuilder; import java.lang.reflect.ParameterizedType; @@ -34,6 +39,7 @@ import java.lang.reflect.Type; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class MybatisPlusRepository extends AbstractSimpleRepository { @@ -45,6 +51,30 @@ public class MybatisPlusRepository extends AbstractSimpleRepository doNewRepository(EntityDef entityDef, EntityEle entityEle, OperationFactory operationFactory) { + AbstractRepository repository = super.doNewRepository(entityDef, entityEle, operationFactory); + if (repository instanceof DefaultRepository) { + DefaultRepository defaultRepository = (DefaultRepository) repository; + Executor executor = defaultRepository.getExecutor(); + if (executor instanceof MybatisPlusExecutor) { + Map attachments = new ConcurrentHashMap<>(); + + AliasAdapter aliasAdapter = new AliasAdapter(entityEle); + attachments.put(Keys.ALIAS_ADAPTER, aliasAdapter); + + MybatisPlusExecutor mybatisPlusExecutor = (MybatisPlusExecutor) executor; + Class pojoClass = mybatisPlusExecutor.getPojoClass(); + TableInfo tableInfo = TableInfoHelper.getTableInfo(pojoClass); + attachments.put(Keys.TABLE_INFO, tableInfo); + + defaultRepository.setAttachments(attachments); + defaultRepository.setAdapter(aliasAdapter); + } + } + return repository; + } + @Override @SuppressWarnings("unchecked") protected Executor newExecutor(EntityDef entityDef, EntityEle entityEle) { @@ -92,9 +122,7 @@ public class MybatisPlusRepository extends AbstractSimpleRepository) mapper, (Class) pojoClass, entityFactory, aliasConverter); + return new MybatisPlusExecutor(entityDef, entityEle, (BaseMapper) mapper, (Class) pojoClass, entityFactory); } }