MyBatisPlus利用Service實(shí)現(xiàn)獲取數(shù)據(jù)列表
1. 簡(jiǎn)單介紹
嗨,大家好,今天給想給大家分享一下關(guān)于Mybatis-plus 的 Service 層的一些方法的使用。今天沒(méi)有總結(jié),因?yàn)槎际且恍〢PI沒(méi)有什么可以總結(jié)的,直接看著調(diào)用就可以了。
下面介紹怎樣使用 IServer 提供的 list 方法查詢多條數(shù)據(jù),這些方法將根據(jù)查詢條件獲取多條數(shù)據(jù)。
2. 接口說(shuō)明
接口提供了如下十個(gè) list 方法:
// 查詢所有 List<T> list(); // 查詢列表 List<T> list(Wrapper<T> queryWrapper); // 查詢(根據(jù)ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(根據(jù) columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 查詢所有列表 List<Map<String, Object>> listMaps(); // 查詢列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查詢?nèi)坑涗? List<Object> listObjs(); // 查詢?nèi)坑涗? <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗? List<Object> listObjs(Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗? <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
3. 參數(shù)說(shuō)明
queryWrapper:實(shí)體對(duì)象封裝操作類 QueryWrapper
idList:主鍵ID列表
columnMap:表字段 map 對(duì)象
mapper:轉(zhuǎn)換函數(shù)
4. 實(shí)例代碼
4.1 不帶任何參數(shù)的 list() 方法查詢數(shù)據(jù)
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class List1Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
List<UserBean> userBeanList = userService.list();
System.out.println("size=" + userBeanList.size());
}
}
4.2 查詢用戶ID大于 10,小于 20 且性別為“男”的用戶列表
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class List2Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.gt("user_id", 10);
wrapper.lt("user_id", 20);
wrapper.eq("sex", "男");
List<UserBean> userBeanList = userService.list(wrapper);
for(UserBean userBean : userBeanList) {
System.out.println(userBean);
}
}
}
4.3 注意事項(xiàng)說(shuō)明
請(qǐng)注意,這里我們所描述的一切方法都是基于 Service 層來(lái)說(shuō)的
請(qǐng)注意,這里我們所描述的一切方法都是不是基于 Mapper 層來(lái)說(shuō)的
到此這篇關(guān)于MyBatisPlus利用Service實(shí)現(xiàn)獲取數(shù)據(jù)列表的文章就介紹到這了,更多相關(guān)MyBatisPlus Service獲取數(shù)據(jù)列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
spring?boot項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的連接
在我們?nèi)粘5拈_(kāi)發(fā)過(guò)程中,肯定不可避免的會(huì)使用到數(shù)據(jù)庫(kù)以及SQL?語(yǔ)句,下面這篇文章主要給大家介紹了關(guān)于spring?boot項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)與數(shù)據(jù)庫(kù)連接的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
關(guān)于spring data jpa一級(jí)緩存的問(wèn)題
這篇文章主要介紹了關(guān)于spring data jpa一級(jí)緩存的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
學(xué)習(xí)spring事務(wù)與消息隊(duì)列
這篇文章主要為大家詳細(xì)介紹了spring事務(wù)與消息隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Java中的時(shí)間日期API知識(shí)點(diǎn)總結(jié)
本文給大家總結(jié)了Java中的時(shí)間日期API知識(shí)點(diǎn)以及相關(guān)的實(shí)例代碼分享,有興趣的朋友參考學(xué)習(xí)下。2018-04-04
java 使用poi動(dòng)態(tài)導(dǎo)出的操作
這篇文章主要介紹了java 使用poi動(dòng)態(tài)導(dǎo)出的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12

