-
写一个包含主键类并实例化
-
在实体类中通过
@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{
/**
* @description 主键
*/
@Id
@Column(name = "id", nullable = false)
private Integer id;
/**
* @description 主键
*/
@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;
}
/**
* @description 主键
*/
@Id
@Column(name = "id", nullable = false)
private Integer id;
/**
* @description 主键
*/
@Id
@Column(name = "user_id", nullable = false)
private Integer userId;
}