一、级联插入数据(无事务版本)
1.1、创建BannerDTO和BannerItemDTO
package io.github.talelin.latticy.dto;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
@Getter
@Setter
public class BannerDTO {
@NotBlank
@Length(min = 2, max = 20)
private String name;
@Length(min = 2, max = 30)
private String title;
private Integer version;
@Length(min = 2, max = 256)
private String img;
@Length(min = 2, max = 256)
private String description;
@NotNull
private List<BannerItemDTO> bannerItems;
}
package io.github.talelin.latticy.dto;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
@Data
public class BannerItemDTO {
// @NotBlank
@Length(min = 1, max = 255)
private String img;
@Length(min = 1, max = 255)
private String name;
// @NotNull
@Positive
private Integer type;
@NotNull
@Positive
private Integer bannerId;
@Length(min = 1, max = 50)
private String keyword;
}
1.2、创建BannerBO和BannerItemBO
package io.github.talelin.latticy.model;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import java.util.List;
@Getter
@Setter
@TableName("banner")
public class BannerDO {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String title;
private String description;
private String img;
@Version
private Integer version;
@JsonIgnore
private Date createTime;
@JsonIgnore
private Date updateTime;
/* @TableLogic注解参数
value = "" 默认的原值
delval = "" 删除后的值
@TableLogic(value="原值",delval="改值")
* */
@JsonIgnore
@TableLogic
private Date deleteTime;
//假设我们需要查询一个banner及其下面的item,首先对 Banner 实体类稍作修改,增加 bannerItemList 集合属性
@TableField(exist = false)
private List<BannerItemDO> items;
}
package io.github.talelin.latticy.model;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import java.util.Objects;
@TableName("banner_item")
@Getter
@Setter
@Data
public class BannerItemDO extends BaseModel {
private String name;
private String img;
private String keyword;
private Integer type;
private Integer bannerId;
}
1.3、创建BannerMapper和BannerItemMapper
package io.github.talelin.latticy.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import io.github.talelin.latticy.model.BannerDO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BannerMapper extends BaseMapper<BannerDO> {
}
package io.github.talelin.latticy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.github.talelin.latticy.model.BannerItemDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.List;
@Repository
public interface BannerItemMapper extends BaseMapper<BannerItemDO> {
}
1.4、创建BannerService
public void create(BannerDTO bannerDTO) {
BannerDO bannerDO = new BannerDO();
BeanUtils.copyProperties(bannerDTO, bannerDO);
bannerMapper.insert(bannerDO);
List<BannerItemDTO> bannerItemDTO = bannerDTO.getBannerItems();
bannerItemDTO.forEach(
s->{
BannerItemDO bannerItemDO = new BannerItemDO();
BeanUtils.copyProperties(s, bannerItemDO);
bannerItemDO.setBannerId(bannerDO.getId());
bannerItemMapper.insert(bannerItemDO);
}
);
}
1.5、BannerController
@PostMapping("/createBanner")
public CreatedVO create(@RequestBody @Validated BannerDTO dto) {
bannerService.create(dto);
return new CreatedVO();
}
级联新增成功。
二、级联插入数据(事务版本)
三、级联删除数据 (无事务版本)
3.1、创建BannerDTO和BannerItemDTO
3.2、创建BannerBO和BannerItemBO
3.3、创建BannerMapper和BannerItemMapper
3.4、创建BannerService
public void deleteWithItem(Integer id) {
BannerDO banner = bannerMapper.selectById(id);
if (banner == null) {
throw new NotFoundException(20000);
}
bannerMapper.deleteById(id);
LambdaQueryWrapper<BannerItemDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(BannerItemDO::getBannerId, id);
bannerItemMapper.delete(wrapper);
}
3.5、BannerController
@DeleteMapping("/deleteBannerWithItem/{id}")
public DeletedVO deleteWithItem(@PathVariable @Positive Integer id) {
bannerService.deleteWithItem(id);
return new DeletedVO();
}