如果是List類型的String,例如:List<String>這種類型的,就直接放值就可以了,本文講的是當(dāng)你查詢到的是一個list集合如何遍歷取值,否則要寫sql和接口就顯得很麻煩。
步驟如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//查詢到list集合 List<User> userList = userService.selectById(id); //結(jié)果集 List<String> resultList = new ArrayList<>(); //遍歷集合取值 userList .forEach(item->{ resultList.add(item.getYouNeedId()); }); //條件構(gòu)造器in上手使用 QueryWrapper<User> qw = new QueryWrapper<>(); qw.in( "you_need_id" , resultList); //這里有個分頁的查詢,你也可以不用分頁,用mybatisplus里面封裝的其他方法 IPage<User> userIPage = userMapper.selectPage(page, qw); //返回查詢結(jié)果,getRecords也是mybatisplus里面封裝的方法 return contractRecordIPage.getRecords(); |
補充:Mybatis Plus 通過QueryWrapper做查詢時in()方法的使用
UserId類:
1
2
3
4
5
6
7
|
@Data public class UserId { /** * 用戶id集合 */ private JSONArray userIdList; } |
測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Test{ public JSONArray getUserStatusList(UserId userId) { // 添加非空校驗,JsonArray對象為null或長度為0時直接返回,不執(zhí)行sql if (userId.getUserIdList() == null || userId.getUserIdList().size() == 0 ) { return new JSONArray(); } // 創(chuàng)建查詢Wrapper對象 QueryWrapper wrapper = new QueryWrapper(); wrapper.in( "user_id" , userId.getUserIdList()); List list = baseMapper.selectObjs(wrapper); return JSONArray.parseArray(JSON.toJSONString(list)); } } |
注意:如果不加非空校驗,當(dāng)集合為空集合時會報SQL的異常
到此這篇關(guān)于mybatis plus in方法使用詳解的文章就介紹到這了,更多相關(guān)mybatis plus in內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_15072163/article/details/107055792