当前位置: 首页 > news >正文

柳州网站建设33站长域名查询

柳州网站建设33,站长域名查询,找人做网站一般要多少钱,做网站违法嘛在MyBatis Plus中,自动填充数据是一种非常实用的功能,它可以自动地为一些字段设置默认值,比如创建时间和更新时间。对于多对多关系来说,虽然自动填充主要针对单一实体的字段,但在某些情况下,你可能也需要在…

在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 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容器中注册它。

@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 BaseMapper<Student> {// 定义自定义方法
}@Mapper
public interface CourseMapper extends BaseMapper<Course> {// 定义自定义方法
}@Mapper
public interface StudentCourseMapper extends BaseMapper<StudentCourse> {// 定义自定义方法
}

Service层定义

@Service
public class StudentService {@Autowiredprivate StudentMapper studentMapper;@Autowiredprivate 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定义

@RestController
@RequestMapping("/students")
public class StudentController {@Autowiredprivate StudentService studentService;@PostMappingpublic void addStudentWithCourses(@RequestBody Student student, @RequestParam List<Long> courseIds) {studentService.addStudentWithCourses(student, courseIds);}
}

总结
上述示例展示了如何在实体类中使用MyBatis Plus的自动填充功能。
对于多对多关系,我们定义了一个中间表,并在Service层手动处理了多对多关系的插入。
如果有更复杂的多对多关系需求,你可以根据具体情况进行调整。

http://www.15wanjia.com/news/4331.html

相关文章:

  • 关于做问卷星网站的毕业论文百度账号申请注册
  • 备案用个人单页网站北京网络推广公司排行
  • 网站seo关键词设置媒介
  • 才做的网站怎么搜不到百度代运营
  • 扫二维码直接进入网站 怎么做百度招聘
  • 网站开发的技术类型有哪些重庆网站设计
  • 南宁市建设厅网站搭建一个网站需要什么
  • 安徽省政府集约化网站建设招标推广渠道
  • 网站建设外包公司管理方法百度推广引流
  • 建设直播网站软件成都网站seo公司
  • 护肤品网页设计图片东莞seo黑帽培训
  • 榆林做网站电话品牌营销策略
  • 婚恋网站翻译可以做吗英文谷歌seo
  • 哪里可以接网站开发项目做网站推广宣传语
  • 精准引流怎么推广google关键词排名优化
  • 互动案例的网站公司推广渠道有哪些
  • 手机怎么做钓鱼网站如何交换友情链接
  • 灯光设计网站推荐国外seo工具
  • 产品设计培训中心重庆好的seo平台
  • 网站开发交流今日头条热搜榜
  • 呼和浩特网站建设费用seo sem论坛
  • 龙岩做网站公司哪家好线上怎么做推广和宣传
  • 如何简单制作自己的网站软件推广怎么赚钱
  • 网站开发有什么注意的网络营销岗位描述的内容
  • 做购物车网站多少钱关键词优化排名用哪些软件比较好
  • 网站快速排名服务商广州网站建设方案优化
  • 高端网站制作成都网站快速开发
  • 河北住房和城乡建设厅网站电话是多少营销技巧培训
  • 做网站jsp和php百度手机浏览器下载
  • 重庆微信网站建设多少钱网站seo优化免费