From ea44b25368a083100410643550fc139c8610137f Mon Sep 17 00:00:00 2001 From: Btown Date: Thu, 13 Aug 2026 14:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=9B=B4=E6=8D=A2IService->I?= =?UTF-8?q?Repository=E3=80=81ServiceImpl->CrudRepository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binder/service/CcCityInfoService.java | 4 +- .../test/binder/service/RegionService.java | 2 - .../core/test/binder/service/RoleService.java | 4 +- .../binder/service/TestUploadFileService.java | 4 +- .../service/impl/CcCityInfoServiceImpl.java | 4 +- .../binder/service/impl/RoleServiceImpl.java | 4 +- .../impl/TestUploadFileServiceImpl.java | 4 +- .../diboot/core/test/util/BeanUtilsTest.java | 4 +- .../com/diboot/core/binding/JoinsBinder.java | 18 +-- .../core/binding/binder/BaseBinder.java | 14 +- .../remote/RemoteBindQueryExecutor.java | 6 +- .../binding/cache/BindingCacheManager.java | 10 +- .../core/binding/helper/ServiceAdaptor.java | 34 ++--- .../core/binding/parser/EntityInfoCache.java | 6 +- .../query/dynamic/ExtQueryWrapper.java | 26 ++-- .../sequence/DefaultSequenceGenerator.java | 4 +- .../core/service/impl/BaseServiceImpl.java | 28 ++-- .../com/diboot/core/util/ContextHolder.java | 120 +++++++++++------- 18 files changed, 158 insertions(+), 138 deletions(-) diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/CcCityInfoService.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/CcCityInfoService.java index 4342d7ac..2ac0c1c6 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/CcCityInfoService.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/CcCityInfoService.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import diboot.core.test.binder.entity.CcCityInfo; /** @@ -24,6 +24,6 @@ import diboot.core.test.binder.entity.CcCityInfo; * @version v2.0 * @date 2019/1/5 */ -public interface CcCityInfoService extends IService { +public interface CcCityInfoService extends IRepository { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RegionService.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RegionService.java index 3bbcd4d7..b27983a2 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RegionService.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RegionService.java @@ -15,10 +15,8 @@ */ package diboot.core.test.binder.service; -import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import diboot.core.test.binder.entity.Region; -import diboot.core.test.binder.entity.Role; /** * 行政区划相关Service diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RoleService.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RoleService.java index 1c681dab..cbb52e25 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RoleService.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/RoleService.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import diboot.core.test.binder.entity.Role; /** @@ -24,6 +24,6 @@ import diboot.core.test.binder.entity.Role; * @version v2.0 * @date 2019/1/5 */ -public interface RoleService extends IService { +public interface RoleService extends IRepository { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/TestUploadFileService.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/TestUploadFileService.java index 402aa463..a2654472 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/TestUploadFileService.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/TestUploadFileService.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import diboot.core.test.binder.entity.TestUploadFile; /** @@ -24,6 +24,6 @@ import diboot.core.test.binder.entity.TestUploadFile; * @version v2.0 * @date 2021/08/27 */ -public interface TestUploadFileService extends IService { +public interface TestUploadFileService extends IRepository { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/CcCityInfoServiceImpl.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/CcCityInfoServiceImpl.java index 10ed1c5f..2e357810 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/CcCityInfoServiceImpl.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/CcCityInfoServiceImpl.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service.impl; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.baomidou.mybatisplus.extension.repository.CrudRepository; import diboot.core.test.binder.entity.CcCityInfo; import diboot.core.test.binder.mapper.CcCityInfoMapper; import diboot.core.test.binder.service.CcCityInfoService; @@ -27,6 +27,6 @@ import org.springframework.stereotype.Service; * @version 2018/12/23 */ @Service -public class CcCityInfoServiceImpl extends ServiceImpl implements CcCityInfoService { +public class CcCityInfoServiceImpl extends CrudRepository implements CcCityInfoService { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java index ec3448e6..209bcf6e 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service.impl; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.baomidou.mybatisplus.extension.repository.CrudRepository; import diboot.core.test.binder.entity.Role; import diboot.core.test.binder.mapper.RoleMapper; import diboot.core.test.binder.service.RoleService; @@ -27,6 +27,6 @@ import org.springframework.stereotype.Service; * @version 2018/12/23 */ @Service -public class RoleServiceImpl extends ServiceImpl implements RoleService { +public class RoleServiceImpl extends CrudRepository implements RoleService { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/TestUploadFileServiceImpl.java b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/TestUploadFileServiceImpl.java index ab50a647..bf3c715b 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/TestUploadFileServiceImpl.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/binder/service/impl/TestUploadFileServiceImpl.java @@ -15,7 +15,7 @@ */ package diboot.core.test.binder.service.impl; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.baomidou.mybatisplus.extension.repository.CrudRepository; import diboot.core.test.binder.entity.TestUploadFile; import diboot.core.test.binder.mapper.TestUploadFileMapper; import diboot.core.test.binder.service.TestUploadFileService; @@ -27,6 +27,6 @@ import org.springframework.stereotype.Service; * @version 2021/08/27 */ @Service -public class TestUploadFileServiceImpl extends ServiceImpl implements TestUploadFileService { +public class TestUploadFileServiceImpl extends CrudRepository implements TestUploadFileService { } diff --git a/diboot-core-starter/src/test/java/diboot/core/test/util/BeanUtilsTest.java b/diboot-core-starter/src/test/java/diboot/core/test/util/BeanUtilsTest.java index 834aa35f..5e6519db 100644 --- a/diboot-core-starter/src/test/java/diboot/core/test/util/BeanUtilsTest.java +++ b/diboot-core-starter/src/test/java/diboot/core/test/util/BeanUtilsTest.java @@ -17,7 +17,7 @@ package diboot.core.test.util; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.cache.BindingCacheManager; import com.diboot.core.config.Cons; import com.diboot.core.entity.Dictionary; @@ -98,7 +98,7 @@ public class BeanUtilsTest { System.out.println(" takes "+ end); start = System.currentTimeMillis(); for(int i=0; i<10000; i++){ - Class dict = ((IService)dictionaryService).getEntityClass(); + Class dict = ((IRepository)dictionaryService).getEntityClass(); } end = System.currentTimeMillis() - start; System.out.println(" takes "+ end); diff --git a/diboot-core/src/main/java/com/diboot/core/binding/JoinsBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/JoinsBinder.java index 0dec4a8c..44609d72 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/JoinsBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/JoinsBinder.java @@ -18,7 +18,7 @@ package com.diboot.core.binding; 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.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.helper.ServiceAdaptor; import com.diboot.core.binding.parser.ParserCache; import com.diboot.core.binding.query.dynamic.AnnoJoiner; @@ -94,12 +94,12 @@ public class JoinsBinder { public static Long queryCount(QueryWrapper queryWrapper, Class entityClazz){ // 非动态查询,走BaseService if(!(queryWrapper instanceof DynamicJoinQueryWrapper)){ - IService iService = ContextHolder.getIServiceByEntity(entityClazz); - if(iService != null){ - return iService.count(queryWrapper); + IRepository iRepository = ContextHolder.getIRepositoryByEntity(entityClazz); + if(iRepository != null){ + return iRepository.count(queryWrapper); } else{ - throw new InvalidUsageException("单表查询对象无BaseService/IService实现: {}", entityClazz.getSimpleName()); + throw new InvalidUsageException("单表查询对象无BaseService/IRepository实现: {}", entityClazz.getSimpleName()); } } long begin = System.currentTimeMillis(); @@ -128,12 +128,12 @@ public class JoinsBinder { private static List executeJoinQuery(QueryWrapper queryWrapper, Class entityClazz, Pagination pagination, boolean limit1){ // 非动态查询,走BaseService if(!(queryWrapper instanceof DynamicJoinQueryWrapper)){ - IService iService = ContextHolder.getIServiceByEntity(entityClazz); - if(iService != null){ - return ServiceAdaptor.queryList(iService, (QueryWrapper)queryWrapper, pagination, entityClazz); + IRepository iRepository = ContextHolder.getIRepositoryByEntity(entityClazz); + if(iRepository != null){ + return ServiceAdaptor.queryList(iRepository, (QueryWrapper)queryWrapper, pagination, entityClazz); } else{ - throw new InvalidUsageException("单表查询对象无BaseService/IService实现: {}", entityClazz.getSimpleName()); + throw new InvalidUsageException("单表查询对象无BaseService/IRepository实现: {}", entityClazz.getSimpleName()); } } long begin = System.currentTimeMillis(); diff --git a/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java index dfc06412..95fa6625 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java @@ -17,7 +17,7 @@ package com.diboot.core.binding.binder; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.annotation.Module; import com.diboot.core.binding.binder.remote.RemoteBindDTO; import com.diboot.core.binding.cache.BindingCacheManager; @@ -65,7 +65,7 @@ public abstract class BaseBinder { /** * 被关联对象的Service实例 */ - protected IService referencedService; + protected IRepository referencedService; /** * 初始化QueryWrapper */ @@ -521,16 +521,16 @@ public abstract class BaseBinder { * @param entityClass * @return */ - private IService getService(Class entityClass, Module moduleAnno){ + private IRepository getService(Class entityClass, Module moduleAnno){ // 根据entity获取Service - IService iService = ContextHolder.getIServiceByEntity(entityClass); - if(iService == null){ + IRepository iRepository = ContextHolder.getIRepositoryByEntity(entityClass); + if(iRepository == null){ // 本地绑定需确保有Service实现类 if(moduleAnno == null){ - throw new InvalidUsageException("{} 无 BaseService/IService实现类,无法执行注解绑定!", entityClass.getSimpleName()); + throw new InvalidUsageException("{} 无 BaseService/IRepository实现类,无法执行注解绑定!", entityClass.getSimpleName()); } } - return iService; + return iRepository; } /** diff --git a/diboot-core/src/main/java/com/diboot/core/binding/binder/remote/RemoteBindQueryExecutor.java b/diboot-core/src/main/java/com/diboot/core/binding/binder/remote/RemoteBindQueryExecutor.java index 546efbab..522f59cb 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/binder/remote/RemoteBindQueryExecutor.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/binder/remote/RemoteBindQueryExecutor.java @@ -17,7 +17,7 @@ package com.diboot.core.binding.binder.remote; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.cache.BindingCacheManager; import com.diboot.core.binding.helper.WrapperHelper; import com.diboot.core.binding.parser.PropInfo; @@ -108,7 +108,7 @@ public class RemoteBindQueryExecutor { * @return */ private static List> getMapList(Class entityClass, Wrapper queryWrapper) { - IService referencedService = ContextHolder.getIServiceByEntity(entityClass); + IRepository referencedService = ContextHolder.getIRepositoryByEntity(entityClass); if(referencedService instanceof BaseService){ return ((BaseService)referencedService).getMapList(queryWrapper); } @@ -124,7 +124,7 @@ public class RemoteBindQueryExecutor { * @return */ private static List getEntityList(Class entityClass, Wrapper queryWrapper) { - IService referencedService = ContextHolder.getIServiceByEntity(entityClass); + IRepository referencedService = ContextHolder.getIRepositoryByEntity(entityClass); if(referencedService instanceof BaseService){ return ((BaseService)referencedService).getEntityList(queryWrapper); } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/cache/BindingCacheManager.java b/diboot-core/src/main/java/com/diboot/core/binding/cache/BindingCacheManager.java index bcbba051..686debeb 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/cache/BindingCacheManager.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/cache/BindingCacheManager.java @@ -17,7 +17,7 @@ package com.diboot.core.binding.cache; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.parser.EntityInfoCache; import com.diboot.core.binding.parser.PropInfo; import com.diboot.core.cache.StaticMemoryCacheManager; @@ -214,15 +214,15 @@ public class BindingCacheManager { private static final BeanInitializer ENTITY_INFO_CACHE_INITIALIZER = BeanInitUtils.lazyInit(() -> { StaticMemoryCacheManager cacheManager = getCacheManager(); // 初始化有service的entity缓存 - Map serviceMap = ContextHolder.getApplicationContext().getBeansOfType(IService.class); + Map serviceMap = ContextHolder.getApplicationContext().getBeansOfType(IRepository.class); Set uniqueEntitySet = new HashSet<>(); if (V.notEmpty(serviceMap)) { - for (Map.Entry entry : serviceMap.entrySet()) { + for (Map.Entry entry : serviceMap.entrySet()) { Class entityClass = entry.getValue().getEntityClass(); if (entityClass != null) { - IService entityIService = entry.getValue(); + IRepository entityIRepository = entry.getValue(); if (uniqueEntitySet.contains(entityClass.getName())) { - if (entityIService.getClass().getAnnotation(Primary.class) != null) { + if (entityIRepository.getClass().getAnnotation(Primary.class) != null) { EntityInfoCache entityInfoCache = cacheManager.getCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), EntityInfoCache.class); if (entityInfoCache != null) { entityInfoCache.setService(entry.getKey()); diff --git a/diboot-core/src/main/java/com/diboot/core/binding/helper/ServiceAdaptor.java b/diboot-core/src/main/java/com/diboot/core/binding/helper/ServiceAdaptor.java index 1984b644..0fe1daf6 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/helper/ServiceAdaptor.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/helper/ServiceAdaptor.java @@ -18,7 +18,7 @@ package com.diboot.core.binding.helper; 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.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.service.BaseService; import com.diboot.core.vo.Pagination; @@ -34,49 +34,49 @@ public class ServiceAdaptor { /** * 查询一个实体 - * @param iService + * @param iRepository * @param queryWrapper * @param * @return */ - public static E getSingleEntity(IService iService, QueryWrapper queryWrapper){ - if(iService instanceof BaseService){ - BaseService baseService = (BaseService)iService; + public static E getSingleEntity(IRepository iRepository, QueryWrapper queryWrapper){ + if(iRepository instanceof BaseService){ + BaseService baseService = (BaseService)iRepository; return (E)baseService.getSingleEntity(queryWrapper); } else{ - return (E)iService.getOne(queryWrapper); + return (E)iRepository.getOne(queryWrapper); } } /** * 查询实体列表 - * @param iService + * @param iRepository * @param queryWrapper * @param * @return */ - public static List queryList(IService iService, QueryWrapper queryWrapper){ - if(iService instanceof BaseService){ - BaseService baseService = (BaseService)iService; + public static List queryList(IRepository iRepository, QueryWrapper queryWrapper){ + if(iRepository instanceof BaseService){ + BaseService baseService = (BaseService)iRepository; return (List)baseService.getEntityList(queryWrapper); } else{ - return iService.list(queryWrapper); + return iRepository.list(queryWrapper); } } /** * 查询一页的实体列表 - * @param iService + * @param iRepository * @param queryWrapper * @param pagination * @param * @return */ - public static List queryList(IService iService, QueryWrapper queryWrapper, Pagination pagination, Class entityClass) { - if(iService instanceof BaseService){ - BaseService baseService = (BaseService)iService; + public static List queryList(IRepository iRepository, QueryWrapper queryWrapper, Pagination pagination, Class entityClass) { + if(iRepository instanceof BaseService){ + BaseService baseService = (BaseService)iRepository; return (List)baseService.getEntityList(queryWrapper, pagination); } else{ @@ -85,7 +85,7 @@ public class ServiceAdaptor { } if(pagination != null){ IPage page = convertToIPage(pagination, entityClass); - page = iService.page(page, queryWrapper); + page = iRepository.page(page, queryWrapper); // 如果重新执行了count进行查询,则更新pagination中的总数 if(page.searchCount()){ pagination.setTotalCount(page.getTotal()); @@ -93,7 +93,7 @@ public class ServiceAdaptor { return page.getRecords(); } else{ - return iService.list(queryWrapper); + return iRepository.list(queryWrapper); } } } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/parser/EntityInfoCache.java b/diboot-core/src/main/java/com/diboot/core/binding/parser/EntityInfoCache.java index 8caaf8e2..2529d44e 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/parser/EntityInfoCache.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/parser/EntityInfoCache.java @@ -2,7 +2,7 @@ package com.diboot.core.binding.parser; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.cache.BindingCacheManager; import com.diboot.core.util.ContextHolder; import com.diboot.core.util.S; @@ -72,8 +72,8 @@ public class EntityInfoCache implements Serializable { this.serviceBeanName = serviceBeanName; } - public IService getService(){ - return this.serviceBeanName == null ? null : (IService) ContextHolder.getApplicationContext().getBean(this.serviceBeanName); + public IRepository getService(){ + return this.serviceBeanName == null ? null : (IRepository) ContextHolder.getApplicationContext().getBean(this.serviceBeanName); } public void setBaseMapper(Class mapper) { diff --git a/diboot-core/src/main/java/com/diboot/core/binding/query/dynamic/ExtQueryWrapper.java b/diboot-core/src/main/java/com/diboot/core/binding/query/dynamic/ExtQueryWrapper.java index 23a71305..2e56cb58 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/query/dynamic/ExtQueryWrapper.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/query/dynamic/ExtQueryWrapper.java @@ -16,7 +16,7 @@ package com.diboot.core.binding.query.dynamic; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.helper.ServiceAdaptor; import com.diboot.core.binding.parser.ParserCache; import com.diboot.core.exception.InvalidUsageException; @@ -60,12 +60,12 @@ public class ExtQueryWrapper extends QueryWrapper { */ public T queryOne(Class entityClazz){ this.mainEntityClass = entityClazz; - IService iService = ContextHolder.getIServiceByEntity(this.mainEntityClass); - if(iService != null){ - return ServiceAdaptor.getSingleEntity(iService, this); + IRepository iRepository = ContextHolder.getIRepositoryByEntity(this.mainEntityClass); + if(iRepository != null){ + return ServiceAdaptor.getSingleEntity(iRepository, this); } else{ - throw new InvalidUsageException("查询对象无BaseService/IService实现: {}",this.mainEntityClass.getSimpleName()); + throw new InvalidUsageException("查询对象无BaseService/IRepository实现: {}",this.mainEntityClass.getSimpleName()); } } @@ -76,12 +76,12 @@ public class ExtQueryWrapper extends QueryWrapper { */ public List queryList(Class entityClazz){ this.mainEntityClass = entityClazz; - IService iService = ContextHolder.getIServiceByEntity(entityClazz); - if(iService != null){ - return ServiceAdaptor.queryList(iService, this); + IRepository iRepository = ContextHolder.getIRepositoryByEntity(entityClazz); + if(iRepository != null){ + return ServiceAdaptor.queryList(iRepository, this); } else{ - throw new InvalidUsageException("查询对象无BaseService/IService实现: {}",entityClazz.getSimpleName()); + throw new InvalidUsageException("查询对象无BaseService/IRepository实现: {}",entityClazz.getSimpleName()); } } @@ -92,12 +92,12 @@ public class ExtQueryWrapper extends QueryWrapper { */ public List queryList(Class entityClazz, Pagination pagination){ this.mainEntityClass = entityClazz; - IService iService = ContextHolder.getIServiceByEntity(entityClazz); - if(iService != null){ - return ServiceAdaptor.queryList(iService, this, pagination, entityClazz); + IRepository iRepository = ContextHolder.getIRepositoryByEntity(entityClazz); + if(iRepository != null){ + return ServiceAdaptor.queryList(iRepository, this, pagination, entityClazz); } else{ - throw new InvalidUsageException("查询对象无BaseService/IService实现: {}", entityClazz.getSimpleName()); + throw new InvalidUsageException("查询对象无BaseService/IRepository实现: {}", entityClazz.getSimpleName()); } } diff --git a/diboot-core/src/main/java/com/diboot/core/extension/sequence/DefaultSequenceGenerator.java b/diboot-core/src/main/java/com/diboot/core/extension/sequence/DefaultSequenceGenerator.java index 5c559878..6a4c70b1 100644 --- a/diboot-core/src/main/java/com/diboot/core/extension/sequence/DefaultSequenceGenerator.java +++ b/diboot-core/src/main/java/com/diboot/core/extension/sequence/DefaultSequenceGenerator.java @@ -17,7 +17,7 @@ package com.diboot.core.extension.sequence; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.diboot.core.binding.cache.BindingCacheManager; import com.diboot.core.binding.parser.EntityInfoCache; import com.diboot.core.extension.sequence.counter.SeqCounter; @@ -70,7 +70,7 @@ public class DefaultSequenceGenerator extends SequenceGenerator { @SneakyThrows protected synchronized long getInitValue() { EntityInfoCache entityInfo = BindingCacheManager.getEntityInfoByClass(entityClass); - IService entityService = entityInfo.getService(); + IRepository entityService = entityInfo.getService(); QueryWrapper queryWrapper = new QueryWrapper().select(entityInfo.getPropInfo().getColumnByField(fieldName)) .orderByDesc(entityInfo.getPropInfo().getIdColumn()); //如果id非有序,可换成 create_time T entity = entityService.getOne(queryWrapper, false); diff --git a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java index 09223590..6a94e086 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java +++ b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java @@ -30,8 +30,8 @@ import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.baomidou.mybatisplus.extension.service.IService; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.baomidou.mybatisplus.extension.repository.CrudRepository; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers; import com.diboot.core.binding.Binder; import com.diboot.core.binding.JoinsBinder; @@ -79,7 +79,7 @@ import java.util.stream.Collectors; * @date 2019/01/01 */ @SuppressWarnings({"unchecked", "rawtypes", "JavaDoc"}) -public class BaseServiceImpl, T> extends ServiceImpl implements BaseService { +public class BaseServiceImpl, T> extends CrudRepository implements BaseService { private static final Logger log = LoggerFactory.getLogger(BaseServiceImpl.class); /** @@ -411,7 +411,7 @@ public class BaseServiceImpl, T> extends ServiceImpl for(Object entity : entityList){ this.beforeUpdate((T)entity); } - boolean success = super.updateBatchById(entityList); + boolean success = super.updateBatchById(entityList, IRepository.DEFAULT_BATCH_SIZE); if(success) { for(Object entity : entityList){ this.afterUpdate((T)entity); @@ -541,9 +541,9 @@ public class BaseServiceImpl, T> extends ServiceImpl selectOld.select(followerColumnName); } - IService iService = entityInfo.getService(); + IRepository iRepository = entityInfo.getService(); BaseMapper baseMapper = entityInfo.getBaseMapper(); - List oldEntityList = (iService != null)? iService.list(selectOld) : baseMapper.selectList(selectOld); + List oldEntityList = (iRepository != null)? iRepository.list(selectOld) : baseMapper.selectList(selectOld); // 删除失效关联 List delIds = new ArrayList<>(); for (R entity : oldEntityList) { @@ -559,8 +559,8 @@ public class BaseServiceImpl, T> extends ServiceImpl } if (!delIds.isEmpty()) { if (isExistPk) { - if (iService != null) { - iService.removeByIds(delIds); + if (iRepository != null) { + iRepository.removeByIds(delIds); } else { baseMapper.deleteByIds(delIds); } @@ -570,8 +570,8 @@ public class BaseServiceImpl, T> extends ServiceImpl if (queryConsumer != null) { queryConsumer.accept(selectOld); } - if (iService != null) { - iService.remove(delOld); + if (iRepository != null) { + iRepository.remove(delOld); } else if (!delIds.isEmpty()) { baseMapper.delete(delOld); } @@ -596,11 +596,11 @@ public class BaseServiceImpl, T> extends ServiceImpl log.error("新增关联异常:", e); throw new BusinessException(Status.FAIL_EXCEPTION); } - if (iService != null) { - if (iService instanceof BaseService) { - ((BaseService) iService).createEntities(n2nRelations); + if (iRepository != null) { + if (iRepository instanceof BaseService) { + ((BaseService) iRepository).createEntities(n2nRelations); } else { - iService.saveBatch(n2nRelations); + iRepository.saveBatch(n2nRelations, IRepository.DEFAULT_BATCH_SIZE); } } else { // 新增关联,无service只能循环插入 diff --git a/diboot-core/src/main/java/com/diboot/core/util/ContextHolder.java b/diboot-core/src/main/java/com/diboot/core/util/ContextHolder.java index a7f0b20e..9ff1aeb6 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/ContextHolder.java +++ b/diboot-core/src/main/java/com/diboot/core/util/ContextHolder.java @@ -17,7 +17,7 @@ package com.diboot.core.util; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.repository.IRepository; import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; import com.diboot.core.binding.cache.BindingCacheManager; import com.diboot.core.binding.parser.EntityInfoCache; @@ -46,6 +46,7 @@ import java.util.Map; /** * Spring上下文帮助类 + * * @author mazc@dibo.ltd * @version 2.0 * @date 2019/01/01 @@ -81,11 +82,11 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen * 获取ApplicationContext上下文 */ public static ApplicationContext getApplicationContext() { - if (APPLICATION_CONTEXT == null){ + if (APPLICATION_CONTEXT == null) { log.debug("ApplicationContext未初始化,通过ContextLoader获取!"); APPLICATION_CONTEXT = ContextLoader.getCurrentWebApplicationContext(); } - if(APPLICATION_CONTEXT == null){ + if (APPLICATION_CONTEXT == null) { log.warn("无法获取ApplicationContext,请确保ComponentScan扫描路径包含com.diboot包路径,并在Spring初始化之后调用接口!"); new InvalidUsageException("检查调用时机").printStackTrace(); } @@ -94,25 +95,27 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen /** * 根据beanId获取Bean实例 + * * @param beanId * @return */ - public static Object getBean(String beanId){ + public static Object getBean(String beanId) { return getApplicationContext().getBean(beanId); } /** * 获取指定类型的单个Bean实例 + * * @param clazz * @return */ - public static T getBean(Class clazz){ + public static T getBean(Class clazz) { List clazzInstances = getBeans(clazz); - if(clazzInstances == null){ + if (clazzInstances == null) { log.debug("instance not found: {}", clazz.getSimpleName()); return null; } - if(clazzInstances.size() > 1){ + if (clazzInstances.size() > 1) { for (T instance : clazzInstances) { if (V.equals(clazz.getSimpleName(), instance.getClass().getSimpleName())) { return instance; @@ -125,13 +128,14 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen /** * 获取指定类型的全部实现类 + * * @param type * @param * @return */ - public static List getBeans(Class type){ + public static List getBeans(Class type) { Map map = getApplicationContext().getBeansOfType(type); - if(V.isEmpty(map)){ + if (V.isEmpty(map)) { return null; } List beanList = new ArrayList<>(map.size()); @@ -141,47 +145,61 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen /** * 根据注解获取beans + * * @param annotationType * @return */ - public static List getBeansByAnnotation(Class annotationType){ + public static List getBeansByAnnotation(Class annotationType) { Map map = getApplicationContext().getBeansWithAnnotation(annotationType); - if(V.isEmpty(map)){ + if (V.isEmpty(map)) { return null; } return new ArrayList<>(map.values()); } /** - * 根据Entity获取对应的IService实现 + * 根据Entity获取对应的IRepository实现 + * * @param entity * @return */ - public static IService getIServiceByEntity(Class entity){ + public static IRepository getIRepositoryByEntity(Class entity) { EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity); - IService iService = entityInfoCache != null? entityInfoCache.getService() : null; - if(iService == null){ - log.debug("未能识别到Entity: {} 的IService实现!", entity.getName()); + IRepository iRepository = entityInfoCache != null ? entityInfoCache.getService() : null; + if (iRepository == null) { + log.debug("未能识别到Entity: {} 的IRepository(Service)实现!", entity.getName()); } - return iService; + return iRepository; + } + + + /** + * Use getIRepositoryByEntity + * + * @param entity + * @return {@link IRepository } + */ + @Deprecated + public static IRepository getIServiceByEntity(Class entity) { + return getIRepositoryByEntity(entity); } /** * 根据Entity获取对应的BaseService实现 + * * @param entity * @return */ - public static BaseService getBaseServiceByEntity(Class entity){ + public static BaseService getBaseServiceByEntity(Class entity) { EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity); - IService iService = entityInfoCache != null? entityInfoCache.getService() : null; - if(iService == null){ - log.debug("未能识别到Entity: {} 的Service实现!", entity.getName()); + IRepository iRepository = entityInfoCache != null ? entityInfoCache.getService() : null; + if (iRepository == null) { + log.debug("未能识别到Entity: {} 的IRepository(Service)实现!", entity.getName()); return null; } - if(iService instanceof BaseService){ - return (BaseService)iService; - } - else { + if (iRepository instanceof BaseService) { + return (BaseService) iRepository; + } else { log.warn("Entity的service实现类: {} 非BaseService实现!", entityInfoCache.getService()); return null; } @@ -189,42 +207,47 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen /** * 根据Entity获取对应的BaseMapper实现 + * * @param entityClass * @return */ - public static BaseMapper getBaseMapperByEntity(Class entityClass){ + public static BaseMapper getBaseMapperByEntity(Class entityClass) { return ParserCache.getMapperInstance(entityClass); } /** * 获取Entity主键列名 + * * @return */ - public static String getIdColumnName(Class entity){ + public static String getIdColumnName(Class entity) { PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entity); - return propInfoCache != null? propInfoCache.getIdColumn() : null; + return propInfoCache != null ? propInfoCache.getIdColumn() : null; } /** * 获取Entity主键字段名 + * * @return */ - public static String getIdFieldName(Class entity){ + public static String getIdFieldName(Class entity) { PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entity); return propInfoCache != null ? propInfoCache.getIdFieldName() : null; } /** * 获取Entity主键 + * * @return */ @Deprecated - public static String getPrimaryKey(Class entity){ + public static String getPrimaryKey(Class entity) { return getIdFieldName(entity); } /** * 获取JdbcUrl + * * @return */ public static String getJdbcUrl() { @@ -233,37 +256,36 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen return null; } String jdbcUrl; - try{ + try { DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); jdbcUrl = connection.getMetaData().getURL(); connection.close(); return jdbcUrl; - } - catch (Exception e){ + } catch (Exception e) { log.warn("获取JDBC URL异常: {}", e.getMessage()); } // 候补识别方式,暂时保留 Environment environment = applicationContext.getEnvironment(); jdbcUrl = environment.getProperty("spring.datasource.url"); - if(jdbcUrl == null){ + if (jdbcUrl == null) { jdbcUrl = environment.getProperty("spring.datasource.druid.url"); } - if(jdbcUrl == null){ + if (jdbcUrl == null) { String master = environment.getProperty("spring.datasource.dynamic.primary"); - if(master != null){ - jdbcUrl = environment.getProperty("spring.datasource.dynamic.datasource."+master+".url"); + if (master != null) { + jdbcUrl = environment.getProperty("spring.datasource.dynamic.datasource." + master + ".url"); } } - if(jdbcUrl == null){ + if (jdbcUrl == null) { String names = environment.getProperty("spring.shardingsphere.datasource.names"); - if(names != null){ - jdbcUrl = environment.getProperty("spring.shardingsphere.datasource."+ names.split(",")[0] +".url"); + if (names != null) { + jdbcUrl = environment.getProperty("spring.shardingsphere.datasource." + names.split(",")[0] + ".url"); } } - if(jdbcUrl == null){ + if (jdbcUrl == null) { String urlConfigItem = environment.getProperty("diboot.datasource.url.config"); - if(urlConfigItem != null){ + if (urlConfigItem != null) { jdbcUrl = environment.getProperty(urlConfigItem); } } @@ -272,24 +294,24 @@ public class ContextHolder implements ApplicationContextAware, ApplicationListen /** * 获取数据库类型 + * * @return */ - public static String getDatabaseType(){ - if(DATABASE_TYPE != null){ + public static String getDatabaseType() { + if (DATABASE_TYPE != null) { return DATABASE_TYPE; } String jdbcUrl = getJdbcUrl(); - if(jdbcUrl != null){ + if (jdbcUrl != null) { DbType dbType = JdbcUtils.getDbType(jdbcUrl); DATABASE_TYPE = dbType.getDb(); - if(DATABASE_TYPE.startsWith(DbType.SQL_SERVER.getDb())){ + if (DATABASE_TYPE.startsWith(DbType.SQL_SERVER.getDb())) { DATABASE_TYPE = DbType.SQL_SERVER.getDb(); - } - else if(DATABASE_TYPE.startsWith(DbType.ORACLE_12C.getDb())){ + } else if (DATABASE_TYPE.startsWith(DbType.ORACLE_12C.getDb())) { DATABASE_TYPE = DbType.ORACLE.getDb(); } } - if(DATABASE_TYPE == null){ + if (DATABASE_TYPE == null) { log.warn("无法识别数据库类型,请检查数据源配置:spring.datasource.url等"); } return DATABASE_TYPE; -- Gitee