前言
在上期内容中,我们详细讲解了 MyBatis Plus 的基础配置,包括如何配置分页插件、乐观锁插件以及性能分析插件。我们还介绍了如何设置项目的日志输出,这些配置极大地提高了项目的开发效率和调试便利性。通过这些配置,我们已经为项目的进一步开发奠定了坚实的基础。
本期内容将继续深入,重点介绍如何创建实体类和 Mapper 接口。实体类用于映射数据库表结构,而 Mapper 接口则提供了与数据库交互的方法。通过这些步骤,你将学会如何将数据库表结构映射到 Java 对象,并实现对数据库的基本操作。
一、创建实体类
实体类是数据库表在 Java 代码中的映射。MyBatis Plus 提供了便捷的注解来简化实体类的定义。
-
创建实体类目录:
- 在
src/main/java/com/example/mybatisplusdemo/entity
目录下创建一个包entity
,并在该包下创建User
类:package com.example.mybatisplusdemo.entity; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("user") public class User { @TableId private Long id; private String name; private Integer age; private String email; }
- 在
-
注解说明:
@TableName
:指定数据库表名,如果表名与类名一致,可以省略。@TableId
:指定表的主键。
二、创建 Mapper 接口
Mapper 接口用于定义数据访问层的操作。MyBatis Plus 提供了 BaseMapper
接口,封装了常见的 CRUD 操作。
-
创建 Mapper 接口目录:
- 在
src/main/java/com/example/mybatisplusdemo/mapper
目录下创建一个包mapper
,并在该包下创建UserMapper
接口:package com.example.mybatisplusdemo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mybatisplusdemo.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { }
- 在
-
注解说明:
@Mapper
:标记该接口为 MyBatis 的 Mapper 接口,Spring 会自动扫描并注册。
三、配置 MyBatis Plus 的 Mapper 扫描
为了让 Spring Boot 自动扫描和识别 Mapper 接口,我们需要在启动类中配置 Mapper 扫描路径。
-
修改启动类:
- 打开
MybatisPlusDemoApplication
类,添加@MapperScan
注解:package com.example.mybatisplusdemo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mybatisplusdemo.mapper") public class MybatisPlusDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisPlusDemoApplication.class, args); } }
- 打开
-
注解说明:
@MapperScan
:指定 Mapper 接口的扫描包路径。
四、测试实体类和 Mapper 接口
为了确保我们的实体类和 Mapper 接口工作正常,我们需要进行一些基本的测试。
-
创建测试数据:
- 在 MySQL 数据库中创建
user
表:CREATE TABLE user ( id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, age INT NOT NULL, email VARCHAR(50), PRIMARY KEY (id) );
- 在 MySQL 数据库中创建
-
测试 Mapper 接口:
- 创建单元测试类
UserMapperTest
,测试 CRUD 操作:package com.example.mybatisplusdemo; import com.example.mybatisplusdemo.entity.User; import com.example.mybatisplusdemo.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void testInsert() { User user = new User(); user.setName("John Doe"); user.setAge(25); user.setEmail("john.doe@example.com"); int result = userMapper.insert(user); assert result == 1; } @Test public void testSelect() { List<User> users = userMapper.selectList(null); assert users.size() > 0; } @Test public void testUpdate() { User user = userMapper.selectById(1L); user.setAge(26); int result = userMapper.updateById(user); assert result == 1; } @Test public void testDelete() { int result = userMapper.deleteById(1L); assert result == 1; } }
- 创建单元测试类
五、总结
通过本篇文章,你已经学会了如何创建实体类和 Mapper 接口,并配置 MyBatis Plus 的 Mapper 扫描。你还测试了基本的 CRUD 操作,确保了实体类和 Mapper 接口的正确性。在接下来的文章中,我们将进一步探讨 MyBatis Plus 的服务类与控制器的创建。
下期预告
在下一期内容中,我们将深入讲解如何创建服务类和控制器。这些服务类将处理业务逻辑,而控制器则负责处理 HTTP 请求,调用服务类的方法,并返回响应结果。通过这些步骤,你将学会如何构建一个完整的 Web 应用程序。敬请期待!
通过本系列教程的学习,你将系统地掌握 MyBatis Plus 的各项功能,从基础到高级,从理论到实践,全面提升你的开发技能。希望你在学习过程中能够有所收获,并应用到实际项目中。让我们继续这段学习之旅吧!