課程
/后端開發(fā)
/Java
/MyBatis-Plus入門
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
想問下這個方法怎么使用,搞不懂
2019-12-25
源自:MyBatis-Plus入門
正在回答
? ? ? ?先簡單解釋一下。getObj方法內(nèi)部又調(diào)用了listObjs。listObjs返回一個list,如果有元素的話,只取第一個,沒有元素的話,返回null。第二個參數(shù)是一個Function函數(shù)式接口。就是你不想返回實體了,想返回一個自定義類型對象或其他類型對象,你要進(jìn)行轉(zhuǎn)換。
看源碼,你就明白啦,給你show一下源碼
ServiceImpl類 @Override public?<V>?V?getObj(Wrapper<T>?queryWrapper,?Function<??super?Object,?V>?mapper)?{ ????return?SqlHelper.getObject(log,?listObjs(queryWrapper,?mapper)); }
SqlHelper類 /** ?*?從list中取第一條數(shù)據(jù)返回對應(yīng)List中泛型的單個結(jié)果 ?* ?*?@param?list?ignore ?*?@param?<E>??ignore ?*?@return?ignore ?*/ public?static?<E>?E?getObject(Log?log,?List<E>?list)?{ ????if?(CollectionUtils.isNotEmpty(list))?{ ????????int?size?=?list.size(); ????????if?(size?>?1)?{ ????????????log.warn(String.format("Warn:?execute?Method?There?are??%s?results.",?size)); ????????} ????????return?list.get(0); ????} ????return?null; }
ServiceImpl類 @Override public?<V>?List<V>?listObjs(Wrapper<T>?queryWrapper,?Function<??super?Object,?V>?mapper)?{ ????return?baseMapper.selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList()); }
@Test public?void?getOneTest04()?{ ????queryWrapper.ge("emp_age",?18); ????boolean?b?=?employeeService.getObj(queryWrapper,?(m)?->?{ ????????Employee?employee?=?employeeService.getById((Long)m); ????????employee.setEmpPhone("-----"); ????????boolean?flag?=?employeeService.updateById(employee); ????????return?flag; ????}); ????System.out.println(b); }
這個大概懂了,就是不知道為什么那個 m 參數(shù)是 id集合中的當(dāng)前id,而不是一個 Employee集合。
然后就是傳入 wrapper 條件構(gòu)造器之后,為什么就直接會有一個 id集合返回---? 哈哈哈 我還是個學(xué)生,問題就是多哦,,老師?
老猿
想好好學(xué)習(xí)的懶人 提問者
amllng 回復(fù) 老猿
a12345531
能舉個栗子不..
就是后面的那個函數(shù)式接口不會傳參?對8的特性理解不夠
舉報
MyBatis-Plus框架入門必學(xué)課程!
1 回答如何優(yōu)雅的使用lambdaQuery實現(xiàn)allEq方法呢?
2 回答老師 使用LambdaQueryWrapper進(jìn)行查詢的時候如何不使用方法引用而用lambda表達(dá)式去查詢呀
2 回答 主鍵id 不能使用 第2中方式 嗎 ?
1 回答mp中的枚舉類怎么使用
2 回答2.1.9的mybatis-plus-boot-starter,無法使用QueryWrapper
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2019-12-26
? ? ? ?先簡單解釋一下。getObj方法內(nèi)部又調(diào)用了listObjs。listObjs返回一個list,如果有元素的話,只取第一個,沒有元素的話,返回null。第二個參數(shù)是一個Function函數(shù)式接口。就是你不想返回實體了,想返回一個自定義類型對象或其他類型對象,你要進(jìn)行轉(zhuǎn)換。
看源碼,你就明白啦,給你show一下源碼
2019-12-26
這個大概懂了,就是不知道為什么那個 m 參數(shù)是 id集合中的當(dāng)前id,而不是一個 Employee集合。
然后就是傳入 wrapper 條件構(gòu)造器之后,為什么就直接會有一個 id集合返回---? 哈哈哈 我還是個學(xué)生,問題就是多哦,,老師?
2019-12-26
能舉個栗子不..
2019-12-26
就是后面的那個函數(shù)式接口不會傳參?對8的特性理解不夠