本篇文章小编给大家分享一下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 层来说的
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级