MyBatis-Plus Select Query Examples and Usage
This article provides a step‑by‑step tutorial on using MyBatis‑Plus select queries, including data preparation, SQL table creation, and detailed Java code examples for retrieving specific fields and filtering by age with both simple and advanced query wrappers.
The article introduces a series of MyBatis‑Plus tutorials titled “每天玩转3分钟 MyBatis‑Plus”, focusing on the select query functionality. After a brief recap of previous advanced query examples, it presents a new case study on using select queries.
Data preparation includes creating a user table and inserting sample records with fields such as id, name, age, email, manager_id, and create_time. The SQL statements are shown in a code block:
#创建用户表</code>
<code>CREATE TABLE user (</code>
<code> id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',</code>
<code> name VARCHAR(30) DEFAULT NULL COMMENT '姓名',</code>
<code> age INT(11) DEFAULT NULL COMMENT '年龄',</code>
<code> email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',</code>
<code> manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',</code>
<code> create_time DATETIME DEFAULT NULL COMMENT '创建时间',</code>
<code> CONSTRAINT manager_fk FOREIGN KEY (manager_id)</code>
<code> REFERENCES user (id)</code>
<code>) ENGINE=INNODB CHARSET=UTF8;</code>
<code>#初始化数据:</code>
<code>INSERT INTO user (id, name, age, email, manager_id, create_time)</code>
<code>VALUES (1087982257332887553, '大boss', 40, '[email protected]', NULL, '2019-01-11 14:20:20'),</code>
<code> (1088248166370832385, '王天风', 25, '[email protected]', 1087982257332887553, '2019-02-05 11:12:22'),</code>
<code> (1088250446457389058, '李艺伟', 28, '[email protected]', 1088248166370832385, '2019-02-14 08:31:16'),</code>
<code> (1094590409767661570, '张雨琪', 31, '[email protected]', 1088248166370832385, '2019-01-14 09:15:15'),</code>
<code> (1094592041087729666, '刘红雨', 32, '[email protected]', 1088248166370832385, '2019-01-14 09:48:16');Case 1.1 – Query users aged 20, 21, 25, 26 and return only id and name (difficulty ★). The Java test method demonstrates using QueryWrapper with in and select:
/*</code>
<code> * 描述:例1.9 查询年龄为20、21、25、26的用户,且只返回id和name字段</code>
<code> * SQL语句:SELECT id,name FROM user WHERE age IN (20,21,25,26)</code>
<code> */</code>
<code>@Test</code>
<code>public void testSelectByQueryWrapper9() {</code>
<code> System.out.println(("----- 查询年龄为20、21、25、26的用户,且只返回id和name字段 ------"));</code>
<code> QueryWrapper<User> queryWrapper = new QueryWrapper<>();</code>
<code> queryWrapper.in("age", Arrays.asList(20,21,25,26)).select("id","name");</code>
<code> List<User> userList = userMapper.selectList(queryWrapper);</code>
<code> userList.forEach(System.out::println);</code>
<code>}Execution logs and result screenshots are included in the original article.
Case 1.2 – Query the same age set but return id , name , and manager_id (difficulty ★★). This example shows how to exclude unwanted columns using a lambda expression:
/*</code>
<code> * 描述:例1.10 查询年龄为20、21、25、26的用户,且只返回id、name、manager_id 字段</code>
<code> * SQL语句:SELECT id,name,manager_id FROM user WHERE age IN (20,21,25,26)</code>
<code> */</code>
<code>@Test</code>
<code>public void testSelectByQueryWrapper10() {</code>
<code> System.out.println(("----- 查询年龄为20、21、25、26的用户,且只返回id、name、manager_id 字段 ------"));</code>
<code> QueryWrapper<User> queryWrapper = new QueryWrapper<>();</code>
<code> queryWrapper.in("age", Arrays.asList(20,21,25,26))</code>
<code> .select(User.class, info -> !info.getColumn().equals("email") && !info.getColumn().equals("create_time"));</code>
<code> List<User> userList = userMapper.selectList(queryWrapper);
<code> userList.forEach(System.out::println);
<code>}Logs and result images are also provided.
The article concludes with links to other parts of the “每天玩转3分钟 MyBatis‑Plus” series and a call‑to‑action to follow the “悟空聊架构” public account for more resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
