您好,登錄后才能下訂單哦!
今天小編給大家分享一下Mybatis Plus的lambda表達(dá)式查詢異常的處理方法的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
在使用新版Mybatis Plus工具時,新增的查詢有支持lambda表達(dá)式。
在使用的時候一定要注意,設(shè)計的字段名是否標(biāo)準(zhǔn)。不允許字段名出現(xiàn)以 is get 為開頭,負(fù)責(zé)mybatis plus 在編譯lambda表達(dá)式會出錯
java 8中引入了lambda表達(dá)式,lambda表達(dá)式可以讓我們的代碼更加簡介,業(yè)務(wù)邏輯更加清晰,但是在lambda表達(dá)式中使用的Functional Interface并沒有很好的處理異常,因?yàn)镴DK提供的這些Functional Interface通常都是沒有拋出異常的,這意味著需要我們自己手動來處理異常。
因?yàn)楫惓7譃閁nchecked Exception和checked Exception,我們分別來討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException,出現(xiàn)RuntimeException通常是因?yàn)槲覀兊拇a有問題。RuntimeException是不需要被捕獲的。也就是說如果有RuntimeException,沒有捕獲也可以通過編譯。
List<Integer> integers = Arrays.asList(1,2,3,4,5); integers.forEach(i -> System.out.println(1 / i));
這個例子是可以編譯成功的,但是上面有一個問題,如果list中有一個0的話,就會拋出ArithmeticException。
雖然這個是一個Unchecked Exception,但是我們還是想處理一下:
integers.forEach(i -> { try { System.out.println(1 / i); } catch (ArithmeticException e) { System.err.println( "Arithmetic Exception occured : " + e.getMessage()); } });
上面的例子我們使用了try,catch來處理異常,簡單但是破壞了lambda表達(dá)式的最佳實(shí)踐。代碼變得臃腫。
我們將try,catch移到一個wrapper方法中:
static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) { return i -> { try { consumer.accept(i); } catch (ArithmeticException e) { System.err.println( "Arithmetic Exception occured : " + e.getMessage()); } }; }
則原來的調(diào)用變成這樣:
integers.forEach(lambdaWrapper(i -> System.out.println(1 / i)));
但是上面的wrapper固定了捕獲ArithmeticException,我們再將其改編成一個更通用的類:
static <T, E extends Exception> Consumer<T> consumerWrapperWithExceptionClass(Consumer<T> consumer, Class<E> clazz) { return i -> { try { consumer.accept(i); } catch (Exception ex) { try { E exCast = clazz.cast(ex); System.err.println( "Exception occured : " + exCast.getMessage()); } catch (ClassCastException ccEx) { throw ex; } } }; }
上面的類傳入一個class,并將其cast到異常,如果能cast,則處理,否則拋出異常。
這樣處理之后,我們這樣調(diào)用:
ntegers.forEach( consumerWrapperWithExceptionClass( i -> System.out.println(1 / i), ArithmeticException.class));
處理checked Exception
checked Exception是必須要處理的異常,我們還是看個例子:
static void throwIOException(Integer integer) throws IOException { } List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5); integers.forEach(i -> throwIOException(i));
上面我們定義了一個方法拋出IOException,這是一個checked Exception,需要被處理,所以在下面的forEach中,程序會編譯失敗,因?yàn)闆]有處理相應(yīng)的異常。
最簡單的辦法就是try,catch住,如下所示:
ntegers.forEach(i -> { try { throwIOException(i); } catch (IOException e) { throw new RuntimeException(e); } });
當(dāng)然,這樣的做法的壞處我們在上面已經(jīng)講過了,同樣的,我們可以定義一個新的wrapper方法:
static <T> Consumer<T> consumerWrapper( ThrowingConsumer<T, Exception> throwingConsumer) { return i -> { try { throwingConsumer.accept(i); } catch (Exception ex) { throw new RuntimeException(ex); } }; }
我們這樣調(diào)用:
integers.forEach(consumerWrapper(i -> throwIOException(i)));
我們也可以封裝一下異常:
static <T, E extends Exception> Consumer<T> consumerWrapperWithExceptionClass( ThrowingConsumer<T, E> throwingConsumer, Class<E> exceptionClass) { return i -> { try { throwingConsumer.accept(i); } catch (Exception ex) { try { E exCast = exceptionClass.cast(ex); System.err.println( "Exception occured : " + exCast.getMessage()); } catch (ClassCastException ccEx) { throw new RuntimeException(ex); } } }; }
然后這樣調(diào)用:
integers.forEach(consumerWrapperWithExceptionClass( i -> throwIOException(i), IOException.class));
以上就是“Mybatis Plus的lambda表達(dá)式查詢異常的處理方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。