java新增關(guān)聯(lián)的三張表,每張表要求都插入集合,代碼實現(xiàn)方式
一、表
1.第一張表
表名:goods_sell_attribute

2.第二張表
表名:goods_sell_attribute_value
以第一張表的name_id關(guān)聯(lián)

3.第三張表
表名:goods_sub_attribute_value
以第二張表的value_id關(guān)聯(lián)

二、實體類
1.第一張表的實體類
// lombok
@Data
public class GoodsSellAttributeDTO {
private Integer nameId;
private Integer valueId;
private Integer firstLevelId;
private String firstLevelName;
private Integer secondLevelId;
private String secondLevelName;
private Integer thirdLevelId;
private String thirdLevelName;
private String attributeName;
private String required;
// 放入第二張表的DTO
private List<GoodsSellAttributeValueDTO> goodsSellAttributeValueDTO;
2.第二張表的實體類
@Data
public class GoodsSellAttributeValueDTO {
private Integer nameId;
private String attributeValue;
private Integer valueId;
// 放入第三張表的DTO
private List<GoodsSubAttributeValueDTO> goodsSubAttributeValueDTO;
}
3.第三張表的是實體類
@Data
public class GoodsSubAttributeValueDTO {
private Integer valueId;
private String subValue;
}
三、在xml中sql的編寫
1.第一張表的新增sql
<!--第一張表的新增 -->
<insert id="insertGoodsSellAttributes" keyProperty="nameId" useGeneratedKeys="true" parameterType="com.dy.mallConfig.pojo.dto.GoodsSellAttributeDTO">
INSERT INTO goods_sell_attribute
(first_level_id,
second_level_id,
third_level_id,
first_level_name,
second_level_name,
third_level_name,
attribute_name,
required,
status)
VALUES
<foreach collection="list" separator="," item="item" index="index">
(#{item.firstLevelId},
#{item.secondLevelId},
#{item.thirdLevelId},
#{item.firstLevelName},
#{item.secondLevelName},
#{item.thirdLevelName},
#{item.attributeName},
#{item.required},
'1')
</foreach>
</insert>
2.第二張表的新增sql
<!-- 第二張表-->
<insert id="insertGoodsSellAttributeValues" keyProperty="valueId" useGeneratedKeys="true" parameterType="com.dy.mallConfig.pojo.dto.GoodsSellAttributeValueDTO">
INSERT INTO goods_sell_attribute_value
(name_id,attribute_value,status) VALUES
<foreach collection="list" index="goodsIndex" item="item" separator="),(" open="(" close=")" >
#{item.nameId},
#{item.attributeValue,jdbcType=VARCHAR},
'1'
</foreach>
</insert>
3.第三張表的新增sql
<!-- 第三張表-->
<insert id="insertGoodsSubAttributeValue" parameterType="com.dy.mallConfig.pojo.dto.GoodsSellAttributeValueDTO">
INSERT INTO goods_sub_attribute_value
(value_id,
sub_value,
status)
VALUES
<foreach collection="list" separator="," index="index" item="items">
<foreach collection="items.goodsSubAttributeValueDTO" item="items1" index="index" separator="),(" open="(" close=")">
#{items.valueId},
#{items1.subValue},
'1'
</foreach>
</foreach>
</insert>
四、mapper層
// 第一張表
int insertGoodsSellAttributes(List<GoodsSellAttributeDTO> goodsSellAttributeDTO);
// 第二張表
int insertGoodsSellAttributeValues(List<GoodsSellAttributeValueDTO> goodsSellAttributeValueDTO);
// 第三張表
int insertGoodsSubAttributeValue(List<GoodsSellAttributeValueDTO> goodsSellAttributeValueDTO);
五、service層
// 接口 OutputObject insertGoodsSellAttributes(List<GoodsSellAttributeDTO> goodsSellAttributeDTO);
六、serviceImpl層
@Service
public class GoodsSellAttributeServiceImpl implements GoodsSellAttributeService {
/**
* 添加Log日志
* @author zhushaojie
*/
private static final Logger LOGGER = LoggerFactory.getLogger(com.dy.mallConfig.service.impl.GoodsSellAttributeServiceImpl.class);
@Autowired
private GoodsSellAttributeMapper goodsSellAttributeMapper;
@Override
// 事務(wù)注解
@Transactional(rollbackFor = Exception.class)
public OutputObject insertGoodsSellAttributes(List<GoodsSellAttributeDTO> goodsSellAttributeDTO) {
try {
// 批量添加一級屬性
int i = goodsSellAttributeMapper.insertGoodsSellAttributes(goodsSellAttributeDTO);
// 將商品屬性和屬性子屬性中 抽離出來
List<GoodsSellAttributeValueDTO> goodsSellAttributeValueDTO = goodsSellAttributeDTO.stream()
.flatMap(o -> o.getGoodsSellAttributeValueDTO().stream().peek(s -> {
s.setNameId(o.getNameId());
})).collect(Collectors.toList());
if (i > 0 || goodsSellAttributeValueDTO.size()>0) {
// 一級屬性添加完后,回調(diào)一級id添加到二級當(dāng)中,進(jìn)行新增操作
int i1 = goodsSellAttributeMapper.insertGoodsSellAttributeValues(goodsSellAttributeValueDTO);
List<List<GoodsSubAttributeValueDTO>> goodsSubAttributeValueDTO= goodsSellAttributeValueDTO.stream().map(GoodsSellAttributeValueDTO::getGoodsSubAttributeValueDTO)
.collect(Collectors.toList());
if (i1 > 0 && goodsSubAttributeValueDTO.get(0)!=null) {
// 二級屬性添加完后,回調(diào)二級id添加到二級當(dāng)中,進(jìn)行新增操作
goodsSellAttributeMapper.insertGoodsSubAttributeValue(goodsSellAttributeValueDTO);
}
return new OutputObject(ReturnCode.SUCCESS, "添加成功", "添加成功");
}else {
return new OutputObject(ReturnCode.FAIL, "添加失敗", goodsSellAttributeDTO);
}
}catch (Exception e){
// 事務(wù)回滾
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return new OutputObject(ReturnCode.FAIL, "添加失敗", e.getMessage());
}
}
}
七、Controller
@RequestMapping("/insertGoodsSellAttributes")
@ResponseBody
public OutputObject insertGoodsSellAttributes(@RequestBody List<GoodsSellAttributeDTO> goodsSellAttributeDTO){
return goodsSellAttributeService.insertGoodsSellAttributes(goodsSellAttributeDTO);
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程,Thread類包含諸多操作線程的方法,非常重要,需要的朋友可以參考下2015-12-12
Java如何使用spire進(jìn)行word文檔的替換詳解
創(chuàng)作一份文案經(jīng)常會高頻率地使用某些詞匯,如地名、人名、人物職位等,若表述有誤,就需要整體撤換,下面這篇文章主要給大家介紹了關(guān)于Java如何使用spire進(jìn)行word文檔的替換的相關(guān)資料,需要的朋友可以參考下2023-01-01
MybatisPlus自定義Sql實現(xiàn)多表查詢的示例
這篇文章主要介紹了MybatisPlus自定義Sql實現(xiàn)多表查詢的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringBoot+MyBatis Plus實現(xiàn)update_time字段自動更新詳解
在 Spring Boot + MyBatis Plus 中實現(xiàn) update_time 字段自動更新,可通過 ??MyBatis Plus 的自動填充(Auto Fill)功能?? 完成,下面小編就來和大家詳細(xì)介紹一下吧2025-07-07
IDEA Debug啟動tomcat報60659端口占用錯誤的解決
工作中將開發(fā)工具由Eclipse轉(zhuǎn)為IntelliJ IDEA,在使用過程中遇到許多問題,其中60659端口占用錯誤對于不熟悉IDEA的開發(fā)者來說或許會比較頭痛,本文就來解決一下這個問題2018-11-11
一文詳解SpringBoot使用Kafka如何保證消息不丟失
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Kafka如何保證消息不丟失的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2025-01-01
SpringBoot?創(chuàng)建獲取yml里配置字段值
在Spring?Boot中通過@ConfigurationProperties綁定YML配置,創(chuàng)建Bean并提供訪問方法,實現(xiàn)根據(jù)配置字段動態(tài)處理業(yè)務(wù)邏輯,具有一定的參考價值,感興趣的可以了解一下2025-06-06

