MyBatis-Plus Tutorial: Creating User Table, Entity, Mapper, and Basic Query Methods
This tutorial demonstrates how to create a User table, define the corresponding entity and mapper classes, and perform basic MyBatis-Plus queries such as selectList, selectById, selectBatchIds, and selectByMap, including the required SQL scripts and Java code examples.
This article provides a step‑by‑step guide for using MyBatis‑Plus in a Java backend project. It covers creating a User table, defining the entity and mapper classes, and performing common query operations.
1. Create User Table
Table structure:
id
name
age
1
Jone
18
2
Jack
20
3
Tom
28
4
Sandy
21
5
Billie
24
Corresponding database schema 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)
);Corresponding data script:
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 Base Classes
2.1 User 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;
}2.2 UserMapper 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> {
}3. Query Methods
3.1 selectList – query all records
@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size()); // expects 6 records
userList.forEach(System.out::println);
}Corresponding SQL:
SELECT id,name,age,email FROM user3.2 selectById – query by primary key
@Test
public void testSelectById() {
User user = userMapper.selectById(2);
System.out.println(user);
}Corresponding SQL:
SELECT id,name,age,email FROM user WHERE id=?3.3 selectBatchIds – batch query by ID list
@Test
public void testSelectByIds() {
List<Long> idsList = Arrays.asList(2L, 4L, 6L);
List<User> userList = userMapper.selectBatchIds(idsList);
userList.forEach(System.out::println);
}Corresponding SQL:
SELECT id,name,age,email FROM user WHERE id IN ( ?, ?, ? )3.4 selectByMap – query by conditions
@Test
public void testSelectByMap() {
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);
}Corresponding SQL:
SELECT id,name,age,email FROM user WHERE name = ? AND age = ?Note: When using selectByMap, the field names in the condition map must exactly match the column names in the database; otherwise an error will occur.
For the full source code and additional details, see the GitHub repository https://github.com/Jackson0714/study-mybatis-plus.git . The article also includes author information and promotional messages.
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.
