select 查询和 Wrapper

注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码

本章节将介绍 BaseMapper 的几个用法最简单的 select 方法,以及怎样通过 Wrapper 构建查询条件。如下:

  • selectById:根据 ID 查询

  • selectBatchIds:根据 ID 批量查询,即一次传递多个 ID

  • selectOne:根据构建的 Wrapper 条件查询数据,且只返回一个结果对象

  • selectCount:根据构建的 Wrapper 条件对象查询数据条数

注意:SimpleMapper 已经在“MyBatis Plus 简单示例”章节进行了定义,这里将不再赘述。

示例代码

(1)根据 ID 查询,代码如下:

package com.hxstrive.mybatis_plus.select;

import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
class Select0Test {

    @Autowired
    private SimpleMapper simpleMapper;

    @Test
    void contextLoads() {
        UserBean userBean = simpleMapper.selectById(1);
        System.out.println(userBean);
    }

}

(2)根据 ID 进行批量查询,代码如下:

package com.hxstrive.mybatis_plus.select;

import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
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.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
class Select1Test {

    @Autowired
    private SimpleMapper simpleMapper;

    @Test
    void contextLoads() {
        List<Integer> ids = Arrays.asList(1, 2, 3);
        List<UserBean> userBeanList = simpleMapper.selectBatchIds(ids);
        for(UserBean userBean : userBeanList) {
            System.out.println(userBean);
        }
    }

}

(3)根据构建的 Wrapper 条件(根据ID查询用户信息)对象,使用 selectOne 查询数据,且只返回一个对象。代码如下:

package com.hxstrive.mybatis_plus.select;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
class Select8Test {

    @Autowired
    private SimpleMapper simpleMapper;

    @Test
    void contextLoads() {
        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
        wrapper.le("user_id", 1);
        UserBean userBean = simpleMapper.selectOne(wrapper);
        System.out.println(userBean);
    }

}

(4)获取数据总数,也可以使用 Wrapper 条件过滤数据。代码如下:

package com.hxstrive.mybatis_plus.select;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
class Select9Test {

    @Autowired
    private SimpleMapper simpleMapper;

    @Test
    void contextLoads() {
        // 统计年龄大于等于 28 岁的用户数
        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 28);
        int count = simpleMapper.selectCount(wrapper);
        System.out.println("count=" + count);
    }

}

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号