java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫
場景
很多情況下,查單條記錄也用通用查詢接口,但是輸入的條件卻能確定唯一性。如果我們要確定list中只有一條記錄,如下寫法:
// 記錄不為空 && 只有一條 才繼續(xù)
if(!CollectionUtils.isEmpty(list) && 1!=list.size()){
return "記錄條數(shù)不是1";
}
Object object = list.get(0);
上面代碼對么,貌似正確啊。后來報錯了,被打臉了。
其實相當于 >0 && !=1 恰好漏掉了 =0 這種情況,
因此get(0)完美報錯。
解決方案
像這種條件不要怕麻煩,多寫幾個if更清晰。
補充:判斷一個java對象中的屬性值是否為空(批量判斷)
有時候數(shù)據(jù)庫中的某些字段值要求不為空,所以代碼中要判斷這些字段對應的屬性值是否為空,當對象屬性過多時,一個一個屬性去判斷,會顯得代碼冗余,所以,可以借助工具類
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class IsNull {
//整個類都校驗
public static List<String> validateProperty(Object validateObj) {
return validateProperty(validateObj,(String[])null);
}
//類中的某些字段不校驗
public static List<String> validateProperty(Object validateObj,String... ignoreProperties) {
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(validateObj.getClass());
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
List<String> errList = new ArrayList<>();
for (PropertyDescriptor targetPd : targetPds) {
Method readMethod = targetPd.getReadMethod();
if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(validateObj);
if (value instanceof String) {
if (StringUtils.isEmpty((String) value)) {
errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能為空");
continue;
}
}
if (value instanceof Float || value instanceof Integer) {
if (StringUtils.isEmpty(value.toString())) {
errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能為空");
continue;
}
}
if (value == null) {
errList.add(validateObj.getClass().getSimpleName() + "中的" + targetPd.getName() + "不能為空");
}
} catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
return errList;
}
}
之后對拿到的數(shù)據(jù)進行業(yè)務判斷
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
SpringBoot自定義加載yml實現(xiàn)方式,附源碼解讀
這篇文章主要介紹了SpringBoot自定義加載yml實現(xiàn)方式附源碼解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java自定義協(xié)議報文封裝 添加Crc32校驗的實例
下面小編就為大家分享一篇Java自定義協(xié)議報文封裝 添加Crc32校驗的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Java中ArrayList和LinkedList的區(qū)別
ArrayList和LinkedList在這個方法上存在一定的性能差異,本文就介紹了Java中ArrayList和LinkedList的區(qū)別,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Java實現(xiàn)上傳Excel文件并導入數(shù)據(jù)庫
這篇文章主要介紹了在java的基礎(chǔ)上學習上傳Excel文件并導出到數(shù)據(jù)庫,感興趣的小伙伴不要錯過奧2021-09-09
Java代理模式與動態(tài)代理之間的關(guān)系以及概念
代理模式是開發(fā)中常見的一種設(shè)計模式,使用代理模式可以很好的對程序進行橫向擴展。動態(tài)代理:代理類在程序運行時被創(chuàng)建的代理方式。關(guān)鍵在于動態(tài),程序具有了動態(tài)特性,可以在運行期間根據(jù)不同的目標對象生成動態(tài)代理對象2023-02-02
Java?IO流—異常及捕獲異常處理?try…catch…finally
這篇文章主要介紹了Java?IO流—異常及捕獲異常處理?try…catch…finally,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

