MyBatisPlus利用Service實現(xiàn)獲取數(shù)據(jù)列表
1. 簡單介紹
嗨,大家好,今天給想給大家分享一下關(guān)于Mybatis-plus 的 Service 層的一些方法的使用。今天沒有總結(jié),因為都是一些API沒有什么可以總結(jié)的,直接看著調(diào)用就可以了。
下面介紹怎樣使用 IServer 提供的 list 方法查詢多條數(shù)據(jù),這些方法將根據(jù)查詢條件獲取多條數(shù)據(jù)。
2. 接口說明
接口提供了如下十個 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ù)說明
queryWrapper:實體對象封裝操作類 QueryWrapper
idList:主鍵ID列表
columnMap:表字段 map 對象
mapper:轉(zhuǎn)換函數(shù)
4. 實例代碼
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 注意事項說明
請注意,這里我們所描述的一切方法都是基于 Service 層來說的
請注意,這里我們所描述的一切方法都是不是基于 Mapper 層來說的
到此這篇關(guān)于MyBatisPlus利用Service實現(xiàn)獲取數(shù)據(jù)列表的文章就介紹到這了,更多相關(guān)MyBatisPlus Service獲取數(shù)據(jù)列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ElasticSearch 動態(tài)映射實戰(zhàn)詳解
這篇文章主要為大家介紹了ElasticSearch 動態(tài)映射實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
SpringBoot+TestNG單元測試的實現(xiàn)
本文主要介紹了SpringBoot+TestNG單元測試的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Springboot-Starter造輪子之自動鎖組件lock-starter實現(xiàn)
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動鎖組件lock-starter實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題
這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
關(guān)于springboot 配置文件中屬性變量引用方式@@解析
這篇文章主要介紹了關(guān)于springboot 配置文件中屬性變量引用方式@@解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

