From 995465984c9382ab1d0213b7a50cce120660eb03 Mon Sep 17 00:00:00 2001 From: zer0Black Date: Thu, 27 Jan 2022 16:39:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jero/common/util/ValidUtil.java | 142 ------------------ .../controller/${entityName}Controller.javai | 14 +- 2 files changed, 1 insertion(+), 155 deletions(-) delete mode 100644 jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java deleted file mode 100644 index b8fae457..00000000 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java +++ /dev/null @@ -1,142 +0,0 @@ -package com.jero.common.util; - -import com.jero.common.exception.JeroBootException; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; - -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.Validator; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -/** - * 手动校验@Vaild方法 - * @author liJiaRao - * @date 2021-08-05 15:09 - */ -@Slf4j -public class ValidUtil { - - private static Validator validator; - - static { - validator = Validation.buildDefaultValidatorFactory() - .getValidator(); - } - - private static Validator validatorAll; - - static { - validatorAll = Validation.buildDefaultValidatorFactory() - .getValidator(); - } - - /** - * 手动校验@Vaild并抛出异常信息 - * - * @param object 待校验对象 - * @param groups 待校验的组 - */ - public static void validate(Object object, Class... groups){ - StringBuilder msg = new StringBuilder(); - Set> set = validator.validate(object, groups); - for (ConstraintViolation constraintViolation : set) { - msg.append(constraintViolation.getMessage()).append(","); - } - if (StringUtils.isNotBlank(msg)) { - throw new JeroBootException(msg.substring(0,msg.length()-1)); - } - } - - /** - * 手动校验@Vaild并抛出异常信息list - * - * @param object 待校验对象 - * @param groups 待校验的组 - * @return errorList - */ - public static List validateReturnList(Object object, Class... groups){ - List list = new ArrayList<>(); - Set> set = validator.validate(object, groups); - for (ConstraintViolation constraintViolation : set) { - list.add(constraintViolation.getMessage()); - } - return list; - } - - /** - * 手动校验@Vaild并抛出异常信息list - * - * @param object 待校验对象 - * @param groups 待校验的组 - * @return errorList - */ - public static List validateReturnListWithNum(Object object, int errorRowNum, Class... groups){ - List list = new ArrayList<>(); - Set> set = validator.validate(object, groups); - for (ConstraintViolation constraintViolation : set) { - list.add("第" + errorRowNum + "行:"+constraintViolation.getMessage()); - } - return list; - } - - /** - * @Description 校验全部数据格式 - * @Author lqt - * @Date 2021/8/26 - * @Param - * @Return - * @Exception - */ - public static StringBuilder validateAll(Object object){ - StringBuilder msg = new StringBuilder(); - Set> set = validatorAll.validate(object); - if(set != null && set.size() >0 ){ - for(ConstraintViolation constraintViolation : set){ - // 这里循环获取错误信息,可以自定义格式 - msg.append(constraintViolation.getMessage()).append("
"); - } - } - return msg; - } - /** - * @Description 校验excel 每行数据格式 - * @Author lqt - * @Date 2021/8/26 - * @Param count excel 行数 - * @Return - * @Exception - */ - public static StringBuilder validateExcel(Object object, int count){ - StringBuilder msg = new StringBuilder(); - Set> set = validatorAll.validate(object); - if(set != null && set.size() > 0 ){ - for(ConstraintViolation constraintViolation : set){ - // 这里循环获取错误信息,可以自定义格式 - msg.append("第").append(count).append("行:").append(constraintViolation.getMessage()).append(",请修改。
"); - } - } - return msg; - } - /** - * @Description 校验excel 每行数据格式(只用于支出合同导入校验) - * @Author lqt - * @Date 2021/8/26 - * @Param count excel 行数 - * @Return - * @Exception - */ - public static StringBuilder validateSpendingContractExcel(Object object, int count,int num){ - StringBuilder msg = new StringBuilder(); - Set> set = validatorAll.validate(object); - if(set != null && set.size() > 0 ){ - for(ConstraintViolation constraintViolation : set){ - // 这里循环获取错误信息,可以自定义格式 - msg.append("第").append(count).append("行:").append(num).append(constraintViolation.getMessage()).append(",请修改。
"); - } - } - return msg; - } -} diff --git a/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai b/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai index a6631f30..782e23f7 100644 --- a/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai +++ b/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai @@ -253,21 +253,9 @@ public class ${entityName}Controller { try { List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params); for (${entityName}Page page : list) { - int lineNumber = i + 1; ${entityName} po = new ${entityName}(); BeanUtils.copyProperties(page, po); - try { - List errorList = ValidUtil.validateReturnList(po); - if (!errorList.isEmpty()){ - errorLines++; - errorMessage.add("第 " + lineNumber + " 行:"+ StringUtils.join(errorList,",")); - } - ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,); - } catch (Exception e) { - // 通过索引名判断出错信息 - errorMessage.add("第 " + lineNumber + " 行:未知错误"); - log.error(e.getMessage(), e); - } + ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,); } return Result.OK("文件导入成功!数据行数:" + list.size()); } catch (Exception e) {