Mybatis-Plus 实战完整学习笔记(七)------select测试二
1.查询selectOne (3.0.3版)
@Test
public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Employee employee =employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id","1")); if (employee!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(employee));
} }
其相当于SQL语句:SELECT id,last_name,email,gender,age FROM tbl_employee WHERE id = ?
2.3版本的写法是直接注入一个对象,对象的set值就是查询的条件,同时,ID要是Long或Integer类型,条件一定要唯一,否则,会报数据重复的异常。
Employee employee = new Employee();
employee.setId(1); Employee result =employeeMapper.selectOne(employee); if (result!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(result));
}
2、查询selectMaps(3.0.3新出)
@Test
public void selectMethod() throws SQLException { // 6、多个Map查询
Employee employee = employeeMapper.selectById(1); QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>(); List<Map<String, Object>> employeeList =employeeMapper.selectMaps(employeeQueryWrapper); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
写法跟其他的查询没有区别,只是返回的结果,变成List<Map<String,Object>>,条件查询跟上边的一样。
返回样式
++一个员工信息+++++[{"gender":"0","last_name":"Marry","id":1,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":2,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":3,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":4,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":12,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":13,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":14,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":15,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":16,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":17,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":18,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":19,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":20,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":21,"email":"1","age":55}] (TestCRUD.java:159)
本来以为是要装多个map进去查询就写成下边的样子,结果不是那个意思
// 6、多个Map查询
Employee employee = employeeMapper.selectById(1); Map<String,Object> map1 = new HashMap<>(16);
map1.put("email",employee.getEmail());
map1.put("age",employee.getAge()); Employee employee2 = employeeMapper.selectById(2);
Map<String,Object> map2 = new HashMap<>(16);
map2.put("email",employee2.getEmail());
map2.put("age",employee2.getAge()); List<Map<String,Object>> mapList = new ArrayList<>();
mapList.add(map1);
mapList.add(map2); QueryWrapper<List<Map<String, Object>>> maps = new QueryWrapper<List<Map<String, Object>>>();
maps.setEntity((mapList)); List<Map<String, Object>> employeeList =employeeMapper.selectMaps(maps);
3、查询selectObjs
@Test
public void selectMethod() throws SQLException { // 7、selectObjs
Employee employee = employeeMapper.selectById(1); List<Object> employeeList =employeeMapper.selectObjs(new QueryWrapper<Employee>().eq("email",employee.getEmail())); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
用法一样,就是返回的结果是个Object类型。
4、统计结果查询selectCount
@Test
public void selectMethod() throws SQLException { // 8、selectCount
Employee employee = employeeMapper.selectById(1); Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("email", employee.getEmail())); if (count > 0) {
logger.info("++统计结果:+++++" + count);
} }
5、分页查询selectPage
@Test
public void selectMethod() throws SQLException { // 9、分页查询 Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100)
.eq("gender", 0)); IPage<Employee> employeeIPage = new Page<Employee>(1, 3,count); IPage<Employee> employeeList = employeeMapper.selectPage(employeeIPage,new QueryWrapper<Employee>()
.between("age", 0, 100)
.eq("gender", 0)); if (!employeeList.getRecords().isEmpty()) {
logger.info("++一个员工信息+++++" + gson.toJson(employeeList));
} }
6、分页查询返回map类型的selectMapsPage
@Test
public void selectMethod() throws SQLException { // 10、分页查询Map结果返回
Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100)
.eq("gender", 0)); IPage<Employee> employeeIPage = new Page<Employee>(1, 3, count); IPage<Map<String, Object>> employeeList = employeeMapper.selectMapsPage(employeeIPage, new QueryWrapper<Employee>()
.between("age", 0, 100)
.eq("gender", 0)); if (!employeeList.getRecords().isEmpty()) {
logger.info("++一个员工信息+++++" + gson.toJson(employeeList));
} }
System.out.println("返回数据:"+employeeIPage.getRecords());
System.out.println("总条数:"+page.getTotal());
System.out.println("当前页码:"+page.getCurrent());
System.out.println("总页码:"+page.getPages());
System.out.println("每页显示条数:"+page.getSize());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
System.out.println("返回的数据:"+page.getRecords());
Mybatis-Plus 实战完整学习笔记(七)------select测试二的更多相关文章
- Mybatis-Plus 实战完整学习笔记(六)------select测试一
查询方法(3.0.3) 1.查询一个员工的数据 @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...
- Mybatis-Plus 实战完整学习笔记(八)------delete测试
1.根据ID删除一个员工deleteById /** * 删除客户 * * @throws SQLException */ @Test public void deletedMethod() thro ...
- Mybatis-Plus 实战完整学习笔记(五)------insert测试
一.更新操作探究(3.0.3版本) demo /** * 修改用户 * @throws SQLException */ @Test public void update() throws SQLExc ...
- Mybatis-Plus 实战完整学习笔记(一)------简介
第一章 简介 1. 什么是MybatisPlus MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只 ...
- Mybatis-Plus 实战完整学习笔记(十一)------条件构造器删除,修改,conditon
1.修改功能--其他过滤方式跟select一样 /** * 修改条件构造器 * @throws SQLException */ @Test public void selectUpdate() thr ...
- Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)
31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...
- Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)
一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...
- Mybatis-Plus 实战完整学习笔记(四)------全局参数配置
一.全局配置设置 (1)全局配置Id自动生成 <!--定义mybatisplus全局配置--> <bean id="globalConfig" class=&qu ...
- Mybatis-Plus 实战完整学习笔记(二)------环境搭建
第二章 使用实例 1.搭建测试数据库 -- 创建库 CREATE DATABASE mp; -- 使用库 USE mp; -- 创建表 CREATE TABLE tbl_employee( ...
随机推荐
- 浅析Java 数组-基础详解
什么是数组:数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 数组:用来存储固定大小的同类型元素. 一 声明.创建,初始化Java 数组 写在前面 ...
- linux命令学习之:sed
sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上 ...
- git bash 基本命令
1.打开git bash界面后,进入某个目录下时时,可以使用cd 命令,cd时change directory的简写,表示改变目录,比如,想切换到某个盘符下,可以使用cd g:,则会进入到g盘路径下, ...
- golang语言中os/exec包的学习与使用
package main; import ( "os/exec" "fmt" "io/ioutil" "bytes" ) ...
- Linux下网络排查之ping|traceroute|mtr工具(zz)
1.ping ping使用了ICMP回送请求和回送应答报文.ping工具发出去的数据包没有通过tcp/udp协议,但是要经过ip协议.ping命令计算的时间是数据包的往返总时间. ping命令常用 ...
- 十年百度工作心得(月薪75k)
百度,是多少从事IT事业的程序员梦寐以求的地方,能进入这样大厂的程序员可以说都是数一数二的人才. 最近有不少朋友问,成为百度,腾讯,阿里Java架构师需要系统学习哪些Java技术. 下面分享互联网Ja ...
- How to update XENTRY Connect C5 software with .iso file
07.2018 Xentry Mercedes SD Connect c5 software update manual for newbies: Important: If you have XDO ...
- Git和SourceTree入门教程
转自CSDN:http://blog.csdn.net/collonn/article/details/39259227 -->本教程适用于主流的开源网站github和bitbucket,个人认 ...
- (转帖)CentOS最常用命令及快捷键整理
原文:http://www.centoscn.com/CentOS/help/2014/0306/2493.html 最近开始学Linux,在VMware Player中安装了CentOS 6.4.为 ...
- macOS X Mount NFS Share / Set an NFS Client
last updated November 3, 2018 in CategoriesLinux, Mac OS X, UNIX How do I access my enterprise NAS s ...