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测试二的更多相关文章

  1. Mybatis-Plus 实战完整学习笔记(六)------select测试一

    查询方法(3.0.3) 1.查询一个员工的数据 @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...

  2. Mybatis-Plus 实战完整学习笔记(八)------delete测试

    1.根据ID删除一个员工deleteById /** * 删除客户 * * @throws SQLException */ @Test public void deletedMethod() thro ...

  3. Mybatis-Plus 实战完整学习笔记(五)------insert测试

    一.更新操作探究(3.0.3版本) demo /** * 修改用户 * @throws SQLException */ @Test public void update() throws SQLExc ...

  4. Mybatis-Plus 实战完整学习笔记(一)------简介

    第一章    简介      1. 什么是MybatisPlus                MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只 ...

  5. Mybatis-Plus 实战完整学习笔记(十一)------条件构造器删除,修改,conditon

    1.修改功能--其他过滤方式跟select一样 /** * 修改条件构造器 * @throws SQLException */ @Test public void selectUpdate() thr ...

  6. Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)

    31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...

  7. Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)

    一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...

  8. Mybatis-Plus 实战完整学习笔记(四)------全局参数配置

    一.全局配置设置 (1)全局配置Id自动生成 <!--定义mybatisplus全局配置--> <bean id="globalConfig" class=&qu ...

  9. Mybatis-Plus 实战完整学习笔记(二)------环境搭建

     第二章    使用实例   1.搭建测试数据库 -- 创建库 CREATE DATABASE mp; -- 使用库 USE mp; -- 创建表 CREATE TABLE tbl_employee( ...

随机推荐

  1. jQuery跳转到页面指定位置

    @参考博客 var t = $("#id").offset().top;// 获取需要跳转到标签的top值 //$(window).scrollTop(t);// 跳转到指定位置 ...

  2. android屏蔽BACK键、HOME键和多任务键

    HOME: @Overridepublic void onAttachedToWindow() { System.out.println("Page01 -->onAttachedTo ...

  3. activity背景毛玻璃效果

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  4. 解决jenkins的内存溢出问题

    在jenkins的控制台会看到如下信息: FATAL: Remote call on ime_checkcode failed java.io.IOException: Remote call on ...

  5. for循环中的 break和continue的区别

    break 语句用于跳出循环. for (i=0;i<10;i++) { if (i==3) { break; } x=x + "The number is " + i + ...

  6. 解决loadrunner录制页面的乱码问题

    以下亲自验证了的:好用.     三步解决loadrunner录制页面的乱码问题 第一步:去lr 的vugen的Tools -> Recoding Options -> Advanced ...

  7. andorid 全部对话框

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  8. BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防

    一道树的直径 树网的核 BZOJ原题链接 树网的核 洛谷原题链接 消防 BZOJ原题链接 消防 洛谷原题链接 一份代码四倍经验,爽 显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍 ...

  9. qt 5.2.1类和模块的关系图

    QT│  ├─ActiveQt│  │  ActiveQt│  │  ActiveQtDepends│  │  ActiveQtVersion│  │  QAxAggregated│  │  QAxB ...

  10. Configuration Reference In Vue CLI 3.0

    Configuration Reference This project is sponsored by  #Global CLI Config Some global configurations ...