当前位置: 首页 > news >正文

java+postgresql+swagger-多表关联insert操作(七)

入参为json,然后根据需要对多张表进行操作:

入参格式:

[{"custstoreName":"swagger-测试经销商01","customerName":"swagger-测试客户01","propertyNo":"swaggertest01","propertyName":"swagger-测试资产01","proName":"四川省","cityName":"攀枝花市","propertyType":"1","deviceNumber":"swaggerdevice01","fileName":"swagger-测试资产01附件01","fileUrl":"https://www.baidu.com/?tn=25017023_17_dg"},{"custstoreName":"swagger-测试经销商02","customerName":"swagger-测试客户01","propertyNo":"swaggertest02","propertyName":"swagger-测试资产02","proName":"黑龙江省","cityName":"七台河市","propertyType":"1","deviceNumber":"swaggerdevice02","fileName":"swagger-测试资产02附件01","fileUrl":"https://www.baidu.com/?tn=25017023_17_dg"},{"custstoreName":"swagger-测试经销商03","customerName":"swagger-测试客户02","propertyNo":"swaggertest03","propertyName":"swagger-测试资产03","proName":"江苏省","cityName":"徐州市","propertyType":"1","deviceNumber":"swaggerdevice03","fileName":"swagger-测试资产03附件01","fileUrl":"https://www.baidu.com/?tn=25017023_17_dg"}]

其中tbPropertyService.addProperty的内容,参考:

java+postgresql+swagger-多表关联insert操作(六)

1、Service:

package com.example.springbootmybatisplus.service;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootmybatisplus.dto.PropertyDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbPropertyService2 {
    private static final Logger log = LoggerFactory.getLogger(TbDevicestatusService.class);

    @Autowired
    private TbPropertyService tbPropertyService;

    public void addProperty2(String msg) {
        try {
            List<PropertyDto> jsonArray = JSONObject.parseArray(msg, PropertyDto.class);
            for (int i = 0; i < jsonArray.size(); i++) {
                PropertyDto propertyDto = jsonArray.get(i);
                tbPropertyService.addProperty(JSONObject.toJSONString(propertyDto));
            }
        } catch (Exception e) {
            log.error("异常信息:" + e.getMessage());
        }
    }

}

2、Controller:

package com.example.springbootmybatisplus.contronller;

import com.example.springbootmybatisplus.service.TbPropertyService;
import com.example.springbootmybatisplus.service.TbPropertyService2;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tb-property")
@Api(tags = "资产管理")
public class TbPropertyController {
    @Autowired
    private TbPropertyService tbPropertyService;

    @Autowired
    private TbPropertyService2 tbPropertyService2;

    @PostMapping("/insert1")
    public ResponseEntity<String> insert1(@RequestBody String msg) {
        try {
            tbPropertyService.addProperty(msg);
            return ResponseEntity.status(HttpStatus.CREATED).body("Success");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed");
        }
    }

    @PostMapping("/insert2")
    public ResponseEntity<String> insert2(@RequestBody String msg) {
        try {
            tbPropertyService2.addProperty2(msg);
            return ResponseEntity.status(HttpStatus.CREATED).body("Success");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed");
        }
    }
}

3、调用接口打印的日志:

相关文章:

  • 流体力学笔记
  • 体验为王:云化园区网络如何重塑用户业务零卡顿时代
  • QT控件 修改QtTreePropertyBrowser自定义属性编辑器源码,添加第一列标题勾选,按钮,右键菜单事件等功能
  • 网络缓冲区
  • OpenCV--图像平滑处理
  • 使用protobuf编译提示无法打开包括文件: ‘absl/log/absl_log.h’: No such file or directory
  • 第十四届蓝桥杯省赛B组c++——填空题201串的熵
  • AI比人脑更强,因为被植入思维模型【47】可复制化思维模型
  • LemonSqueezy: 1靶场渗透
  • OpenCV 图形API(21)逐像素操作
  • Kafka的索引设计有什么亮点
  • 利用pytorch对加噪堆叠自编码器在MNIST数据集进行训练和验证
  • linux-添加开机自启动指定脚本
  • #关于require 与 import 相关了解
  • AI爬虫?爬!(附加mcp实践教程)
  • 第十五届蓝桥杯国赛题 代码量统计-困难 解题思路
  • 【蓝桥杯】搜索算法:DFS/BFS
  • R Excel 文件:高效数据处理与可视化工具的完美结合
  • 图形裁剪算法
  • 深度学习天崩开局
  • 南宁一学校发生伤害案件,警方通报:嫌疑人死亡,2人受伤
  • 国家统计局:2024年城镇单位就业人员工资平稳增长
  • 外交部介绍对巴西、阿根廷、智利、秘鲁、乌拉圭等5国试行免签政策
  • 科普|男性这个器官晚到岗,可能影响生育能力
  • 严打金融黑灰产,今年来上海警方破获各类经济犯罪案件690余起
  • KPL“王朝”诞生背后:AG和联赛一起迈向成熟