-
写一个包含主键类并实例化
-
在实体类中通过 @IdClass 添加主键类
-
实体类中使用 @Id 加在需要设置成主键的字段上
@Data
public class UserPrimaryKey implements Serializable {
private Integer id;
private Integer userId;
}
实体类
@Data
@Entity
@Table(name = "sys_user")
@IdClass(UserPrimaryKey.class)
@EqualsAndHashCode(callSuper = true)
@DynamicUpdate
public class SysUserEntity{
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Id
@Column(name = "user_id", nullable = false)
private Integer userId;
}
如果你不想单独写一个主键类,那你可以这样写
@Data
@Entity
@Table(name = "sys_user")
@IdClass(SysUserEntity.UserPrimaryKey.class)
@EqualsAndHashCode(callSuper = true)
@DynamicUpdate
public class SysUserEntity{
@Data
static class UserPrimaryKey implements Serializable {
private Integer id;
private Integer userId;
}
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Id
@Column(name = "user_id", nullable = false)
private Integer userId;
}



