入參為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);@Autowiredprivate 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 {@Autowiredprivate TbPropertyService tbPropertyService;@Autowiredprivate 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");}}
}