MyBatis-Plus 常用查询(一)

转载请注明出处:https://blog.csdn.net/qq_41254299

本文出自  [Superclover_的博客]

 MyBatis-Plus 入门操作:https://blog.csdn.net/qq_41254299/article/details/104139486

 查询方法(Select)介绍

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
参数说明
类型 参数名 描述
Serializable id 主键ID
Wrapper<T> queryWrapper 实体对象封装操作类(可以为 null)
Collection<? extends Serializable> idList 主键ID列表(不能为 null 以及 empty)
Map<String, Object> columnMap 表字段 map 对象
IPage<T> page 分页查询条件(可以为 RowBounds.DEFAULT)

 MyBatis-Plus 查询篇

package com.sen.MybatisPlus.MybatisPlus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.MybatisPlus.MybatisPlus.entity.User;
import com.sen.MybatisPlus.MybatisPlus.mapper.UserMapper;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有数据
     * UserMapper 中的 selectList() 方法的参数
     * 为 MP 内置的条件封装器 Wrapper,所以不填写就是无任何条件(条件构造器)
     */
    @Test
    public void selectList() {
        List<User> list = userMapper.selectList(null);
        list.forEach(System.out::println);
    }

    /**
     * 根据 entity 条件,查询一条记录
     * 其实 QueryWrapper 条件构造器(相当于给SQL的条件语句)
     * selectOne() 方法查询必须有且只能查询一条记录,多一条会报错。
     */
    @Test
    public void selectOne() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //查询名字为 Tom 的一条记录
        queryWrapper.eq("name","Tom");
        User user = userMapper.selectOne(queryWrapper);
        System.out.println(user);
    }

    /**
     * 根据主键ID查询数据
     * 查询主键id=2 的一条数据,只能查询一个主键的数据不能多个
     */
    @Test
    public void selectById() {
        User user = userMapper.selectById(2);
        System.out.println(user);
    }

    /**
     * 根据 List 的 ID 集合 查询对应的用户list
     */
    @Test
    public void selectBatchIds() {
        List<User> list = userMapper.selectBatchIds(Arrays.asList(5, 3, 1));
        list.forEach(System.out::println);
    }

    /**
     * 查询(根据 columnMap 条件),查询年龄为20岁的所有记录
     *
     * 注意:建议尽量减少使用map这种方式。
     * 因为可能字段名可能存在修改的情况,
     * 如果,项目开发一段时间后,再修改,影响太大
     */
    @Test
    public void selectByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("age", "20");
        List<User> list = userMapper.selectByMap(map);
        list.forEach(System.out::println);
    }

    /**
     * 查询大于20岁的学生,名称中包含“J”的人,带条件判断的,可以采用这种方式
     * SELECT * FROM user WHERE (name LIKE ? AND age > ?)
     * gt()方法 相当于:大于 > 但没有等于=
     */
    @Test
    public void selectListWrapper() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "J");//格式:(字段,值)
        queryWrapper.gt("age", "19"); //查询不小于20岁
        List<User> list = userMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
    }

    /**
     * 根据 Wrapper 条件,查询全部记录。
     * 查询名字含有 a 的,且年龄大于等于20。
     * ge()方法 相当于: 大于等于 >= 。
     * like()方法  相当于: NOT LIKE '%值%' 。
     */
    @Test
    public void selectMaps(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name","a");
        queryWrapper.ge("age","20");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        maps.forEach(System.out::println);
    }
    

    //分页查询需要需要在项目中加入分页插件,下文介绍有-------
    /**
     * 根据 entity 条件,查询全部记录(并翻页)。
     * 查询年龄大于 18 且 小于等于30 岁的所有记录,分 3 条数记录为一页
     * gt()方法 相当于: 大于 > 。
     * le()方法 相当于: 小于等于 <= 。
     */
    @Test
    public void selectPage(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age","18");
        queryWrapper.le("age","30");
        //第一个参数表示当前页
        //第二个参数表示当前页显示多少条
        //第三个参数是否查询count
        Page<User> page = new Page<>(1,3);
        Page<User> userIPage = userMapper.selectPage(page, queryWrapper);
        List<User> records = userIPage.getRecords();
        records.forEach(System.out::println);
    }


    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     */
    @Test
    public void selectMapsPage() {
        QueryWrapper<User> query = new QueryWrapper<User>();
        //第一个参数表示当前页
        //第二个参数表示当前页显示多少条
        Page<Map<String, Object>> page = new Page<>(2, 3);
        Page<Map<String, Object>> maps = userMapper.selectMapsPage(page, query);
        maps.getRecords().forEach(System.out::println);

    }


    /**
     * 根据 Wrapper 条件,查询总记录数
     * 查询一共有多少条数据记录
     */
    @Test
    public void selectCount(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Integer count = userMapper.selectCount(queryWrapper);
        System.out.printf("一共有" + String.valueOf(count) + "条记录");
    }

}

SQL日志输出配置

mybatis-plus配置控制台打印完整带参数SQL语句方便联合控制台和Navicat/PLSQL等工具进行语句的拼接检查,极大提高我们的效率。(以 IDEA 开发工具为例)

application.yml 配置方式:
 

mybatis-plus:
     configuration:
          log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

 

application.properties 配置方式:

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

 分页查询插件(Spring Boot 方式)

package com.sen.MybatisPlus.MybatisPlus.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.sen.MybatisPlus.MybatisPlus.mapper")
public class MyBatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

}

 在项目下新建package包,新建类 MyBatisPlusConfig.class

 其他 spring......等 设置方式请看官方文档:https://mp.baomidou.com/guide/page.html

转载请注明出处:https://blog.csdn.net/qq_41254299

本文出自  [Superclover_的博客]

猜你喜欢

转载自blog.csdn.net/qq_41254299/article/details/104384257