修改关联绑定实现,支持ORACLE字段名大写

This commit is contained in:
mazhicheng 2019-11-04 11:53:38 +08:00
parent 5213c9cb50
commit a326ce2865
6 changed files with 59 additions and 37 deletions

View File

@ -176,6 +176,22 @@ public abstract class BaseBinder<T> {
}
}
/**
* 从map中取值如直接取为null尝试转换大写后再取以支持ORACLE等大写命名数据库
* @param map
* @param key
* @return
*/
protected Object getValueIgnoreKeyCase(Map<String, Object> map, String key){
if(map.containsKey(key)){
return map.get(key);
}
if(map.containsKey(key.toUpperCase())){
return map.get(key.toUpperCase());
}
return null;
}
/**
* 检查list结果过多打印warn
* @param list

View File

@ -118,9 +118,9 @@ public class FieldBinder<T> extends BaseBinder<T> {
// 将结果list转换成map
Map<String, Map<String, Object>> referencedEntityPk2DataMap = new HashMap<>(mapList.size());
for(Map<String, Object> map : mapList){
Object pkVal = map.get(referencedEntityPkName);
if(pkVal != null){
referencedEntityPk2DataMap.put(String.valueOf(pkVal), map);
Object valObj = getValueIgnoreKeyCase(map, referencedEntityPkName);
if(valObj != null){
referencedEntityPk2DataMap.put(S.valueOf(valObj), map);
}
}
// 遍历list并赋值
@ -135,7 +135,8 @@ public class FieldBinder<T> extends BaseBinder<T> {
Map<String, Object> relationMap = referencedEntityPk2DataMap.get(annoObjectId);
if(relationMap != null){
for(int i = 0; i< annoObjectSetterPropNameList.size(); i++){
BeanUtils.setProperty(annoObject, annoObjectSetterPropNameList.get(i), relationMap.get(referencedGetterColumnNameList.get(i)));
Object valObj = getValueIgnoreKeyCase(relationMap, referencedGetterColumnNameList.get(i));
BeanUtils.setProperty(annoObject, annoObjectSetterPropNameList.get(i), valObj);
}
}
}

View File

@ -119,6 +119,15 @@ public class S extends StringUtils{
if(V.isEmpty(camelCaseStr)){
return null;
}
// 全小写
if(camelCaseStr.toLowerCase().equals(camelCaseStr)){
return camelCaseStr;
}
// 全大写直接return小写
if(camelCaseStr.toUpperCase().equals(camelCaseStr)){
return camelCaseStr.toLowerCase();
}
// 大小写混合则遇大写转换为_小写
char[] chars = camelCaseStr.toCharArray();
StringBuilder sb = new StringBuilder();
for (char c : chars){

View File

@ -42,14 +42,13 @@ public class TestEntityBinder {
// 自动绑定
List<EntityBinderVO> voList = RelationsBinder.convertAndBind(userList, EntityBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityBinderVO vo : voList){
// 验证直接关联和通过中间表间接关联的绑定
Assert.assertEquals(vo.getDepartmentId(), vo.getDepartment().getId());
Assert.assertNotNull(vo.getDepartment().getOrgId());
Assert.assertNotNull(vo.getOrganization());
System.out.println(JSON.stringify(vo));
}
Assert.assertTrue(V.notEmpty(voList));
for(EntityBinderVO vo : voList){
// 验证直接关联和通过中间表间接关联的绑定
Assert.assertEquals(vo.getDepartmentId(), vo.getDepartment().getId());
Assert.assertNotNull(vo.getDepartment().getOrgId());
Assert.assertNotNull(vo.getOrganization());
System.out.println(JSON.stringify(vo));
}
}

View File

@ -51,16 +51,15 @@ public class TestEntityListBinder {
// 自动绑定
List<EntityListSimpleBinderVO> voList = RelationsBinder.convertAndBind(entityList, EntityListSimpleBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityListSimpleBinderVO vo : voList){
// 验证直接关联的绑定
Assert.assertTrue(V.notEmpty(vo.getChildren()));
System.out.println(JSON.stringify(vo));
Assert.assertTrue(V.notEmpty(voList));
for(EntityListSimpleBinderVO vo : voList){
// 验证直接关联的绑定
Assert.assertTrue(V.notEmpty(vo.getChildren()));
System.out.println(JSON.stringify(vo));
if(vo.getChildren() != null){
for(Department dept : vo.getChildren()){
System.out.println(dept.toString());
}
if(vo.getChildren() != null){
for(Department dept : vo.getChildren()){
System.out.println(dept.toString());
}
}
}
@ -78,12 +77,11 @@ public class TestEntityListBinder {
// 自动绑定
List<EntityListComplexBinderVO> voList = RelationsBinder.convertAndBind(userList, EntityListComplexBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(EntityListComplexBinderVO vo : voList){
// 验证通过中间表间接关联的绑定
Assert.assertTrue(V.notEmpty(vo.getRoleList()));
System.out.println(JSON.stringify(vo));
}
Assert.assertTrue(V.notEmpty(voList));
for(EntityListComplexBinderVO vo : voList){
// 验证通过中间表间接关联的绑定
Assert.assertTrue(V.notEmpty(vo.getRoleList()));
System.out.println(JSON.stringify(vo));
}
}

View File

@ -47,17 +47,16 @@ public class TestFieldBinder {
// 自动绑定
List<FieldBinderVO> voList = RelationsBinder.convertAndBind(userList, FieldBinderVO.class);
// 验证绑定结果
if(V.notEmpty(voList)){
for(FieldBinderVO vo : voList){
// 验证直接关联和通过中间表间接关联的绑定
Assert.assertNotNull(vo.getDeptName());
Assert.assertNotNull(vo.getOrgName());
Assert.assertNotNull(vo.getOrgTelphone());
// 验证枚举值已绑定
Assert.assertNotNull(vo.getGenderLabel());
Assert.assertTrue(V.notEmpty(voList));
for(FieldBinderVO vo : voList){
// 验证直接关联和通过中间表间接关联的绑定
Assert.assertNotNull(vo.getDeptName());
Assert.assertNotNull(vo.getOrgName());
Assert.assertNotNull(vo.getOrgTelphone());
// 验证枚举值已绑定
Assert.assertNotNull(vo.getGenderLabel());
System.out.println(JSON.stringify(vo));
}
System.out.println(JSON.stringify(vo));
}
}