java新增關聯(lián)的三張表,每張表要求都插入集合,代碼實現(xiàn)方式
更新時間:2023年12月13日 09:16:04 作者:程序猿不禿頭
這篇文章主要介紹了java新增關聯(lián)的三張表,每張表要求都插入集合,代碼實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
一、表
1.第一張表
表名:goods_sell_attribute
2.第二張表
表名:goods_sell_attribute_value
以第一張表的name_id關聯(lián)
3.第三張表
表名:goods_sub_attribute_value
以第二張表的value_id關聯(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 // 事務注解 @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添加到二級當中,進行新增操作 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添加到二級當中,進行新增操作 goodsSellAttributeMapper.insertGoodsSubAttributeValue(goodsSellAttributeValueDTO); } return new OutputObject(ReturnCode.SUCCESS, "添加成功", "添加成功"); }else { return new OutputObject(ReturnCode.FAIL, "添加失敗", goodsSellAttributeDTO); } }catch (Exception e){ // 事務回滾 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); }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章

IntelliJ IDEA 部署 Web 項目,看這一篇夠了!
這篇文章主要介紹了IntelliJ IDEA 部署 Web 項目的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
2020-05-05 
2024版本IDEA創(chuàng)建Servlet模板的圖文教程
新版IDEA?2024.1.4中,用戶需要自行創(chuàng)建Servlet模板以解決Web項目無法通過右鍵創(chuàng)建Servlet的問題,本文詳細介紹了添加ServletAnnotatedClass.java模板的步驟,幫助用戶快速配置并使用新的Servlet模板,需要的朋友可以參考下
2024-10-10 
Springboot啟動同時創(chuàng)建數(shù)據(jù)庫和表實現(xiàn)方法
這篇文章主要介紹了Springboot啟動同時創(chuàng)建數(shù)據(jù)庫和表,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
2023-01-01 
Spring boot自定義http反饋狀態(tài)碼詳解
這篇文章主要給大家介紹了Spring boot自定義http反饋狀態(tài)碼的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
2017-06-06