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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值