MyBatis-Plus Tutorial: Creating User Table, Entity, Mapper, and Query Methods
This article walks through building a MySQL user table, defining the corresponding Java entity and MyBatis‑Plus mapper, and demonstrates basic query operations such as selectList, selectById, selectBatchIds, and selectByMap with full code examples and the generated SQL statements.
This guide shows how to use MyBatis‑Plus for basic CRUD operations in a Java backend project.
1. Create the User table
The table schema is defined with the following SQL script:
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)
);Sample data is inserted with:
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. Create the Java entity
package com.example.demo.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}3. Create the mapper interface
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
public interface UserMapper extends BaseMapper<User> {
}4. Query methods
4.1 selectList – retrieve all records
@Test
public void testSelect() {
System.out.println("----- 单表查询所有记录------");
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size());
userList.forEach(System.out::println);
}Generated SQL:
SELECT id,name,age,email FROM user4.2 selectById – retrieve a single record by primary key
@Test
public void testSelectById() {
System.out.println("----- 单表根据主键id查询单条记录 ------");
User user = userMapper.selectById(2);
System.out.println(user);
}Generated SQL:
SELECT id,name,age,email FROM user WHERE id=?4.3 selectBatchIds – batch query by a list of IDs
@Test
public void testSelectByIds() {
System.out.println("----- 单表根据 id list 批量查询 ------");
List<Long> idsList = Arrays.asList(2L,4L,6L);
List<User> userList = userMapper.selectBatchIds(idsList);
userList.forEach(System.out::println);
}Generated SQL:
SELECT id,name,age,email FROM user WHERE id IN ( ?, ?, ? )4.4 selectByMap – conditional query
@Test
public void testSelectByMap() {
System.out.println("----- 单表根据条件查询 ------");
Map<String, Object> conditions = new HashMap<>();
conditions.put("name", "Jack");
conditions.put("age", 20);
List<User> userList = userMapper.selectByMap(conditions);
userList.forEach(System.out::println);
}Generated SQL:
SELECT id,name,age,email FROM user WHERE name = ? AND age = ?The article also includes screenshots, links to related tutorials, and a brief author note, but the core instructional content focuses on setting up the database schema, Java entity, mapper, and demonstrating the four basic query methods provided by MyBatis‑Plus.
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.
