Skip to content

Commit

Permalink
* 优化getPrimaryKeyVal支持VO,DTO类获取主键值
Browse files Browse the repository at this point in the history
  • Loading branch information
Scorrt-2021 committed Dec 15, 2021
1 parent 5d57484 commit b475fad
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.binding.RelationsBinder;
import com.diboot.core.binding.cache.BindingCacheManager;
import com.diboot.core.binding.parser.EntityInfoCache;
import com.diboot.core.binding.parser.ParserCache;
Expand Down Expand Up @@ -431,6 +432,15 @@ public void testCache(){

Class<?> entityClass = BindingCacheManager.getEntityClassBySimpleName("Dictionary");
Assert.assertTrue(entityClass != null && entityClass.getName().equals(Dictionary.class.getName()));

// 测试PropInfo缓存
QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", "GENDER").eq("item_value", "F");
Dictionary dictionary = dictionaryService.getSingleEntity(queryWrapper);

DictionaryVO dictionaryVO = RelationsBinder.convertAndBind(dictionary, DictionaryVO.class);
Assert.assertTrue(dictionaryVO.getPrimaryKeyVal().equals(dictionary.getId()));
Assert.assertTrue(ContextHelper.getIdFieldName(dictionaryVO.getClass()).equals("id"));
}

@Test
Expand Down Expand Up @@ -482,4 +492,14 @@ public void testDictExtdata(){
Assert.assertTrue(dictionary.getFromExt("createByName").equals("张三"));
}

@Test
public void testGetEntityList(){
QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id", "item_name", "item_value")
.eq("id", -1L);
List<Dictionary> dictionaries = dictionaryService.getEntityList(queryWrapper);
Assert.assertTrue(dictionaries != null);
Assert.assertTrue(dictionaries.size() == 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ public void setService(IService iService){
* @return
*/
public String getFieldByColumn(String columnName){
if(this.propInfo == null || V.isEmpty(this.propInfo.getColumnToFieldMap())){
if(this.propInfo == null){
return null;
}
return this.propInfo.getColumnToFieldMap().get(columnName);
return this.propInfo.getFieldByColumn(columnName);
}

/**
* 根据列名获取字段
* @return
*/
public String getColumnByField(String fieldName){
if(this.propInfo == null || V.isEmpty(this.propInfo.getFieldToColumnMap())){
if(this.propInfo == null){
return null;
}
return this.propInfo.getFieldToColumnMap().get(fieldName);
return this.propInfo.getColumnByField(fieldName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,26 @@ else if(columnName == null){

}

/**
* 根据列名获取字段
* @return
*/
public String getFieldByColumn(String columnName){
if(V.isEmpty(this.columnToFieldMap)){
return null;
}
return this.columnToFieldMap.get(columnName);
}

/**
* 根据列名获取字段
* @return
*/
public String getColumnByField(String fieldName){
if(V.isEmpty(this.fieldToColumnMap)){
return null;
}
return this.fieldToColumnMap.get(fieldName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.diboot.core.binding.Binder;
import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.binding.cache.BindingCacheManager;
import com.diboot.core.binding.parser.EntityInfoCache;
import com.diboot.core.binding.parser.PropInfo;
import com.diboot.core.config.Cons;
import com.diboot.core.dto.AttachMoreDTO;
import com.diboot.core.dto.CascaderDTO;
Expand All @@ -32,15 +32,12 @@
import com.diboot.core.util.ContextHelper;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.LabelValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -282,10 +279,10 @@ protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMoreDTO) {
log.error("未找到实体类型{} 对应的Service定义", attachMoreDTO.getTarget());
return null;
}
EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entityClass);
PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entityClass);
BiFunction<String, String, String> columnByField = (field, defValue) -> {
if (V.notEmpty(field)) {
String column = entityInfoCache.getColumnByField(field);
String column = propInfoCache.getColumnByField(field);
return V.notEmpty(column) ? column : field;
}
return defValue;
Expand All @@ -295,7 +292,7 @@ protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMoreDTO) {
log.error("实体类型:{} ,未指定label属性", attachMoreDTO.getTarget());
return null;
}
String value = columnByField.apply(attachMoreDTO.getValue(), entityInfoCache.getIdColumn());
String value = columnByField.apply(attachMoreDTO.getValue(), propInfoCache.getIdColumn());
String ext = columnByField.apply(attachMoreDTO.getExt(), null);
// 构建前端下拉框的初始化数据
QueryWrapper<?> queryWrapper = Wrappers.query()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public Map<String, Object> toMap(){
@JsonIgnore
public Object getPrimaryKeyVal(){
String pk = ContextHelper.getIdFieldName(this.getClass());
if(pk == null){
return null;
}
if(Cons.FieldName.id.name().equals(pk)){
return getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.diboot.core.binding.cache.BindingCacheManager;
import com.diboot.core.binding.parser.EntityInfoCache;
import com.diboot.core.binding.parser.ParserCache;
import com.diboot.core.binding.parser.PropInfo;
import com.diboot.core.service.BaseService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -188,18 +189,18 @@ public static BaseMapper getBaseMapperByEntity(Class entityClass){
* @return
*/
public static String getIdColumnName(Class entity){
EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity);
return entityInfoCache != null? entityInfoCache.getIdColumn() : null;
PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entity);
return propInfoCache != null? propInfoCache.getIdColumn() : null;
}

/**
* 获取Entity主键字段名
* @return
*/
public static String getIdFieldName(Class entity){
EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity);
if(entityInfoCache != null && entityInfoCache.getIdColumn() != null){
return entityInfoCache.getFieldByColumn(entityInfoCache.getIdColumn());
PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entity);
if(propInfoCache != null && propInfoCache.getIdColumn() != null){
return propInfoCache.getIdColumn();
}
return null;
}
Expand Down

0 comments on commit b475fad

Please sign in to comment.