1. 注解关系绑定支持继承IService(非BaseService)的实体绑定。2. 绑定入口类更名为RelationsBinder

This commit is contained in:
mazhicheng 2019-07-18 12:49:13 +08:00
parent e7a11e67d6
commit a4ff66c49b
24 changed files with 391 additions and 230 deletions

View File

@ -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<Message> entityList = getService().getEntityList(queryWrapper, pagination);
// 自动转换VO中注解绑定的关联
List<MessageVO> voList = AnnotationBindingManager.autoConvertAndBind(entityList, MessageVO.class);
List<MessageVO> voList = RelationsBinder.convertAndBind(entityList, MessageVO.class);
//返回结果
return new JsonResult(Status.OK, voList).bindPagination(pagination);
}

View File

@ -48,14 +48,14 @@ private List<Role> roleList;
~~~java
// 调用AnnotationBindingManager自动绑定注解相关关联
//List<MyUserVO> voList = ...;
AnnotationBindingManager.autoBind(voList);
RelationsBinder.bind(voList);
~~~
#### 2. 自动转型并绑定关联(需要转型)
~~~java
// 获取Entity列表
List<User> entityList = userService.getEntityList(queryWrapper);
// 调用AnnotationBindingManager自动绑定注解相关关联
List<MyUserVO> voList = AnnotationBindingManager.autoConvertAndBind(userList, MyUserVO.class);
List<MyUserVO> voList = RelationsBinder.convertAndBind(userList, MyUserVO.class);
~~~

View File

