Mybatis Plus 使用学习五 mapper查询用法 上

Mybatis Plus本身提供基于Service和Mapper的各种增删改查的方法。

这一节我们来详细看一下Mybatis Plus中Mapper的查询功能。

Mybatis Plus的查询默认提供了10种查询方式,主要支持单条查询和批量查询,还有就是返回值的区别,我们来看看每种查询的使用方式。

1、根据主键查询单条记录

代码 :

    @Test
    public void testSelectById() {
        System.out.println(("----- selectById method test ------"));
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

输出 :

==>  Preparing: SELECT id,name,age,email FROM user WHERE id=? 
==> Parameters: 1(Integer)
<==    Columns: ID, NAME, AGE, EMAIL
<==        Row: 1, Jone, 18, test1@baomidou.com
<==      Total: 1

System.out.println输出:

User(id=1, name=Jone, age=18, email=test1@baomidou.com)

2、根据指定条件,查询单条数据

代码:

    @Test
    public void testSelectOne() {
        System.out.println(("----- selectOne method test ------"));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Name", "Jack");
        User user = userMapper.selectOne(wrapper);
        System.out.println(user);
    }

输出:

==>  Preparing: SELECT id,name,age,email FROM user WHERE (Name = ?) 
==> Parameters: Jack(String)
<==    Columns: ID, NAME, AGE, EMAIL
<==        Row: 2, Jack, 20, test2@baomidou.com
<==      Total: 1

System.out.println输出:

User(id=2, name=Jack, age=20, email=test2@baomidou.com)

3、根据主键的集合,查询多条数据

代码:

    @Test
    public void testSelectBatchIds() {
        System.out.println(("----- selectBatchIds method test ------"));
        List<Integer> ids =  new ArrayList<>();
        ids.add(1);
        ids.add(2);
        List<User> users = userMapper.selectBatchIds(ids);
        users.forEach(System.out::println);
    }

输出 :

==>  Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? ) 
==> Parameters: 1(Integer), 2(Integer)
<==    Columns: ID, NAME, AGE, EMAIL
<==        Row: 1, Jone, 18, test1@baomidou.com
<==        Row: 2, Jack, 20, test2@baomidou.com
<==      Total: 2

System.out.println输出:

User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)

4、根据Map作为条件,查询多条数据

代码:

    @Test
    public void testSelectByMap() {
        System.out.println(("----- selectByMap method test ------"));
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("Name","Jack");
        columnMap.put("Name","Jone");
        columnMap.put("Age",20);
        List<User> users = userMapper.selectByMap(columnMap);
        users.forEach(System.out::println);
    }

输出:

==>  Preparing: SELECT id,name,age,email FROM user WHERE Age = ? AND Name = ? 
==> Parameters: 20(Integer), Jone(String)
<==      Total: 0
Closing non transactio

这里查询结果为0条是数据,看日志输出,age:20,Name:Jone。

我实际的Name设置了两次,分别是:

columnMap.put("Name","Jack");
columnMap.put("Name","Jone");

我们都知道map的特性,key不能重复,所以设置两次肯定是以最后依次为准,所以这里的参数是Name:Jone。

实际Jone的age是18,我参数设置了20,肯定查不出来,设置为18是可以查出结果的。

selectByMap的作用就是基于一列的多个字段作为条件来查询,并且条件是and的关系。

5、根据设置条件统计总记录数

代码:

    @Test
    public void testSelectCount() {
        System.out.println(("----- selectCount method test ------"));
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("Name","Jack");
        columnMap.put("Name","Jone");
        columnMap.put("Age",20);
        Integer integer = userMapper.selectCount(null);
        System.out.println(("----- 总记录数: "+integer));
    }

输出:

==>  Preparing: SELECT COUNT( 1 ) FROM user 
==> Parameters: 
<==    Columns: COUNT(*)
<==        Row: 5
<==      Total: 1

System.out.println输出:

----- 总记录数: 5