《课次24:实体类与Mapper》学习笔记
一、教学目标
- 创建
User和News实体类,使用 Lombok 和 MyBatis-Plus 注解。 - 创建
UserMapper和NewsMapper,继承BaseMapper。 - 测试基础查询功能。
二、核心知识点
| 注解 | 作用 |
|---|---|
| @TableName | 指定实体类对应的数据库表名 |
| @TableId | 指定主键字段,IdType.AUTO 表示数据库自增主键 |
| @TableField(fill) | 指定字段的自动填充策略(如 INSERT、INSERT_UPDATE) |
| @TableLogic | 逻辑删除注解,删除时不会物理删除数据 |
| BaseMapper | MyBatis-Plus 提供的通用 Mapper,包含基本的 CRUD 方法 |
三、操作步骤
本课次是在课次23的工程基础上继续操作。
1. 创建实体 User
- 在
com.weitoutiao包下新建entity.User类 - 代码如下:
package com.weitoutiao.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
private String password;
private String role;
private Integer newsCount;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
}
2. 创建实体 News
- 同样在
entity包下创建News类 - 代码如下:
package com.weitoutiao.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName("news")
public class News {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userId;
private String title;
private String content;
private LocalDateTime publishTime;
private Integer viewCount;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableLogic
private Integer deleted;
}
3. 创建 Mapper 接口
- 在
com.weitoutiao下新建mapper包 - 创建
UserMapper接口:
package com.weitoutiao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weitoutiao.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
- 同样的方式创建
NewsMapper接口:
package com.weitoutiao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weitoutiao.entity.News;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface NewsMapper extends BaseMapper<News> {
}
4. 在启动类添加 @MapperScan
修改启动类 WeiTouTiaoSpringBootApplication,添加 @MapperScan 注解扫描 mapper 包:
package com.weitoutiao;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.weitoutiao.mapper")
public class WeiTouTiaoSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(WeiTouTiaoSpringBootApplication.class, args);
}
}
5. 编写测试类
- 在
test文件夹中的com.weitoutiao包下创建测试类UserMapperTest:
package com.weitoutiao;
import com.weitoutiao.entity.User;
import com.weitoutiao.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
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
void testSelect() {
List<User> users = userMapper.selectList(null); // 查询所有数据
users.forEach(System.out::println);
}
}
6. 运行测试
运行 testSelect 方法,控制台会打印出数据库中所有 user 信息。
四、笔记总结
| 步骤 | 核心内容 |
|---|---|
| 实体类 | User 和 News 使用 @Data + MyBatis-Plus 注解映射表结构 |
| Mapper 接口 | 继承 BaseMapper<T>,获得通用 CRUD 能力 |
| @MapperScan | 在启动类中扫描 mapper 包,注册所有 Mapper |
| 测试查询 | selectList(null) 查询全部数据,验证配置是否正确 |
本课次完成了数据访问层的基础搭建,通过 MyBatis-Plus 的 BaseMapper 大幅简化了 DAO 层的代码编写,后续可以直接使用这些 Mapper 进行数据库操作。 |
浙公网安备 33010602011771号