@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; },对这个类的对象进行添加和修改时,因为在server层有大量重复代码,如何简化代码并避免并发问题
时间: 2024-02-24 16:57:47 浏览: 78
Ambiguous mapping. Cannot map *** method 报错解决
5星 · 资源好评率100%
可以使用Spring Data JPA中的`CrudRepository`或`JpaRepository`接口来简化代码,并避免并发问题。
首先,定义一个通用的BaseService类,其中包含常用的CRUD操作:
```
public abstract class BaseService<T, ID extends Serializable> {
@Autowired
protected JpaRepository<T, ID> repository;
public T save(T entity) {
return repository.save(entity);
}
public T findById(ID id) {
return repository.findById(id).orElse(null);
}
public void delete(T entity) {
repository.delete(entity);
}
public void deleteById(ID id) {
repository.deleteById(id);
}
public List<T> findAll() {
return repository.findAll();
}
public Page<T> findAll(Pageable pageable) {
return repository.findAll(pageable);
}
}
```
然后,定义一个AdvertService类,继承BaseService,并添加一些特定的业务方法:
```
@Service
public class AdvertService extends BaseService<Advert, Integer> {
@Autowired
private AdvertRepository advertRepository;
public List<Advert> findByStatus(Integer status) {
return advertRepository.findByStatus(status);
}
public List<Advert> findByPositionId(Integer positionId) {
return advertRepository.findByPositionId(positionId);
}
// add other business methods here
}
```
在AdvertRepository接口中定义需要的查询方法:
```
public interface AdvertRepository extends JpaRepository<Advert, Integer> {
List<Advert> findByStatus(Integer status);
List<Advert> findByPositionId(Integer positionId);
// add other query methods here
}
```
最后,在controller层中使用AdvertService来进行增删改查操作:
```
@RestController
@RequestMapping("/advert")
public class AdvertController {
@Autowired
private AdvertService advertService;
@PostMapping("/add")
public Result add(@RequestBody Advert advert) {
advertService.save(advert);
return Result.success();
}
@PostMapping("/update")
public Result update(@RequestBody Advert advert) {
advertService.save(advert);
return Result.success();
}
// add other controller methods here
}
```
这样做可以避免重复的代码,同时也可以有效地避免并发问题。由于Spring Data JPA会自动处理事务,因此在进行增删改操作时,可以确保数据的一致性。
阅读全文