滚雪球学MyBatis-Plus(05):创建实体类与Mapper接口

前言

在上期内容中,我们详细讲解了 MyBatis Plus 的基础配置,包括如何配置分页插件、乐观锁插件以及性能分析插件。我们还介绍了如何设置项目的日志输出,这些配置极大地提高了项目的开发效率和调试便利性。通过这些配置,我们已经为项目的进一步开发奠定了坚实的基础。

本期内容将继续深入,重点介绍如何创建实体类和 Mapper 接口。实体类用于映射数据库表结构,而 Mapper 接口则提供了与数据库交互的方法。通过这些步骤,你将学会如何将数据库表结构映射到 Java 对象,并实现对数据库的基本操作。

一、创建实体类

实体类是数据库表在 Java 代码中的映射。MyBatis Plus 提供了便捷的注解来简化实体类的定义。

  1. 创建实体类目录

    • 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;
      }
      
  2. 注解说明

    • @TableName:指定数据库表名,如果表名与类名一致,可以省略。
    • @TableId:指定表的主键。
二、创建 Mapper 接口

Mapper 接口用于定义数据访问层的操作。MyBatis Plus 提供了 BaseMapper 接口,封装了常见的 CRUD 操作。

  1. 创建 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> {
      }
      
  2. 注解说明

    • @Mapper:标记该接口为 MyBatis 的 Mapper 接口,Spring 会自动扫描并注册。
三、配置 MyBatis Plus 的 Mapper 扫描

为了让 Spring Boot 自动扫描和识别 Mapper 接口,我们需要在启动类中配置 Mapper 扫描路径。

  1. 修改启动类

    • 打开 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);
          }
      }
      
  2. 注解说明

    • @MapperScan:指定 Mapper 接口的扫描包路径。
四、测试实体类和 Mapper 接口

为了确保我们的实体类和 Mapper 接口工作正常,我们需要进行一些基本的测试。

  1. 创建测试数据

    • 在 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)
      );
      
  2. 测试 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 的各项功能,从基础到高级,从理论到实践,全面提升你的开发技能。希望你在学习过程中能够有所收获,并应用到实际项目中。让我们继续这段学习之旅吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug菌¹

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值