@ -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<T> {
/**
* 被关联对象的Service实例
*/
protected BaseService<T> referencedService;
protected IService<T> referencedService;
/***
* DO对象中的主键属性名
*/
@ -141,4 +148,48 @@ public abstract class BaseBinder<T> {
*/
public abstract void bind();
/**
* 获取EntityList
* @param queryWrapper
* @return
*/
protected List<T> getEntityList(Wrapper queryWrapper) {
if(referencedService instanceof BaseService){
return ((BaseService)referencedService).getEntityList(queryWrapper, null);
}
else{
List<T> list = referencedService.list(queryWrapper);
return checkedList(list);
}
}
/**
* 获取Map结果
* @param queryWrapper
* @return
*/
protected List<Map<String, Object>> getMapList(Wrapper queryWrapper) {
if(referencedService instanceof BaseService){
return ((BaseService)referencedService).getMapList(queryWrapper);
}
else{
List<Map<String, Object>> 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;
}
}

View File

@ -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<T> extends BaseBinder<T> {
* @param referencedService
* @param voList
*/
public EntityBinder(BaseService<T> referencedService, List voList){
public EntityBinder(IService<T> referencedService, List voList){
this.referencedService = referencedService;
this.annoObjectList = voList;
this.queryWrapper = new QueryWrapper<T>();
@ -85,7 +86,7 @@ public class EntityBinder<T> extends BaseBinder<T> {
// 构建查询条件
queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), middleTableColumnValueList);
// 查询entity列表
List<T> list = referencedService.getEntityList(queryWrapper);
List<T> list = getEntityList(queryWrapper);
if(V.notEmpty(list)){
// 转换entity列表为Map<ID, Entity>
Map<String, T> listMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey));
@ -108,7 +109,7 @@ public class EntityBinder<T> extends BaseBinder<T> {
// 构建查询条件
queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList);
// 查询entity列表
List<T> list = referencedService.getEntityList(queryWrapper);
List<T> list = getEntityList(queryWrapper);
if(V.notEmpty(list)){
String refEntityPKFieldName = S.toLowerCaseCamel(referencedEntityPrimaryKey);
for(T entity : list){

View File

@ -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<T> extends EntityBinder<T> {
* @param serviceInstance
* @param voList
*/
public EntityListBinder(BaseService<T> serviceInstance, List voList){
public EntityListBinder(IService<T> serviceInstance, List voList){
this.referencedService = serviceInstance;
this.annoObjectList = voList;
this.queryWrapper = new QueryWrapper<T>();
@ -56,7 +57,7 @@ public class EntityListBinder<T> extends EntityBinder<T> {
// 构建查询条件
queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), entityIdList);
// 查询entity列表: List<Role>
List list = referencedService.getEntityList(queryWrapper);
List list = getEntityList(queryWrapper);
// 转换entity列表为Map<ID, Entity>
Map<String, T> entityMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey));
for(Map.Entry<String, List> entry : middleTableResultMap.entrySet()){
@ -80,7 +81,7 @@ public class EntityListBinder<T> extends EntityBinder<T> {
// 构建查询条件
queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList);
// 查询entity列表
List<T> list = referencedService.getEntityList(queryWrapper);
List<T> list = getEntityList(queryWrapper);
if(V.notEmpty(list)){
for(T entity : list){
String keyValue = BeanUtils.getStringProperty(entity, S.toLowerCaseCamel(referencedEntityPrimaryKey));

View File

@ -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<T> extends BaseBinder<T> {
* @param serviceInstance
* @param voList
*/
public FieldBinder(BaseService<T> serviceInstance, List voList){
public FieldBinder(IService<T> serviceInstance, List voList){
this.referencedService = serviceInstance;
this.annoObjectList = voList;
this.queryWrapper = new QueryWrapper<T>();
@ -113,7 +114,7 @@ public class FieldBinder<T> extends BaseBinder<T> {
}
// 获取匹配结果的mapList
List<Map<String, Object>> mapList = referencedService.getMapList(queryWrapper);
List<Map<String, Object>> mapList = getMapList(queryWrapper);
if(V.isEmpty(mapList)){
return;
}

View File

@ -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 <E, VO> List<VO> autoConvertAndBind(List<E> entityList, Class<VO> voClass){
// 转换为VO列表
List<VO> 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 <VO> void autoBind(List<VO> voList){
if(V.isEmpty(voList)){
return;
}
// 获取VO类
Class voClass = voList.get(0).getClass();
BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass);
if(bindAnnotationGroup.isNotEmpty()){
// 绑定数据字典
List<FieldAnnotation> dictAnnoList = bindAnnotationGroup.getBindDictAnnotations();
if(dictAnnoList != null){
for(FieldAnnotation annotation : dictAnnoList){
doBindingDict(voList, annotation);
}
}
// 绑定Field字段名
List<FieldAnnotation> fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations();
if(fieldAnnoList != null){
doBindingField(voList, fieldAnnoList);
}
// 绑定Entity实体
List<FieldAnnotation> entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations();
if(entityAnnoList != null){
for(FieldAnnotation anno : entityAnnoList){
doBindingEntity(voList, anno);
}
}
// 绑定Entity实体List
List<FieldAnnotation> entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations();
if(entitiesAnnoList != null){
for(FieldAnnotation anno : entitiesAnnoList){
doBindingEntityList(voList, anno);
}
}
}
}
/***
* 绑定数据字典
* @param voList
* @param fieldAnno
* @param <VO>
*/
private static <VO> void doBindingDict(List<VO> 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 <VO>
*/
private static <VO> void doBindingField(List<VO> voList, List<FieldAnnotation> fieldAnnoList) {
//多个字段合并查询以减少SQL数
Map<String, List<FieldAnnotation>> clazzToListMap = new HashMap<>();
for(FieldAnnotation anno : fieldAnnoList){
BindField bindField = (BindField) anno.getAnnotation();
String key = bindField.entity().getName() + ":" + bindField.condition();
List<FieldAnnotation> list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>());
list.add(anno);
}
// 解析条件并且执行绑定
for(Map.Entry<String, List<FieldAnnotation>> entry : clazzToListMap.entrySet()){
List<FieldAnnotation> 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 <VO>
*/
private static <VO> void doBindingEntity(List<VO> 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 <VO>
*/
private static <VO> void doBindingEntityList(List<VO> 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);
}
}

View File

@ -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 <E>
* @param <VO>
* @return
*/
public static <E, VO> List<VO> convertAndBind(List<E> entityList, Class<VO> voClass){
// 转换为VO列表
List<VO> voList = BeanUtils.convertList(entityList, voClass);
// 自动绑定关联对象
bind(voList);
return voList;
}
/**
* 自动绑定关联对象
* @return
* @throws Exception
*/
public static <VO> void bind(List<VO> voList){
if(V.isEmpty(voList)){
return;
}
// 获取VO类
Class voClass = voList.get(0).getClass();
BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass);
if(bindAnnotationGroup.isNotEmpty()){
// 绑定数据字典
List<FieldAnnotation> dictAnnoList = bindAnnotationGroup.getBindDictAnnotations();
if(dictAnnoList != null){
for(FieldAnnotation annotation : dictAnnoList){
doBindingDict(voList, annotation);
}
}
// 绑定Field字段名
List<FieldAnnotation> fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations();
if(fieldAnnoList != null){
doBindingField(voList, fieldAnnoList);
}
// 绑定Entity实体
List<FieldAnnotation> entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations();
if(entityAnnoList != null){
for(FieldAnnotation anno : entityAnnoList){
doBindingEntity(voList, anno);
}
}
// 绑定Entity实体List
List<FieldAnnotation> entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations();
if(entitiesAnnoList != null){
for(FieldAnnotation anno : entitiesAnnoList){
doBindingEntityList(voList, anno);
}
}
}
}
/***
* 绑定数据字典
* @param voList
* @param fieldAnno
* @param <VO>
*/
private static <VO> void doBindingDict(List<VO> 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 <VO>
*/
private static <VO> void doBindingField(List<VO> voList, List<FieldAnnotation> fieldAnnoList) {
//多个字段合并查询以减少SQL数
Map<String, List<FieldAnnotation>> clazzToListMap = new HashMap<>();
for(FieldAnnotation anno : fieldAnnoList){
BindField bindField = (BindField) anno.getAnnotation();
String key = bindField.entity().getName() + ":" + bindField.condition();
List<FieldAnnotation> list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>());
list.add(anno);
}
// 解析条件并且执行绑定
for(Map.Entry<String, List<FieldAnnotation>> entry : clazzToListMap.entrySet()){
List<FieldAnnotation> 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 <VO>
*/
private static <VO> void doBindingEntity(List<VO> 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 <VO>
*/
private static <VO> void doBindingEntityList(List<VO> 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;
}
}

View File

@ -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 <VO> List<VO> convertToVoAndBindRelations(List entityList, Class<VO> voClass){
// 转换为VO
List<VO> voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass);
List<VO> voList = RelationsBinder.convertAndBind(entityList, voClass);
return voList;
}

View File

@ -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();
}
}

View File

@ -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<M extends BaseCrudMapper<T>, T> extends ServiceImpl
List<T> enityList = new ArrayList<>();
enityList.add(entity);
// 绑定
List<VO> voList = AnnotationBindingManager.autoConvertAndBind(enityList, voClass);
List<VO> voList = RelationsBinder.convertAndBind(enityList, voClass);
return voList.get(0);
}
@ -255,7 +256,7 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
public <VO> List<VO> getViewObjectList(Wrapper queryWrapper, Pagination pagination, Class<VO> voClass) {
List<T> entityList = getEntityList(queryWrapper, pagination);
// 自动转换为VO并绑定关联对象
List<VO> voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass);
List<VO> voList = RelationsBinder.convertAndBind(entityList, voClass);
return voList;
}
@ -264,17 +265,26 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
* @param pagination
* @return
*/
protected IPage<T> convertToIPage(Pagination pagination){
protected Page<T> convertToIPage(Pagination pagination){
if(pagination == null){
return null;
}
IPage<T> page = new Page<T>()
Page<T> page = new Page<T>()
.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;
}

View File

@ -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<String, BaseService> entityToMapperCacheMap = new ConcurrentHashMap<>();
private static Map<String, IService> 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<String, BaseService> serviceMap = getApplicationContext().getBeansOfType(BaseService.class);
Map<String, IService> serviceMap = getApplicationContext().getBeansOfType(IService.class);
if(V.notEmpty(serviceMap)){
for(Map.Entry<String, BaseService> entry : serviceMap.entrySet()){
for(Map.Entry<String, IService> entry : serviceMap.entrySet()){
String entityClassName = getEntityClassByServiceImpl(entry.getValue().getClass());
if(V.notEmpty(entityClassName)){
entityToMapperCacheMap.put(entityClassName, entry.getValue());

View File

@ -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<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(User::getId, 1001L, 1002L);
List<User> userList = userService.getEntityList(queryWrapper);
List<User> userList = userService.list(queryWrapper);
// 自动绑定
List<EntityBinderVO> voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityBinderVO.class);
List<EntityBinderVO> voList = RelationsBinder.convertAndBind(userList, EntityBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityBinderVO vo : voList){

View File

@ -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<Department> entityList = departmentService.getEntityList(queryWrapper);
// 自动绑定
List<EntityListSimpleBinderVO> voList = AnnotationBindingManager.autoConvertAndBind(entityList, EntityListSimpleBinderVO.class);
List<EntityListSimpleBinderVO> voList = RelationsBinder.convertAndBind(entityList, EntityListSimpleBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityListSimpleBinderVO vo : voList){
@ -76,9 +75,9 @@ public class TestEntityListBinder {
// 加载测试数据
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(User::getId, 1001L, 1002L);
List<User> userList = userService.getEntityList(queryWrapper);
List<User> userList = userService.list(queryWrapper);
// 自动绑定
List<EntityListComplexBinderVO> voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityListComplexBinderVO.class);
List<EntityListComplexBinderVO> voList = RelationsBinder.convertAndBind(userList, EntityListComplexBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityListComplexBinderVO vo : voList){

View File

@ -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<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(User::getId, 1001L, 1002L);
List<User> userList = userService.getEntityList(queryWrapper);
List<User> userList = userService.list(queryWrapper);
// 自动绑定
List<FieldBinderVO> voList = AnnotationBindingManager.autoConvertAndBind(userList, FieldBinderVO.class);
List<FieldBinderVO> voList = RelationsBinder.convertAndBind(userList, FieldBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(FieldBinderVO vo : voList){

View File

@ -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<Role> {
public interface RoleService extends IService<Role> {
}

View File

@ -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<User> {
public interface UserService extends IService<User> {
}

View File

@ -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<RoleMapper, Role> implements RoleService {
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
}

View File

@ -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<UserMapper, User> implements UserService {
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

View File

@ -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<Dictionary> dictionaryList = dictionaryService.getEntityList(queryWrapper, pagination);
//筛选出在列表页展示的字段
List<DictionaryListVO> dicVoList = AnnotationBindingManager.autoConvertAndBind(dictionaryList, DictionaryListVO.class);
List<DictionaryListVO> dicVoList = RelationsBinder.convertAndBind(dictionaryList, DictionaryListVO.class);
//返回结果
return new JsonResult(Status.OK, dicVoList).bindPagination(pagination);
}

View File

@ -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<SysUserVO> voList = sysUserService.getSysUserList(queryWrapper, pagination);
//筛选出在列表页展示的字段
List<SysUserListVO> userVoList = AnnotationBindingManager.autoConvertAndBind(voList, SysUserListVO.class);
List<SysUserListVO> userVoList = RelationsBinder.convertAndBind(voList, SysUserListVO.class);
// 返回结果
return new JsonResult(Status.OK, userVoList).bindPagination(pagination);
}

View File

@ -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;
/**

View File

@ -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<SysUserMapper, SysUser>
@Override
public List<SysUserVO> getSysUserList(Wrapper queryWrapper, Pagination pagination) {
List<SysUser> sysUserList = super.getEntityList(queryWrapper, pagination);
List<SysUserVO> sysUserVOList = AnnotationBindingManager.autoConvertAndBind(sysUserList, SysUserVO.class);
List<SysUserVO> sysUserVOList = RelationsBinder.convertAndBind(sysUserList, SysUserVO.class);
if(V.notEmpty(sysUserVOList)){
for(SysUserVO sysUserVO : sysUserVOList){
List<Role> roleList = sysUserVO.getRoleList();

View File

@ -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<RoleMapper, Role> implement
@Override
public List<RoleVO> getRoleList(Wrapper queryWrapper, Pagination pagination) {
List<Role> roleList = super.getEntityList(queryWrapper, pagination);
List<RoleVO> roleVOList = AnnotationBindingManager.autoConvertAndBind(roleList, RoleVO.class);
List<RoleVO> roleVOList = RelationsBinder.convertAndBind(roleList, RoleVO.class);
if(V.notEmpty(roleVOList)){
for(RoleVO roleVO : roleVOList){
List<Permission> permissionList = roleVO.getPermissionList();