数据字典子项插入由批量改为逐条兼容多数据库

This commit is contained in:
mazhicheng 2019-11-27 18:47:24 +08:00
parent cb0265f95f
commit 2feaaae6b0
6 changed files with 44 additions and 38 deletions

View File

@ -1,7 +1,8 @@
package com.diboot.core.handle;
package com.diboot.core.handler;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -44,7 +45,7 @@ public class DefaultExceptionHandler {
}
if (br != null && br.hasErrors()) {
map.put("code", Status.FAIL_VALIDATION.code());
String validateErrorMsg = getBindingError(br);
String validateErrorMsg = V.getBindingError(br);
map.put("msg", validateErrorMsg);
log.warn("数据校验失败, {}: {}", br.getObjectName(), validateErrorMsg);
}
@ -124,21 +125,4 @@ public class DefaultExceptionHandler {
}
}
/**
* 解析所有的验证错误信息转换为JSON
* @param result
* @return
*/
protected String getBindingError(BindingResult result){
if(result == null || !result.hasErrors()){
return null;
}
List<ObjectError> errors = result.getAllErrors();
List<String> allErrors = new ArrayList<>(errors.size());
for(ObjectError error : errors){
allErrors.add(error.getDefaultMessage().replaceAll("\"", "'"));
}
return S.join(allErrors);
}
}

View File

@ -3,6 +3,7 @@ package com.diboot.core.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.entity.Dictionary;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.mapper.DictionaryMapper;
import com.diboot.core.service.DictionaryService;
import com.diboot.core.util.BeanUtils;
@ -11,6 +12,7 @@ import com.diboot.core.util.ISetter;
import com.diboot.core.util.V;
import com.diboot.core.vo.DictionaryVO;
import com.diboot.core.vo.KeyValue;
import com.diboot.core.vo.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
@ -74,32 +76,33 @@ public class DictionaryServiceImpl extends BaseServiceImpl<DictionaryMapper, Dic
}
@Override
@Transactional
@Transactional(rollbackFor = Exception.class)
public boolean addDictTree(DictionaryVO dictVO) {
//将DictionaryVO转化为Dictionary
Dictionary dictionary = new Dictionary();
dictionary = (Dictionary) BeanUtils.copyProperties(dictVO, dictionary);
if(!super.createEntity(dictionary)){
log.warn("新建数据字典失败type="+dictVO.getType());
log.warn("新建数据字典定义失败type="+dictVO.getType());
return false;
}
List<Dictionary> children = dictVO.getChildren();
if(V.notEmpty(children)){
try {
for(Dictionary dict : children){
dict.setParentId(dictionary.getId());
dict.setType(dictionary.getType());
boolean success = true;
for(Dictionary dict : children){
dict.setParentId(dictionary.getId());
dict.setType(dictionary.getType());
boolean insertOK = super.createEntity(dict);
if (!insertOK){
log.warn("dictionary插入数据字典失败请检查");
success = false;
}
if(!super.createEntities(children)){
log.warn("新建子数据字典失败type="+dictVO.getType());
throw new RuntimeException();
}
} catch (Exception e) {
log.warn("新建子数据字典失败type="+dictVO.getType());
throw new RuntimeException();
}
if(!success){
String errorMsg = "新建数据字典子项失败type="+dictVO.getType();
log.warn(errorMsg);
throw new BusinessException(Status.FAIL_OPERATION, errorMsg);
}
}
return true;
}
}

View File

@ -71,11 +71,11 @@ public class ContextHelper implements ApplicationContextAware {
/***
* 获取指定类型的单个Bean实例
* @param type
* @param clazz
* @return
*/
public static Object getBean(Class type){
return getApplicationContext().getBean(type);
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
/***

View File

@ -2,6 +2,8 @@ package com.diboot.core.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import java.sql.Timestamp;
import java.util.*;
@ -385,4 +387,21 @@ public class V {
}
}
/**
* 解析所有的验证错误信息转换为JSON
* @param result
* @return
*/
public static String getBindingError(BindingResult result){
if(result == null || !result.hasErrors()){
return null;
}
List<ObjectError> errors = result.getAllErrors();
List<String> allErrors = new ArrayList<>(errors.size());
for(ObjectError error : errors){
allErrors.add(error.getDefaultMessage().replaceAll("\"", "'"));
}
return S.join(allErrors);
}
}

View File

@ -1,6 +1,6 @@
package com.diboot.example.controller;
import com.diboot.core.handle.DefaultExceptionHandler;
import com.diboot.core.handler.DefaultExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;

View File

@ -1,6 +1,6 @@
package com.diboot.example.handle;
import com.diboot.core.handle.DefaultExceptionHandler;
import com.diboot.core.handler.DefaultExceptionHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
/**