注册网站地址,湘潭市网站建设科技有限公司,网站在百度的图标显示不正常,泉州网站制作推广在MyBatis Plus中#xff0c;自动填充数据是一种非常实用的功能#xff0c;它可以自动地为一些字段设置默认值#xff0c;比如创建时间和更新时间。对于多对多关系来说#xff0c;虽然自动填充主要针对单一实体的字段#xff0c;但在某些情况下#xff0c;你可能也需要在…在MyBatis Plus中自动填充数据是一种非常实用的功能它可以自动地为一些字段设置默认值比如创建时间和更新时间。对于多对多关系来说虽然自动填充主要针对单一实体的字段但在某些情况下你可能也需要在创建或更新实体时自动填充与之相关的多对多关系。 下面我将展示如何在实体类中使用MyBatis Plus的自动填充功能并且给出一个简单的多对多关系示例。
实体类定义 假设我们有两个实体类Student 和 Course它们之间存在多对多关系。我们将定义这两个实体类并为它们添加自动填充字段。
// 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 ListCourse 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 ListStudent students;
}
这里我们使用了TableField(fill FieldFill.INSERT)和TableField(fill FieldFill.INSERT_UPDATE)注解来指定哪些字段应该在插入时和更新时自动填充。 自动填充处理器 为了使自动填充生效你需要定义一个MetaObjectHandler实现类并在Spring容器中注册它。
Slf4j
Component
public class MyMetaObjectHandler implements MetaObjectHandler {Overridepublic void insertFill(MetaObject metaObject) {this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now());this.strictInsertFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now());}Overridepublic void updateFill(MetaObject metaObject) {this.strictUpdateFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now());}
}这里我们重写了insertFill和updateFill方法当实体插入或更新时这些方法会被自动调用以填充指定的字段。 多对多关系处理 对于多对多关系的处理你可以选择以下几种方式之一 使用中间表定义一个中间表来存储多对多关系然后在插入或更新实体时手动处理中间表的插入或更新。 使用注解如果MyBatis Plus支持多对多关系的注解处理那么你可以使用相应的注解来简化多对多关系的处理。 由于MyBatis Plus本身并不直接支持多对多关系的注解处理这里我们采用第一种方式即定义中间表并手动处理。 中间表定义 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接口定义
Mapper
public interface StudentMapper extends BaseMapperStudent {// 定义自定义方法
}Mapper
public interface CourseMapper extends BaseMapperCourse {// 定义自定义方法
}Mapper
public interface StudentCourseMapper extends BaseMapperStudentCourse {// 定义自定义方法
}Service层定义
Service
public class StudentService {Autowiredprivate StudentMapper studentMapper;Autowiredprivate StudentCourseMapper studentCourseMapper;public void addStudentWithCourses(Student student, ListLong courseIds) {studentMapper.insert(student); // 插入学生ListStudentCourse studentCourses courseIds.stream().map(courseId - new StudentCourse(null, student.getId(), courseId)).collect(Collectors.toList());studentCourseMapper.insertBatch(studentCourses); // 插入多对多关系}
}Controller定义
RestController
RequestMapping(/students)
public class StudentController {Autowiredprivate StudentService studentService;PostMappingpublic void addStudentWithCourses(RequestBody Student student, RequestParam ListLong courseIds) {studentService.addStudentWithCourses(student, courseIds);}
}总结 上述示例展示了如何在实体类中使用MyBatis Plus的自动填充功能。 对于多对多关系我们定义了一个中间表并在Service层手动处理了多对多关系的插入。 如果有更复杂的多对多关系需求你可以根据具体情况进行调整。