diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java index dcb9404..471f03a 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diboot.components.msg.entity.Message; import com.diboot.components.msg.service.MessageService; import com.diboot.components.msg.vo.MessageVO; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.service.BaseService; import com.diboot.core.vo.JsonResult; @@ -39,7 +39,7 @@ public class MessageController extends BaseCrudRestController { // 查询当前页的Entity主表数据 List entityList = getService().getEntityList(queryWrapper, pagination); // 自动转换VO中注解绑定的关联 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, MessageVO.class); + List voList = RelationsBinder.convertAndBind(entityList, MessageVO.class); //返回结果 return new JsonResult(Status.OK, voList).bindPagination(pagination); } diff --git a/diboot-core/README.md b/diboot-core/README.md index d1bbb5b..d860c39 100644 --- a/diboot-core/README.md +++ b/diboot-core/README.md @@ -48,14 +48,14 @@ private List roleList; ~~~java // 调用AnnotationBindingManager自动绑定注解相关关联 //List voList = ...; -AnnotationBindingManager.autoBind(voList); +RelationsBinder.bind(voList); ~~~ #### 2. 自动转型并绑定关联(需要转型) ~~~java // 获取Entity列表 List entityList = userService.getEntityList(queryWrapper); // 调用AnnotationBindingManager自动绑定注解相关关联 -List voList = AnnotationBindingManager.autoConvertAndBind(userList, MyUserVO.class); +List voList = RelationsBinder.convertAndBind(userList, MyUserVO.class); ~~~ diff --git a/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java index f117190..0dd82c5 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java @@ -1,16 +1,23 @@ package com.diboot.core.binding; +import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.parser.MiddleTable; +import com.diboot.core.config.BaseConfig; import com.diboot.core.service.BaseService; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.IGetter; import com.diboot.core.util.S; +import com.diboot.core.vo.Pagination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; /** * 关系绑定Binder父类 @@ -31,7 +38,7 @@ public abstract class BaseBinder { /** * 被关联对象的Service实例 */ - protected BaseService referencedService; + protected IService referencedService; /*** * DO对象中的主键属性名 */ @@ -141,4 +148,48 @@ public abstract class BaseBinder { */ public abstract void bind(); + /** + * 获取EntityList + * @param queryWrapper + * @return + */ + protected List getEntityList(Wrapper queryWrapper) { + if(referencedService instanceof BaseService){ + return ((BaseService)referencedService).getEntityList(queryWrapper, null); + } + else{ + List list = referencedService.list(queryWrapper); + return checkedList(list); + } + } + + /** + * 获取Map结果 + * @param queryWrapper + * @return + */ + protected List> getMapList(Wrapper queryWrapper) { + if(referencedService instanceof BaseService){ + return ((BaseService)referencedService).getMapList(queryWrapper); + } + else{ + List> list = referencedService.listMaps(queryWrapper); + return checkedList(list); + } + } + + /** + * 检查list,结果过多打印warn + * @param list + * @return + */ + private List checkedList(List list){ + if(list == null){ + list = Collections.emptyList(); + } + else if(list.size() > BaseConfig.getBatchSize()){ + log.warn("单次查询记录数量过大,返回结果数={},请检查!", list.size()); + } + return list; + } } \ No newline at end of file diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java index e8c2b80..cdfce0d 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import com.diboot.core.util.*; import org.slf4j.Logger; @@ -31,7 +32,7 @@ public class EntityBinder extends BaseBinder { * @param referencedService * @param voList */ - public EntityBinder(BaseService referencedService, List voList){ + public EntityBinder(IService referencedService, List voList){ this.referencedService = referencedService; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -85,7 +86,7 @@ public class EntityBinder extends BaseBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), middleTableColumnValueList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ // 转换entity列表为Map Map listMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey)); @@ -108,7 +109,7 @@ public class EntityBinder extends BaseBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ String refEntityPKFieldName = S.toLowerCaseCamel(referencedEntityPrimaryKey); for(T entity : list){ diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java index eae1109..ed84c8f 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.S; @@ -25,7 +26,7 @@ public class EntityListBinder extends EntityBinder { * @param serviceInstance * @param voList */ - public EntityListBinder(BaseService serviceInstance, List voList){ + public EntityListBinder(IService serviceInstance, List voList){ this.referencedService = serviceInstance; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -56,7 +57,7 @@ public class EntityListBinder extends EntityBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), entityIdList); // 查询entity列表: List - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); // 转换entity列表为Map Map entityMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey)); for(Map.Entry entry : middleTableResultMap.entrySet()){ @@ -80,7 +81,7 @@ public class EntityListBinder extends EntityBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ for(T entity : list){ String keyValue = BeanUtils.getStringProperty(entity, S.toLowerCaseCamel(referencedEntityPrimaryKey)); diff --git a/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java index d8a73f3..55b2331 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.annotation.BindField; import com.diboot.core.service.BaseService; import com.diboot.core.util.*; @@ -33,7 +34,7 @@ public class FieldBinder extends BaseBinder { * @param serviceInstance * @param voList */ - public FieldBinder(BaseService serviceInstance, List voList){ + public FieldBinder(IService serviceInstance, List voList){ this.referencedService = serviceInstance; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -113,7 +114,7 @@ public class FieldBinder extends BaseBinder { } // 获取匹配结果的mapList - List> mapList = referencedService.getMapList(queryWrapper); + List> mapList = getMapList(queryWrapper); if(V.isEmpty(mapList)){ return; } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java b/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java index 685b37d..03a79cb 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java @@ -1,6 +1,9 @@ package com.diboot.core.binding.manager; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.BaseBinder; +import com.diboot.core.binding.EntityBinder; +import com.diboot.core.binding.EntityListBinder; import com.diboot.core.binding.FieldBinder; import com.diboot.core.binding.annotation.BindEntity; import com.diboot.core.binding.annotation.BindEntityList; @@ -25,11 +28,13 @@ import java.util.HashMap; import java.util.List; /** - * 绑定管理器 + * 绑定管理器 (已废弃,请调用RelationsBinder) * @author Mazhicheng * @version v2.0 * @date 2019/3/30 + * @see com.diboot.core.binding.manager.RelationsBinder */ +@Deprecated public class AnnotationBindingManager { private static final Logger log = LoggerFactory.getLogger(AnnotationBindingManager.class); @@ -42,11 +47,7 @@ public class AnnotationBindingManager { * @return */ public static List autoConvertAndBind(List entityList, Class voClass){ - // 转换为VO列表 - List voList = BeanUtils.convertList(entityList, voClass); - // 自动绑定关联对象 - autoBind(voList); - return voList; + return RelationsBinder.convertAndBind(entityList, voClass); } /** @@ -55,172 +56,7 @@ public class AnnotationBindingManager { * @throws Exception */ public static void autoBind(List voList){ - if(V.isEmpty(voList)){ - return; - } - // 获取VO类 - Class voClass = voList.get(0).getClass(); - BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass); - if(bindAnnotationGroup.isNotEmpty()){ - // 绑定数据字典 - List dictAnnoList = bindAnnotationGroup.getBindDictAnnotations(); - if(dictAnnoList != null){ - for(FieldAnnotation annotation : dictAnnoList){ - doBindingDict(voList, annotation); - } - } - // 绑定Field字段名 - List fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations(); - if(fieldAnnoList != null){ - doBindingField(voList, fieldAnnoList); - } - // 绑定Entity实体 - List entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations(); - if(entityAnnoList != null){ - for(FieldAnnotation anno : entityAnnoList){ - doBindingEntity(voList, anno); - } - } - // 绑定Entity实体List - List entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations(); - if(entitiesAnnoList != null){ - for(FieldAnnotation anno : entitiesAnnoList){ - doBindingEntityList(voList, anno); - } - } - } - } - - /*** - * 绑定数据字典 - * @param voList - * @param fieldAnno - * @param - */ - private static void doBindingDict(List voList, FieldAnnotation fieldAnno) { - DictionaryService dictionaryService = (DictionaryService) ContextHelper.getBean(DictionaryService.class); - if(dictionaryService != null){ - BindDict annotation = (BindDict) fieldAnno.getAnnotation(); - dictionaryService.bindItemLabel(voList, fieldAnno.getFieldName(), annotation.field(), annotation.type()); - } - } - - /*** - * 绑定字段 - * @param voList - * @param fieldAnnoList - * @param - */ - private static void doBindingField(List voList, List fieldAnnoList) { - //多个字段,合并查询,以减少SQL数 - Map> clazzToListMap = new HashMap<>(); - for(FieldAnnotation anno : fieldAnnoList){ - BindField bindField = (BindField) anno.getAnnotation(); - String key = bindField.entity().getName() + ":" + bindField.condition(); - List list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>()); - list.add(anno); - } - // 解析条件并且执行绑定 - for(Map.Entry> entry : clazzToListMap.entrySet()){ - List list = entry.getValue(); - BindField bindAnnotation = (BindField) list.get(0).getAnnotation(); - BaseService service = getService(bindAnnotation); - FieldBinder binder = service.bindingFieldTo(voList); - for(FieldAnnotation anno : list){ - BindField bindField = (BindField) anno.getAnnotation(); - binder.link(bindField.field(), anno.getFieldName()); - } - parseConditionsAndBinding(binder, bindAnnotation.condition()); - } - } - - /*** - * 绑定Entity - * @param voList - * @param fieldAnnotation - * @param - */ - private static void doBindingEntity(List voList, FieldAnnotation fieldAnnotation) { - BindEntity annotation = (BindEntity) fieldAnnotation.getAnnotation(); - // 绑定关联对象entity - BaseService service = getService(annotation); - if(service != null){ - // 字段名 - String voFieldName = fieldAnnotation.getFieldName(); - // 构建binder - BaseBinder binder = service.bindingEntityTo(voList).set(voFieldName); - // 解析条件并且执行绑定 - parseConditionsAndBinding(binder, annotation.condition()); - } - } - - /*** - * 绑定Entity - * @param voList - * @param fieldAnnotation - * @param - */ - private static void doBindingEntityList(List voList, FieldAnnotation fieldAnnotation) { - BindEntityList bindAnnotation = (BindEntityList) fieldAnnotation.getAnnotation(); - // 绑定关联对象entity - BaseService service = getService(bindAnnotation); - if(service != null){ - // 字段名 - String voFieldName = fieldAnnotation.getFieldName(); - // 构建binder - BaseBinder binder = service.bindingEntityListTo(voList).set(voFieldName); - // 解析条件并且执行绑定 - parseConditionsAndBinding(binder, bindAnnotation.condition()); - } - } - - /*** - * 解析条件并且执行绑定 - * @param condition - * @param binder - */ - private static void parseConditionsAndBinding(BaseBinder binder, String condition){ - try{ - ConditionManager.parseConditions(condition, binder); - binder.bind(); - } - catch (Exception e){ - log.error("解析注解条件与绑定执行异常", e); - } - } - - /** - * 通过Entity获取对应的Service实现类 - * @param annotation - * @return - */ - private static BaseService getService(Annotation annotation){ - Class entityClass = null; - if(annotation instanceof BindDict){ - entityClass = Dictionary.class; - } - else if(annotation instanceof BindField){ - BindField bindAnnotation = (BindField)annotation; - entityClass = bindAnnotation.entity(); - } - else if(annotation instanceof BindEntity){ - BindEntity bindAnnotation = (BindEntity)annotation; - entityClass = bindAnnotation.entity(); - } - else if(annotation instanceof BindEntityList){ - BindEntityList bindAnnotation = (BindEntityList)annotation; - entityClass = bindAnnotation.entity(); - } - else{ - log.warn("非预期的注解: "+ annotation.getClass().getSimpleName()); - return null; - } - // 根据entity获取Service - BaseService service = ContextHelper.getServiceByEntity(entityClass); - if(service == null){ - log.error("未能识别到Entity: "+entityClass.getName()+" 的Service实现!"); - } - return service; + RelationsBinder.bind(voList); } } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java new file mode 100644 index 0000000..50793bc --- /dev/null +++ b/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java @@ -0,0 +1,264 @@ +package com.diboot.core.binding.manager; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.diboot.core.binding.BaseBinder; +import com.diboot.core.binding.EntityBinder; +import com.diboot.core.binding.EntityListBinder; +import com.diboot.core.binding.FieldBinder; +import com.diboot.core.binding.annotation.BindDict; +import com.diboot.core.binding.annotation.BindEntity; +import com.diboot.core.binding.annotation.BindEntityList; +import com.diboot.core.binding.annotation.BindField; +import com.diboot.core.binding.parser.BindAnnotationGroup; +import com.diboot.core.binding.parser.ConditionManager; +import com.diboot.core.binding.parser.FieldAnnotation; +import com.diboot.core.entity.Dictionary; +import com.diboot.core.service.DictionaryService; +import com.diboot.core.util.BeanUtils; +import com.diboot.core.util.ContextHelper; +import com.diboot.core.util.V; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 绑定管理器 + * @author Mazhicheng + * @version v2.0 + * @date 2019/7/18 + */ +public class RelationsBinder { + private static final Logger log = LoggerFactory.getLogger(RelationsBinder.class); + + /** + * 自动转换和绑定VO中的注解关联 + * @param entityList + * @param voClass + * @param + * @param + * @return + */ + public static List convertAndBind(List entityList, Class voClass){ + // 转换为VO列表 + List voList = BeanUtils.convertList(entityList, voClass); + // 自动绑定关联对象 + bind(voList); + return voList; + } + + /** + * 自动绑定关联对象 + * @return + * @throws Exception + */ + public static void bind(List voList){ + if(V.isEmpty(voList)){ + return; + } + // 获取VO类 + Class voClass = voList.get(0).getClass(); + BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass); + if(bindAnnotationGroup.isNotEmpty()){ + // 绑定数据字典 + List dictAnnoList = bindAnnotationGroup.getBindDictAnnotations(); + if(dictAnnoList != null){ + for(FieldAnnotation annotation : dictAnnoList){ + doBindingDict(voList, annotation); + } + } + // 绑定Field字段名 + List fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations(); + if(fieldAnnoList != null){ + doBindingField(voList, fieldAnnoList); + } + // 绑定Entity实体 + List entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations(); + if(entityAnnoList != null){ + for(FieldAnnotation anno : entityAnnoList){ + doBindingEntity(voList, anno); + } + } + // 绑定Entity实体List + List entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations(); + if(entitiesAnnoList != null){ + for(FieldAnnotation anno : entitiesAnnoList){ + doBindingEntityList(voList, anno); + } + } + } + } + + /*** + * 绑定数据字典 + * @param voList + * @param fieldAnno + * @param + */ + private static void doBindingDict(List voList, FieldAnnotation fieldAnno) { + DictionaryService dictionaryService = (DictionaryService) ContextHelper.getBean(DictionaryService.class); + if(dictionaryService != null){ + BindDict annotation = (BindDict) fieldAnno.getAnnotation(); + dictionaryService.bindItemLabel(voList, fieldAnno.getFieldName(), annotation.field(), annotation.type()); + } + } + + /*** + * 绑定字段 + * @param voList + * @param fieldAnnoList + * @param + */ + private static void doBindingField(List voList, List fieldAnnoList) { + //多个字段,合并查询,以减少SQL数 + Map> clazzToListMap = new HashMap<>(); + for(FieldAnnotation anno : fieldAnnoList){ + BindField bindField = (BindField) anno.getAnnotation(); + String key = bindField.entity().getName() + ":" + bindField.condition(); + List list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>()); + list.add(anno); + } + // 解析条件并且执行绑定 + for(Map.Entry> entry : clazzToListMap.entrySet()){ + List list = entry.getValue(); + BindField bindAnnotation = (BindField) list.get(0).getAnnotation(); + FieldBinder binder = buildFieldBinder(bindAnnotation, voList); + for(FieldAnnotation anno : list){ + BindField bindField = (BindField) anno.getAnnotation(); + binder.link(bindField.field(), anno.getFieldName()); + } + parseConditionsAndBinding(binder, bindAnnotation.condition()); + } + } + + /*** + * 绑定Entity + * @param voList + * @param fieldAnnotation + * @param + */ + private static void doBindingEntity(List voList, FieldAnnotation fieldAnnotation) { + BindEntity annotation = (BindEntity) fieldAnnotation.getAnnotation(); + // 绑定关联对象entity + EntityBinder binder = buildEntityBinder(annotation, voList); + if(binder != null){ + // 构建binder + binder.set(fieldAnnotation.getFieldName()); + // 解析条件并且执行绑定 + parseConditionsAndBinding(binder, annotation.condition()); + } + } + + /*** + * 绑定Entity + * @param voList + * @param fieldAnnotation + * @param + */ + private static void doBindingEntityList(List voList, FieldAnnotation fieldAnnotation) { + BindEntityList bindAnnotation = (BindEntityList) fieldAnnotation.getAnnotation(); + // 构建binder + EntityListBinder binder = buildEntityListBinder(bindAnnotation, voList); + if(binder != null){ + binder.set(fieldAnnotation.getFieldName()); + // 解析条件并且执行绑定 + parseConditionsAndBinding(binder, bindAnnotation.condition()); + } + } + + /*** + * 解析条件并且执行绑定 + * @param condition + * @param binder + */ + private static void parseConditionsAndBinding(BaseBinder binder, String condition){ + try{ + ConditionManager.parseConditions(condition, binder); + binder.bind(); + } + catch (Exception e){ + log.error("解析注解条件与绑定执行异常", e); + } + } + + /** + * 构建FieldBinder + * @param annotation + * @param voList + * @return + */ + private static FieldBinder buildFieldBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new FieldBinder<>(service, voList); + } + return null; + } + + /** + * 构建EntityBinder + * @param annotation + * @param voList + * @return + */ + private static EntityBinder buildEntityBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new EntityBinder<>(service, voList); + } + return null; + } + + /** + * 构建EntityListBinder + * @param annotation + * @param voList + * @return + */ + private static EntityListBinder buildEntityListBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new EntityListBinder<>(service, voList); + } + return null; + } + + /** + * 通过Entity获取对应的Service实现类 + * @param annotation + * @return + */ + private static IService getService(Annotation annotation){ + Class entityClass = null; + if(annotation instanceof BindDict){ + entityClass = Dictionary.class; + } + else if(annotation instanceof BindField){ + BindField bindAnnotation = (BindField)annotation; + entityClass = bindAnnotation.entity(); + } + else if(annotation instanceof BindEntity){ + BindEntity bindAnnotation = (BindEntity)annotation; + entityClass = bindAnnotation.entity(); + } + else if(annotation instanceof BindEntityList){ + BindEntityList bindAnnotation = (BindEntityList)annotation; + entityClass = bindAnnotation.entity(); + } + else{ + log.warn("非预期的注解: "+ annotation.getClass().getSimpleName()); + return null; + } + // 根据entity获取Service + IService service = ContextHelper.getServiceByEntity(entityClass); + if(service == null){ + log.error("未能识别到Entity: "+entityClass.getName()+" 的Service实现!"); + } + return service; + } + +} diff --git a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java index 2c1dd87..d2fc7f8 100644 --- a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java +++ b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java @@ -1,11 +1,9 @@ package com.diboot.core.controller; import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.entity.BaseEntity; import com.diboot.core.service.BaseService; -import com.diboot.core.util.BeanUtils; -import com.diboot.core.util.V; import com.diboot.core.vo.JsonResult; import com.diboot.core.vo.Status; import com.diboot.core.vo.Pagination; @@ -202,7 +200,7 @@ public abstract class BaseCrudRestController extends BaseController { */ protected List convertToVoAndBindRelations(List entityList, Class voClass){ // 转换为VO - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass); + List voList = RelationsBinder.convertAndBind(entityList, voClass); return voList; } diff --git a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java index 914565c..d6f6279 100644 --- a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java +++ b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java @@ -32,7 +32,6 @@ import org.springframework.web.servlet.ModelAndView; */ @ControllerAdvice public class DefaultExceptionAdviceHandler { - private final static Logger log = LoggerFactory.getLogger(ExceptionHandler.class); @Autowired @@ -71,6 +70,5 @@ public class DefaultExceptionAdviceHandler { return new ModelAndView("redirect:" + errorUrl); } return new ModelAndView(); - } } 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 55a8c1f..4f50380 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 @@ -3,12 +3,13 @@ package com.diboot.core.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.binding.EntityBinder; import com.diboot.core.binding.EntityListBinder; import com.diboot.core.binding.FieldBinder; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.config.BaseConfig; import com.diboot.core.config.Cons; import com.diboot.core.mapper.BaseCrudMapper; @@ -247,7 +248,7 @@ public class BaseServiceImpl, T> extends ServiceImpl List enityList = new ArrayList<>(); enityList.add(entity); // 绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(enityList, voClass); + List voList = RelationsBinder.convertAndBind(enityList, voClass); return voList.get(0); } @@ -255,7 +256,7 @@ public class BaseServiceImpl, T> extends ServiceImpl public List getViewObjectList(Wrapper queryWrapper, Pagination pagination, Class voClass) { List entityList = getEntityList(queryWrapper, pagination); // 自动转换为VO并绑定关联对象 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass); + List voList = RelationsBinder.convertAndBind(entityList, voClass); return voList; } @@ -264,17 +265,26 @@ public class BaseServiceImpl, T> extends ServiceImpl * @param pagination * @return */ - protected IPage convertToIPage(Pagination pagination){ + protected Page convertToIPage(Pagination pagination){ if(pagination == null){ return null; } - IPage page = new Page() + Page page = new Page() .setCurrent(pagination.getPageIndex()) .setSize(pagination.getPageSize()) // 如果前端传递过来了缓存的总数,则本次不再count统计 - .setTotal(pagination.getTotalCount() > 0? -1 : pagination.getTotalCount()) - .setAscs(S.toSnakeCase(pagination.getAscList())) - .setDescs(S.toSnakeCase(pagination.getDescList())); + .setTotal(pagination.getTotalCount() > 0? -1 : pagination.getTotalCount()); + // 排序 + if(V.notEmpty(pagination.getAscList())){ + pagination.getAscList().forEach(s -> { + page.addOrder(OrderItem.asc(s)); + }); + } + if(V.notEmpty(pagination.getDescList())){ + pagination.getDescList().forEach(s -> { + page.addOrder(OrderItem.desc(s)); + }); + } return page; } diff --git a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java index 89909fb..c1ce614 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java +++ b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java @@ -1,5 +1,7 @@ package com.diboot.core.util; +import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.BaseService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +37,7 @@ public class ContextHelper implements ApplicationContextAware { /** * Entity-对应的Mapper缓存 */ - private static Map entityToMapperCacheMap = new ConcurrentHashMap<>(); + private static Map entityToMapperCacheMap = new ConcurrentHashMap<>(); @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -106,11 +108,11 @@ public class ContextHelper implements ApplicationContextAware { * @param entity * @return */ - public static BaseService getServiceByEntity(Class entity){ + public static IService getServiceByEntity(Class entity){ if(entityToMapperCacheMap.isEmpty()){ - Map serviceMap = getApplicationContext().getBeansOfType(BaseService.class); + Map serviceMap = getApplicationContext().getBeansOfType(IService.class); if(V.notEmpty(serviceMap)){ - for(Map.Entry entry : serviceMap.entrySet()){ + for(Map.Entry entry : serviceMap.entrySet()){ String entityClassName = getEntityClassByServiceImpl(entry.getValue().getClass()); if(V.notEmpty(entityClassName)){ entityToMapperCacheMap.put(entityClassName, entry.getValue()); diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java index 954702d..c62d398 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java @@ -1,14 +1,13 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; import diboot.core.test.StartupApplication; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.service.UserService; import diboot.core.test.binder.vo.EntityBinderVO; -import diboot.core.test.binder.vo.FieldBinderVO; import diboot.core.test.config.SpringMvcConfig; import org.junit.Assert; import org.junit.Test; @@ -39,9 +38,9 @@ public class TestEntityBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, EntityBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java index c9e2475..75b42e6 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java @@ -1,8 +1,7 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; import diboot.core.test.StartupApplication; @@ -51,7 +50,7 @@ public class TestEntityListBinder { queryWrapper.eq(Department::getId, 10001L); List entityList = departmentService.getEntityList(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, EntityListSimpleBinderVO.class); + List voList = RelationsBinder.convertAndBind(entityList, EntityListSimpleBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityListSimpleBinderVO vo : voList){ @@ -76,9 +75,9 @@ public class TestEntityListBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityListComplexBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, EntityListComplexBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityListComplexBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java index 584617e..33f38c1 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java @@ -1,10 +1,9 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; -import com.diboot.core.vo.Pagination; import diboot.core.test.StartupApplication; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.service.UserService; @@ -39,9 +38,9 @@ public class TestFieldBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, FieldBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, FieldBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(FieldBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java b/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java index 5aca866..bd471b3 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java @@ -1,5 +1,6 @@ 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.Role; import diboot.core.test.binder.entity.User; @@ -10,6 +11,6 @@ import diboot.core.test.binder.entity.User; * @version v2.0 * @date 2019/1/5 */ -public interface RoleService extends BaseService { +public interface RoleService extends IService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java b/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java index 8ccb7b3..48045c5 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java @@ -1,5 +1,6 @@ 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.User; @@ -9,6 +10,6 @@ import diboot.core.test.binder.entity.User; * @version v2.0 * @date 2019/1/5 */ -public interface UserService extends BaseService { +public interface UserService extends IService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java index 3ec8977..573708d 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service.impl; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.impl.BaseServiceImpl; import diboot.core.test.binder.entity.Role; import diboot.core.test.binder.mapper.RoleMapper; @@ -13,6 +14,6 @@ import org.springframework.stereotype.Service; * Copyright © www.dibo.ltd */ @Service -public class RoleServiceImpl extends BaseServiceImpl implements RoleService { +public class RoleServiceImpl extends ServiceImpl implements RoleService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java index 78d924e..1f31686 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service.impl; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.impl.BaseServiceImpl; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.mapper.UserMapper; @@ -13,6 +14,6 @@ import org.springframework.stereotype.Service; * Copyright © www.dibo.ltd */ @Service -public class UserServiceImpl extends BaseServiceImpl implements UserService { +public class UserServiceImpl extends ServiceImpl implements UserService { } diff --git a/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java b/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java index f6e9cec..d9cce1e 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java @@ -1,7 +1,7 @@ package com.diboot.example.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.entity.Dictionary; import com.diboot.core.service.BaseService; @@ -47,7 +47,7 @@ public class DictionaryController extends BaseCrudRestController { //获取实体list List dictionaryList = dictionaryService.getEntityList(queryWrapper, pagination); //筛选出在列表页展示的字段 - List dicVoList = AnnotationBindingManager.autoConvertAndBind(dictionaryList, DictionaryListVO.class); + List dicVoList = RelationsBinder.convertAndBind(dictionaryList, DictionaryListVO.class); //返回结果 return new JsonResult(Status.OK, dicVoList).bindPagination(pagination); } diff --git a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index 8a22f29..df934fc 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java @@ -2,7 +2,7 @@ package com.diboot.example.controller; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.service.BaseService; import com.diboot.core.service.DictionaryService; @@ -57,7 +57,7 @@ public class SysUserController extends BaseCrudRestController { // 查询当前页的Entity主表数据 List voList = sysUserService.getSysUserList(queryWrapper, pagination); //筛选出在列表页展示的字段 - List userVoList = AnnotationBindingManager.autoConvertAndBind(voList, SysUserListVO.class); + List userVoList = RelationsBinder.convertAndBind(voList, SysUserListVO.class); // 返回结果 return new JsonResult(Status.OK, userVoList).bindPagination(pagination); } diff --git a/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java b/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java index 511f386..572f9a0 100644 --- a/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java +++ b/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java @@ -1,7 +1,6 @@ package com.diboot.example.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.entity.Dictionary; import com.diboot.core.mapper.DictionaryMapper; import com.diboot.core.service.impl.BaseServiceImpl; @@ -15,7 +14,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; /** diff --git a/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java b/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java index 280cbc1..ae628c1 100644 --- a/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java +++ b/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java @@ -2,7 +2,7 @@ package com.diboot.example.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.util.V; import com.diboot.core.vo.Pagination; @@ -40,7 +40,7 @@ public class SysUserServiceImpl extends BaseServiceImpl @Override public List getSysUserList(Wrapper queryWrapper, Pagination pagination) { List sysUserList = super.getEntityList(queryWrapper, pagination); - List sysUserVOList = AnnotationBindingManager.autoConvertAndBind(sysUserList, SysUserVO.class); + List sysUserVOList = RelationsBinder.convertAndBind(sysUserList, SysUserVO.class); if(V.notEmpty(sysUserVOList)){ for(SysUserVO sysUserVO : sysUserVOList){ List roleList = sysUserVO.getRoleList(); diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java index c3c6200..3f64952 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java @@ -2,7 +2,7 @@ package com.diboot.shiro.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.V; @@ -49,7 +49,7 @@ public class RoleServiceImpl extends BaseServiceImpl implement @Override public List getRoleList(Wrapper queryWrapper, Pagination pagination) { List roleList = super.getEntityList(queryWrapper, pagination); - List roleVOList = AnnotationBindingManager.autoConvertAndBind(roleList, RoleVO.class); + List roleVOList = RelationsBinder.convertAndBind(roleList, RoleVO.class); if(V.notEmpty(roleVOList)){ for(RoleVO roleVO : roleVOList){ List permissionList = roleVO.getPermissionList();