每天玩轉3分鐘 MyBatis-Plus - 2. 普通查詢

每天玩轉3分鐘 MyBatis-Plus - 1. 配置環境

每天玩轉3分鐘 MyBatis-Plus - 2. 普通查詢

代碼下載:https://github.com/Jackson0714/study-mybatis-plus.git

mybatis-plus的查詢功能非常強大, 這一篇,我們來看下mybatis-plus的普通查詢功能。後續文章再介紹高級查詢功能。

一、創建User表

User 表結構如下:

idnameageemail
1 Jone 18 [email protected]
2 Jack 20 [email protected]
3 Tom 28 [email protected]
4 Sandy 21 [email protected]
5 Billie 24 [email protected]

其對應的數據庫 Schema 腳本如下:

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主鍵ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年齡',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',
	PRIMARY KEY (id)
);

其對應的數據庫 Data 腳本如下:

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

二、創建基礎類

2.1 User 類

 1 package com.example.demo.entity;
 2  
 3 import lombok.Data;
 4  
 5 @Data
 6 public class User {
 7     private Long id;
 8     private String name;
 9     private Integer age;
10     private String email;
11 }

2.2 創建UserMapper接口,繼承 MyBatis Plus BaseMapper接口

1 package com.example.demo.mapper;
2 
3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.example.demo.entity.User;
5 
6 public interface UserMapper extends BaseMapper<User> {
7 }  

三、查詢方法

3.1 單表查詢所有記錄 selectList

 1     /*
 2     * 描述:單表查詢所有記錄
 3     * 作者:博客園-悟空聊架構
 4     * 時間:2019-01-16
 5     * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 6     * 博客園:https://www.cnblogs.com/jackson0714
 7     * */
 8     @Test
 9     public void testSelect() {
10         System.out.println(("----- 單表查詢所有記錄------"));
11         List<User> userList = userMapper.selectList(null);
12         Assert.assertEquals(6, userList.size()); //表裏面的記錄總條數是否等於6,如果等於6,則測試通過
13         userList.forEach(System.out::println);
14     }
userMapper.selectList(null);
對應的SQL語句是:
SELECT id,name,age,email FROM user

如果找不到這些日誌,可以點擊如下按鈕找到:

數據庫有6條記錄

查詢出總條數6條,測試通過

 3.2 單表根據主鍵id查詢單條記錄 selectById

 1     /*
 2      * 描述:單表根據主鍵id查詢單條記錄
 3      * 作者:博客園-悟空聊架構
 4      * 時間:2019-01-16
 5      * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 6      * 博客園:https://www.cnblogs.com/jackson0714
 7      * */
 8     @Test
 9     public void testSelectById() {
10         System.out.println(("----- 單表根據主鍵id查詢單條記錄 ------"));
11         User user = userMapper.selectById(2);
12         System.out.println(user);
13     }
1 userMapper.selectById(2)
2 對應的SQL語句爲
3 SELECT id,name,age,email FROM user WHERE id=? 

數據庫中有一條記錄

3.3 單表根據 id list 批量查詢 selectBatchIds

 1     /*
 2      * 描述:單表根據 id list 批量查詢
 3      * 作者:博客園-悟空聊架構
 4      * 時間:2019-01-16
 5      * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 6      * 博客園:https://www.cnblogs.com/jackson0714
 7      * */
 8     @Test
 9     public void testSelectByIds() {
10         System.out.println(("----- 單表根據 id list 批量查詢 ------"));
11         List<Long> idsList = Arrays.asList(2L,4L,6L);
12         List<User> userList= userMapper.selectBatchIds(idsList);
13         userList.forEach(System.out::println);
14     }

userMapper.selectBatchIds(idsList);
對應的SQL語句爲
SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? ) 

查詢結果,id=2,id=4 的查詢出來了,沒有 id=6 的記錄

 3.4 單表根據條件查詢 selectByMap

 1      /*
 2      * 描述:單表根據條件查詢
 3      * 作者:博客園-悟空聊架構
 4      * 時間:2019-01-19
 5      * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 6      * 博客園:https://www.cnblogs.com/jackson0714
 7      * */
 8     @Test
 9     public void testSelectByMap() {
10         System.out.println(("----- 單表根據條件查詢 ------"));
11         Map<String, Object> conditions = new HashMap<>();
12         conditions.put("name", "Jack");
13         conditions.put("age", 20);
14         List<User> userList= userMapper.selectByMap(conditions);
15         userList.forEach(System.out::println);
16     }
userMapper.selectByMap(conditions);
對應的SQL語句爲
SELECT id,name,age,email FROM user WHERE name = ? AND age = ?

 

數據庫有一條記錄,name="Jack",age=20

 注意:使用slectByMap時,條件裏面的字段名需要和數據庫保持一致。

 不然會出現以下錯誤:

 

每天玩轉3分鐘 MyBatis-Plus - 1. 配置環境

每天玩轉3分鐘 MyBatis-Plus - 2. 普通查詢

 

關注公衆號:悟空聊架構,回覆pmp,領取pmp資料!回覆悟空,領取架構師資料!


作  者:悟空聊架構 
出  處:http://www.cnblogs.com/jackson0714/ 
關於作者:專注於移動開發。如有問題或建議,請多多賜教! 
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。 
特此聲明:所有評論和私信都會在第一時間回覆。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我 
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角推薦】一下。您的鼓勵是作者堅持原創和持續寫作的最大動力! 

悟空聊架構 

關注我,帶你每天進步一點點!

還有111本書籍贈送~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章