前言:
我在開始用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