好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Mybatis-Plus中的selectByMap使用实例

前言:

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

正文:

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

?

1

2

3

4

5

6

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中接口就有该函数:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

   // 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调用的就是其中的函数。

接下来就是调用的方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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函数最简单基础的用法,如果有什么写得不对或者不够充分的地方还请各位大佬指正补充,我也好跟着一起学习~~

到此这篇关于Mybatis-Plus中的selectByMap使用实例的文章就介绍到这了,更多相关Mybatis-Plus selectByMap内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_51043896/article/details/120048771

查看更多关于Mybatis-Plus中的selectByMap使用实例的详细内容...

  阅读:44次