mybatis-plus + springboot 多对多实例

在MyBatis Plus中,自动填充数据是一种非常实用的功能,它可以自动地为一些字段设置默认值,比如创建时间和更新时间。对于多对多关系来说,虽然自动填充主要针对单一实体的字段,但在某些情况下,你可能也需要在创建或更新实体时自动填充与之相关的多对多关系。

下面我将展示如何在实体类中使用MyBatis Plus的自动填充功能,并且给出一个简单的多对多关系示例。

实体类定义

假设我们有两个实体类:Student 和 Course,它们之间存在多对多关系。我们将定义这两个实体类,并为它们添加自动填充字段。

java 复制代码
// Student.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("student")
public class Student {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    
    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    
    @TableField(exist = false)
    private List<Course> courses;
}

// Course.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("course")
public class Course {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    
    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    
    @TableField(exist = false)
    private List<Student> students;
}

这里我们使用了@TableField(fill = FieldFill.INSERT)和@TableField(fill = FieldFill.INSERT_UPDATE)注解来指定哪些字段应该在插入时和更新时自动填充。

自动填充处理器

为了使自动填充生效,你需要定义一个MetaObjectHandler实现类,并在Spring容器中注册它。

java 复制代码
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}

这里我们重写了insertFill和updateFill方法,当实体插入或更新时,这些方法会被自动调用以填充指定的字段。

多对多关系处理

对于多对多关系的处理,你可以选择以下几种方式之一:

使用中间表:定义一个中间表来存储多对多关系,然后在插入或更新实体时,手动处理中间表的插入或更新。

使用注解:如果MyBatis Plus支持多对多关系的注解处理,那么你可以使用相应的注解来简化多对多关系的处理。

由于MyBatis Plus本身并不直接支持多对多关系的注解处理,这里我们采用第一种方式,即定义中间表并手动处理。

中间表定义

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("student_course")
public class StudentCourse {
    @TableId(type = IdType.AUTO)
    private Long id;
    private Long studentId;
    private Long courseId;
    
    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
}

Mapper接口定义

java 复制代码
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    // 定义自定义方法
}

@Mapper
public interface CourseMapper extends BaseMapper<Course> {
    // 定义自定义方法
}

@Mapper
public interface StudentCourseMapper extends BaseMapper<StudentCourse> {
    // 定义自定义方法
}

Service层定义

java 复制代码
@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;

    public void addStudentWithCourses(Student student, List<Long> courseIds) {
        studentMapper.insert(student); // 插入学生
        
        List<StudentCourse> studentCourses = courseIds.stream()
            .map(courseId -> new StudentCourse(null, student.getId(), courseId))
            .collect(Collectors.toList());
        
        studentCourseMapper.insertBatch(studentCourses); // 插入多对多关系
    }
}

Controller定义

java 复制代码
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping
    public void addStudentWithCourses(@RequestBody Student student, @RequestParam List<Long> courseIds) {
        studentService.addStudentWithCourses(student, courseIds);
    }
}

总结

上述示例展示了如何在实体类中使用MyBatis Plus的自动填充功能。

对于多对多关系,我们定义了一个中间表,并在Service层手动处理了多对多关系的插入。

如果有更复杂的多对多关系需求,你可以根据具体情况进行调整。

相关推荐
W.A委员会1 小时前
JS原型链详解
开发语言·javascript·原型模式
止语Lab1 小时前
Go并发编程实战:Channel 还是 Mutex?一个场景驱动的选择框架
开发语言·后端·golang
她说彩礼65万2 小时前
C# 实现简单的日志打印
开发语言·javascript·c#
绿浪19842 小时前
c# 中结构体 的定义字符串字段(性能优化)
开发语言·c#
HoneyMoose2 小时前
Jenkins Cloudflare 部署提示错误
java·servlet·jenkins
阿丰资源2 小时前
基于SpringBoot的物流信息管理系统设计与实现(附资料)
java·spring boot·后端
Predestination王瀞潞2 小时前
Java EE3-我独自整合(第四章:Spring bean标签的常见配置)
java·spring·java-ee
overmind2 小时前
oeasy Python 121[专业选修]列表_多维列表运算_列表相加_列表相乘
java·windows·python
资深数据库专家2 小时前
总账EBS 应用服务器1 的监控分析
java·网络·数据库
房开民2 小时前
可变参数模板
java·开发语言·算法