本篇文章小编给大家分享一下MyBatisPlus利用Service实现获取数据列表代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
接口说明
接口提供了如下十个 list 方法:
// 查询所有 Listlist(); // 查询列表 List list(Wrapper queryWrapper); // 查询(根据ID 批量查询) Collection listByIds(Collection extends Serializable> idList); // 查询(根据 columnMap 条件) Collection listByMap(Map columnMap); // 查询所有列表 List
参数说明
queryWrapper:实体对象封装操作类 QueryWrapper
idList:主键ID列表
columnMap:表字段 map 对象
mapper:转换函数
实例代码
1 不带任何参数的 list() 方法查询数据
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() { ListuserBeanList = userService.list(); System.out.println("size=" + userBeanList.size()); } }
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() { QueryWrapperwrapper = new QueryWrapper<>(); wrapper.gt("user_id", 10); wrapper.lt("user_id", 20); wrapper.eq("sex", "男"); List userBeanList = userService.list(wrapper); for(UserBean userBean : userBeanList) { System.out.println(userBean); } } }
3 注意事项说明
请注意,这里我们所描述的一切方法都是基于 Service 层来说的
请注意,这里我们所描述的一切方法都是不是基于 Mapper 层来说的
火柴人绳索英雄2无限金币版 (Stickman Rope Hero 2)安卓版v2.2v3.4.2
下载奥特曼之格斗超人内测版本 安卓最新版v11.0.0
下载奥特曼之格斗超人百度版 安卓版v11.0.0
下载奥特曼之格斗超人4399游戏盒版 最新安卓版v11.0.0
下载德凯奥特曼D闪光剑模拟器 安卓版v1.0
德凯奥特曼D闪光剑模拟器是一个非常有趣的奥特曼变身模拟器,可
亚刻觉醒器 安卓版v1.1
亚刻觉醒器模拟器是一个非常有趣的奥特曼变身模拟器,为喜欢亚刻
海上餐厅无限金币版 v1.0
海上餐厅免广告版是游戏的破解版本,在该版本中为玩家去除了广告
阿西美女室友竟然全解锁版 v1.0.2
阿西美女室友竟然内置菜单版是游戏的破解版本,在该版本中为玩家
我是大东家正版 最新版v1.2.738
我是大东家正版是一款放置类模拟经营手游,玩家将回到古代成为家