MyBatis-Plus Essentials: Entity Mapping, CRUD, and Conditional Queries
Adding MyBatis-Plus Dependency
Include the starter in your Maven project, ensuring version compatibility with your Spring Boot and MyBatis release:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
Defining Entities and Maping Rules
Create a domain class and use Lombok to reduce boilerplate. The framework automatically applies camel‑case to underscore conversion for table and column names. You can enforce this with the following YAML setting:
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
Annotation Overrides
@TableNamespecifies the database table name.@TableFieldmaps a property to a column (value), marks it as non‑persistent (exist = false), or excludes it from SELECT queries (select = false).@TableIddesignates the primary key.
Example:
@Data
@TableName("employee")
public class Employee {
@TableId
private Long id;
private String loginName;
@TableField("pwd_hash")
private String passwordHash;
private Integer experience;
private String contactEmail;
@TableField(exist = false)
private boolean workingRemotely;
}
Here Employee maps to table employee, loginName becomes login_name, and passwordHash maps to the explicit column pwd_hash. The workingRemotely field is ignored during persistence.
Mapper Interface and Base Methods
Define a mapper by extending BaseMapper:
@Mapper
public interface EmployeeDao extends BaseMapper<Employee> {}
This gives you ready‑to‑use CRUD operations:
int insert(T entity)— inserts a record; nullable properties become null in the database.int updateById(T entity)— updates by primary key.T selectById(Serializable id)— retrieves a single record.List<T> selectBatchIds(Collection<? extends Serializable> ids)— batch fetch by IDs.List<T> selectByMap(Map<String, Object> map)— query by column-value pairs.List<T> selectList(Wrapper<T> wrapper)— conditional query.Integer selectCount(Wrapper<T> wrapper)— count records.IPage<T> selectPage(IPage<T> page, Wrapper<T> wrapper)— paginated query.List<Map<String, Object>> selectMaps(Wrapper<T> wrapper)— returns result as maps.
Configuring Logging and Banner
Enable SQL statement logging in application.yml:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false
To suppress excessive logging from other components, place an empty logback.xml in src/main/resources:
<?xml version="1.0" encoding="UTF-8"?>
<configuration></configuration>
Building Queries with Wrappers
QueryWrapper
Use equality, range, and pattern-matching methods:
QueryWrapper<Employee> qw = new QueryWrapper<>();
qw.eq("experience", 5)
.like("contact_email", "@company.com");
List<Employee> result = employeeDao.selectList(qw);
LambdaQueryWrapper
Avoid string‑based column names for type safety:
LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
lqw.ge(Employee::getExperience, 3)
.le(Employee::getExperience, 10)
.eq(Employee::getWorkingRemotely, true);
List<Employee> employees = employeeDao.selectList(lqw);
Multiple conditions are combined with AND by default. Use or() to switch to OR:
lqw.lt(Employee::getExperience, 2)
.or()
.gt(Employee::getExperience, 12);
Ordering can be added with orderByAsc / orderByDesc. For pagination, pass a Page object together with a wrapper to selectPage.