@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { /** * 主键 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; /** * 广告名称 */ @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; /** * 广告位置ID */ @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; /** * 广告图片ID */ @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; /** * 开始日期 */ @Column(name = "start_date") @ApiModelProperty(value = "开始日期") @NotNull(message = "开始日期不可为空", groups = {AddGroup.class}) private Date startDate; /** * 结束日期 */ @Column(name = "end_date") @ApiModelProperty(value = "结束日期") @NotNull(message = "结束日期不可为空", groups = {AddGroup.class}) private Date endDate; /** * 广告链接 */ @ApiModelProperty(value = "广告链接") @NotBlank(message = "广告链接不可为空", groups = {AddGroup.class}) private String link; /** * 广告状态:0:未启用,1:已启用,2:已结束 */ @ApiModelProperty(value = "广告状态") @NotNull(message = "广告状态不可为空", groups = {UpdateGroup.class}) private Integer status; /** * 创建时间 */ @Column(name = "create_time") @ApiModelProperty(value = "创建时间") private Date createTime; /** * 更新时间 */ @Column(name = "update_time") @ApiModelProperty(value = "更新时间") private Date updateTime; },这个类进行添加和修改时,因为有大量重复代码,如何简化代码并避免并发问题
时间: 2024-02-24 13:56:37 浏览: 83
Ambiguous mapping. Cannot map *** method 报错解决
5星 · 资源好评率100%
可以使用Lombok提供的@Builder注解简化代码,并且使用@Transactional注解来实现数据库的事务控制,避免并发问题。简化后的代码如下:
```
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "advert")
public class Advert {
/** 主键 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "广告唯一id")
private Integer id;
/** 广告名称 */
@ApiModelProperty(value = "广告名称")
@NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class})
private String name;
/** 广告位置ID */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "position_id")
private AdPosition position;
/** 广告图片ID */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "media_id")
private MediaFile media;
/** 开始日期 */
@Column(name = "start_date")
@ApiModelProperty(value = "开始日期")
@NotNull(message = "开始日期不可为空", groups = {AddGroup.class})
private Date startDate;
/** 结束日期 */
@Column(name = "end_date")
@ApiModelProperty(value = "结束日期")
@NotNull(message = "结束日期不可为空", groups = {AddGroup.class})
private Date endDate;
/** 广告链接 */
@ApiModelProperty(value = "广告链接")
@NotBlank(message = "广告链接不可为空", groups = {AddGroup.class})
private String link;
/** 广告状态:0:未启用,1:已启用,2:已结束 */
@ApiModelProperty(value = "广告状态")
@NotNull(message = "广告状态不可为空", groups = {UpdateGroup.class})
private Integer status;
/** 创建时间 */
@Column(name = "create_time")
@ApiModelProperty(value = "创建时间")
private Date createTime;
/** 更新时间 */
@Column(name = "update_time")
@ApiModelProperty(value = "更新时间")
private Date updateTime;
/**
* 保存或更新广告
*/
@Transactional(rollbackFor = Exception.class)
public void saveOrUpdate() {
AdvertRepository advertRepository = SpringContextUtil.getBean(AdvertRepository.class);
if (this.getId() == null) {
// 新增广告
this.setCreateTime(new Date());
advertRepository.save(this);
} else {
// 更新广告
Advert oldAdvert = advertRepository.findById(this.getId()).orElse(null);
if (oldAdvert != null) {
BeanUtils.copyProperties(this, oldAdvert, "id", "createTime");
oldAdvert.setUpdateTime(new Date());
advertRepository.save(oldAdvert);
}
}
}
}
```
这里使用了Spring的@Transactional注解来实现事务控制,同时使用了Spring的工具类SpringContextUtil来获取AdvertRepository的实例。这样,我们就可以在Advert实体类中调用saveOrUpdate方法来进行保存或更新操作,避免了重复代码的出现,并且确保了并发安全。
阅读全文