多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 1.`@Enumerated` 该注解可以让一个枚举类型变量作为数据库中的一个字段。 ```java package jakarta.persistence; public @interface Enumerated { //EnumType.ORDINAL 将枚举索引作为值插入到数据库中 //EnumType.STRING 将枚举名称作为值插入到数据库中 EnumType value() default EnumType.ORDINAL; } ``` **1. 创建一个枚举类型** ```java public enum GenderEnum { 男生, 女生 } ``` **2. 将该枚举类作为数据库中的一个字段** ```java public class Student implements Serializable { @Enumerated(value = EnumType.ORDINAL) @Column(name = "gender") private GenderEnum gender; } ``` **3. 在插入数据时** :-: ![](https://img.kancloud.cn/4f/5f/4f5fac96d71e72c997c50d7ee997fb03_1501x186.png) EnumType.ORDINAL 插入的是枚举的索引 <br/> :-: ![](https://img.kancloud.cn/8c/7d/8c7dbad1270d23e9afca2c5df7c5c747_1528x190.png) EnumType.STRING 插入的是枚举名 <br/> # 2. `@Fetch` 多表查询时使用注解`@Fetch`可以决定查询方式,如果不使用该注解,默认查询方式应该为`FetchMode.SELECT`。 ```java package org.hibernate.annotations public @interface Fetch { //FetchMode.SELECT 先查主表,在用主表ID去查从表,共查两次 //FetchMode.JOIN left join查询,只查一次 //FetchMode.SUBSELECT 子查询 FetchMode value(); } ``` ```java public class Student implements Serializable { @Fetch(value = FetchMode.SELECT) @OneToMany(mappedBy = "student") private List<Score> scoreList; } ``` <br/> # 3.`@MappedSuperclass` ``` jakarta.persistence.MappedSuperclass 注解可以将一些公共字段抽取出来封装在一个父类中 ``` <br/> **1. 将公共字段抽取到一个抽象父类中** ```java @Getter @Setter @MappedSuperclass public abstract class BaseEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "create_time") private Date createTime; @Column(name = "update_time") private Date updateTime; } ``` **2. 其他实体类继承抽象父类** ```java public class Student extends BaseEntity { @Column(name = "stu_name") private String name; @Column(name = "age") private Integer age; ... } ``` ![](https://img.kancloud.cn/05/f9/05f9f305a3a714d62a5b6054cdf83638_1686x199.png) <br/> # 4. `@PrePersist/@PreUpdate` ```java @Getter @Setter @MappedSuperclass public abstract class BaseEntity implements Serializable { ... @Column(name = "create_time") private Date createTime; @Column(name = "update_time") private Date updateTime; /** * 在新增前对createTime、updateTime赋值 */ @PrePersist public void beforeAdd() { System.out.println("[beforeAdd]"); SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); try { if (this.createTime == null) { this.createTime = dFormat.parse(dFormat.format(now)); } if (this.updateTime == null) { this.updateTime = dFormat.parse(dFormat.format(now)); } } catch (ParseException ex) { } } /** * 在更新前对updateTime赋值 */ @PreUpdate public void beforeUpdate() { System.out.println("[beforeUpdate]"); if (this.updateTime != null) { return; } SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { this.updateTime = dFormat.parse(dFormat.format(new Date())); } catch (ParseException ex) { } } } ``` <br/> # 5. `@DynamicUpdate/@DynamicInsert` ``` org.hibernate.annotations.DynamicInsert 生成动态的 insert 语句 org.hibernate.annotations.DynamicUpdate 生成动态的 update 语句 ``` ```java ... @DynamicUpdate @DynamicInsert public class Student { @Column(name = "stu_name") private String name; @Column(name = "age") private Integer age; ... } ``` 这里只新增/更新 age 一个字段,sql 语句如下: ```sql -- 不标记注解 @DynamicInsert insert into tb_student (age,stu_name) values (?,?) -- 标记注解 @DynamicInsert, insert into tb_student (age) values (?) -- 不标记注解 @DynamicUpdate update tb_student set age=?,stu_name=? where id=? -- 标记注解 @DynamicUpdate update tb_student set age=? where id=? ``` <br/> >[info]放在父类中无效。 ```java @Getter @Setter @DynamicUpdate @DynamicInsert @MappedSuperclass public abstract class BaseEntity implements Serializable { ... } public class Student extends BaseEntity { @Column(name = "stu_name") private String name; } ```