Mybatis-Plus selectByMap Mybatis-Plus中的selectByMap使用实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis-Plus selectByMap Mybatis-Plus中的selectByMap使用实例

陈承宇- 。-   2021-11-01 我要评论
想了解Mybatis-Plus中的selectByMap使用实例的相关内容吗,陈承宇- 。-在本文为您仔细讲解Mybatis-Plus selectByMap的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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<bookBorrowing>{
 
}

其中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 <T> extends com.baomidou.mybatisplus.core.mapper.Mapper<T> {
    int insert(T entity);
 
    int deleteById(java.io.Serializable id);
 
    int deleteByMap(@org.apache.ibatis.annotations.Param("cm") java.util.Map<java.lang.String,java.lang.Object> columnMap);
 
    int delete(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    int deleteBatchIds(@org.apache.ibatis.annotations.Param("coll") java.util.Collection<? extends java.io.Serializable> 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<T> updateWrapper);
 
    T selectById(java.io.Serializable id);
 
    java.util.List<T> selectBatchIds(@org.apache.ibatis.annotations.Param("coll") java.util.Collection<? extends java.io.Serializable> idList);
 
    java.util.List<T> selectByMap(@org.apache.ibatis.annotations.Param("cm") java.util.Map<java.lang.String,java.lang.Object> columnMap);
 
    T selectOne(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    java.lang.Integer selectCount(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    java.util.List<T> selectList(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    java.util.List<java.util.Map<java.lang.String,java.lang.Object>> selectMaps(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    java.util.List<java.lang.Object> selectObjs(@org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    <E extends com.baomidou.mybatisplus.core.metadata.IPage<T>> E selectPage(E page, @org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> queryWrapper);
 
    <E extends com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>> E selectMapsPage(E page, @org.apache.ibatis.annotations.Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<T> 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<String,Object> map = new HashMap<String, Object>();
        map.put("student_code","123456");
        List<bookBorrowing> stu = borrowMapper.selectByMap(map);
        for(bookBorrowing s:stu){
            System.out.println(s);
        }
    }
}

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

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

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

这是运行的结果:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们