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

发布于:2025-04-08 ⋅ 阅读:(40) ⋅ 点赞:(0)

入参为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、调用接口打印的日志:


网站公告

今日签到

点亮在社区的每一天
去签到