Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理
新版lambda 表達(dá)式查詢異常
在使用新版Mybatis Plus工具時(shí),新增的查詢有支持lambda表達(dá)式。
注意點(diǎn)
在使用的時(shí)候一定要注意,設(shè)計(jì)的字段名是否標(biāo)準(zhǔn)。不允許字段名出現(xiàn)以 is get 為開頭,負(fù)責(zé)mybatis plus 在編譯lambda表達(dá)式會(huì)出錯(cuò)
lambda表達(dá)式異常應(yīng)該如何處理
java 8中引入了lambda表達(dá)式,lambda表達(dá)式可以讓我們的代碼更加簡介,業(yè)務(wù)邏輯更加清晰,但是在lambda表達(dá)式中使用的Functional Interface并沒有很好的處理異常,因?yàn)镴DK提供的這些Functional Interface通常都是沒有拋出異常的,這意味著需要我們自己手動(dòng)來處理異常。
因?yàn)楫惓7譃閁nchecked Exception和checked Exception,我們分別來討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException,出現(xiàn)RuntimeException通常是因?yàn)槲覀兊拇a有問題。RuntimeException是不需要被捕獲的。也就是說如果有RuntimeException,沒有捕獲也可以通過編譯。
我們看一個(gè)例子
List<Integer> integers = Arrays.asList(1,2,3,4,5); ? ? ? ? integers.forEach(i -> System.out.println(1 / i));
這個(gè)例子是可以編譯成功的,但是上面有一個(gè)問題,如果list中有一個(gè)0的話,就會(huì)拋出ArithmeticException。
雖然這個(gè)是一個(gè)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移到一個(gè)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,我們?cè)賹⑵涓木幊梢粋€(gè)更通用的類:
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; ? ? ? ? ? ? } ? ? ? ? } ? ? }; }
上面的類傳入一個(gè)class,并將其cast到異常,如果能cast,則處理,否則拋出異常。
這樣處理之后,我們這樣調(diào)用:
ntegers.forEach( ? ? ? ? ? ? ? ? consumerWrapperWithExceptionClass( ? ? ? ? ? ? ? ? ? ? ? ? i -> System.out.println(1 / i), ? ? ? ? ? ? ? ? ? ? ? ? ArithmeticException.class));
處理checked Exception
checked Exception是必須要處理的異常,我們還是看個(gè)例子:
? ? static void throwIOException(Integer integer) throws IOException { ? ? } List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5); ? ? ? ? integers.forEach(i -> throwIOException(i));
上面我們定義了一個(gè)方法拋出IOException,這是一個(gè)checked Exception,需要被處理,所以在下面的forEach中,程序會(huì)編譯失敗,因?yàn)闆]有處理相應(yīng)的異常。
最簡單的辦法就是try,catch住,如下所示:
ntegers.forEach(i -> { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? throwIOException(i); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? throw new RuntimeException(e); ? ? ? ? ? ? } ? ? ? ? });
當(dāng)然,這樣的做法的壞處我們?cè)谏厦嬉呀?jīng)講過了,同樣的,我們可以定義一個(gè)新的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));
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來)
這篇文章主要介紹了Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09FastJson實(shí)現(xiàn)駝峰下劃線相互轉(zhuǎn)換方法詳解
這篇文章主要介紹了使用FastJson進(jìn)行駝峰下劃線相互轉(zhuǎn)換寫法及誤區(qū),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01idea數(shù)據(jù)庫驅(qū)動(dòng)下載失敗的問題及解決
這篇文章主要介紹了idea數(shù)據(jù)庫驅(qū)動(dòng)下載失敗的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java訪問Hadoop分布式文件系統(tǒng)HDFS的配置說明
Hadoop的能提供高吞吐量的數(shù)據(jù)訪問,是集群式服務(wù)器的上的數(shù)據(jù)操作利器,這里就來為大家分享Java訪問Hadoop分布式文件系統(tǒng)HDFS的配置說明:2016-06-06利用Java實(shí)現(xiàn)輕松解析DNS報(bào)文
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)輕松解析DNS報(bào)文,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-11-11java程序員自己的圖片轉(zhuǎn)文字OCR識(shí)圖工具分享
這篇文章主要介紹了java程序員自己的圖片轉(zhuǎn)文字OCR識(shí)圖工具,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序的詳細(xì)流程
這篇文章主要介紹了IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10