Mybatis?Plus?新版lambda?表達式查詢異常的處理
新版lambda 表達式查詢異常
在使用新版Mybatis Plus工具時,新增的查詢有支持lambda表達式。
注意點
在使用的時候一定要注意,設(shè)計的字段名是否標準。不允許字段名出現(xiàn)以 is get 為開頭,負責(zé)mybatis plus 在編譯lambda表達式會出錯
lambda表達式異常應(yīng)該如何處理
java 8中引入了lambda表達式,lambda表達式可以讓我們的代碼更加簡介,業(yè)務(wù)邏輯更加清晰,但是在lambda表達式中使用的Functional Interface并沒有很好的處理異常,因為JDK提供的這些Functional Interface通常都是沒有拋出異常的,這意味著需要我們自己手動來處理異常。
因為異常分為Unchecked Exception和checked Exception,我們分別來討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException,出現(xiàn)RuntimeException通常是因為我們的代碼有問題。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表達式的最佳實踐。代碼變得臃腫。
我們將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īng)的異常。
最簡單的辦法就是try,catch住,如下所示:
ntegers.forEach(i -> {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? throwIOException(i);
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? ? ? }
? ? ? ? });當然,這樣的做法的壞處我們在上面已經(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));
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來)
這篇文章主要介紹了Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
FastJson實現(xiàn)駝峰下劃線相互轉(zhuǎn)換方法詳解
這篇文章主要介紹了使用FastJson進行駝峰下劃線相互轉(zhuǎn)換寫法及誤區(qū),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
idea數(shù)據(jù)庫驅(qū)動下載失敗的問題及解決
這篇文章主要介紹了idea數(shù)據(jù)庫驅(qū)動下載失敗的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Java訪問Hadoop分布式文件系統(tǒng)HDFS的配置說明
Hadoop的能提供高吞吐量的數(shù)據(jù)訪問,是集群式服務(wù)器的上的數(shù)據(jù)操作利器,這里就來為大家分享Java訪問Hadoop分布式文件系統(tǒng)HDFS的配置說明:2016-06-06
java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具分享
這篇文章主要介紹了java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
IDEA遠程部署調(diào)試Java應(yīng)用程序的詳細流程
這篇文章主要介紹了IDEA遠程部署調(diào)試Java應(yīng)用程序,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2021-10-10

