Mybatis-Plus中的selectByMap实例

前言:

        我在开始用Mybatis-Plus来对数据库进行增删改查时,将里面的函数试了个遍,接下来我就将使用selectByMap函数的简单测试实例写出来,方便没有使用过的朋友们快速上手

正文:

       首先我们要使用这个selectByMap函数,需要在我们的Mapper中继承mybatis-plus包中相应的接口

package com.example.library.Mapper;
import com.example.library.entity.bookBorrowing;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface borrowMapper extends BaseMapper{

}

其中BaseMapper中接口就有该函数:
 


  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package com.baomidou.mybatisplus.core.mapper;

public interface BaseMapper  extends com.baomidou.mybatisplus.core.mapper.Mapper {
    int insert(T entity);

    int deleteById(java.io.Serializable id);

    int deleteByMap(@org.apache.ibatis.annotations.Param("cm") java.util.Map columnMap);

    int delete(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    int deleteBatchIds(@org.apache.ibatis.annotations.Param("coll") java.util.Collection idList);

    int updateById(@org.apache.ibatis.annotations.Param("et") T entity);

    int update(@org.apache.ibatis.annotations.Param("et") T entity, @org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper updateWrapper);

    T selectById(java.io.Serializable id);

    java.util.List selectBatchIds(@org.apache.ibatis.annotations.Param("coll") java.util.Collection idList);

    java.util.List selectByMap(@org.apache.ibatis.annotations.Param("cm") java.util.Map columnMap);

    T selectOne(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    java.lang.Integer selectCount(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    java.util.List selectList(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    java.util.List> selectMaps(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    java.util.List selectObjs(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    > E selectPage(E page, @org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);

    >> E selectMapsPage(E page, @org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper queryWrapper);
}

其中的selectByMap调用的就是其中的函数。

接下来就是调用的方法:

package com.example.library;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.library.Mapper.*;
import com.example.library.entity.*;
import org.mybatis.spring.annotation.MapperScan;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MapperScan("com/example/library/Mapper")
@SpringBootTest

class LibraryApplicationTests {

    @Autowired
    private borrowMapper borrowMapper;

    @Test
    public void mapSelect(){
        Map map = new HashMap();
        map.put("student_code","123456");
        List stu = borrowMapper.selectByMap(map);
        for(bookBorrowing s:stu){
            System.out.println(s);
        }
    }
}

@Test注解是表示这是一个测试类,可以单独拎出来测试。

这条语句是,将查到的student_code为123456的那一行信息拿出来并打印在控制台上。

 这是数据库中的相关信息:

Mybatis-Plus中的selectByMap实例_第1张图片

这是运行的结果:

Mybatis-Plus中的selectByMap实例_第2张图片

 这就是selectByMap函数最简单基础的用法,如果有什么写得不对或者不够充分的地方还请各位大佬指正补充,我也好跟着一起学习~~

你可能感兴趣的:(工作日志,数据库,sql,mybatis,spring,boot)