如何将多行作为单个对象传递

时间:2019-04-09 03:35:55

标签: mysql excel hibernate spring-mvc

postman image

这是Controller方法:

@RequestMapping(value = "/add567", method = RequestMethod.POST, consumes = "application/json")
public Response addObj(@RequestBody List121 classA){

    return  service1.addObj(classA);
}

这是Entity类:

 package com.excel.entity;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="ClassA")
public class ClassA  extends ArrayList<Object> {
     @Id
    @Column(name="rollNo")
    private int rollNo;

    @Column(name="name")
    private String name;

    @Column(name="english")
    private double english;

    @Column(name="maths")
    private double maths;

    @Column(name="science")
    private double science;

    @Column(name="totalMarks")
    private double totalMarks;

    @Column(name="percentage")
    private double percentage;

    @Column(name="status")
    private boolean status;

    @Lob
    @Column(name="file", columnDefinition="BLOB")
    private byte[] file;



    public ClassA() {
        // TODO Auto-generated constructor stub
    }



    public ClassA(int rollNo, String name, double english, double maths, double science, double totalMarks,
            double percentage, boolean status, byte[] file) {
        super();
        this.rollNo = rollNo;
        this.name = name;
        this.english = english;
        this.maths = maths;
        this.science = science;
        this.totalMarks = totalMarks;
        this.percentage = percentage;
        this.status = status;
        this.file = file;
    }



    public int getRollNo() {
        return rollNo;
    }



    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public double getEnglish() {
        return english;
    }



    public void setEnglish(double english) {
        this.english = english;
    }



    public double getMaths() {
        return maths;
    }



    public void setMaths(double maths) {
        this.maths = maths;
    }



    public double getScience() {
        return science;
    }



    public void setScience(double science) {
        this.science = science;
    }



    public double getTotalMarks() {
        return totalMarks;
    }



    public void setTotalMarks(double totalMarks) {
        this.totalMarks = totalMarks;
    }



    public double getPercentage() {
        return percentage;
    }



    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }



    public boolean isStatus() {
        return status;
    }



    public void setStatus(boolean status) {
        this.status = status;
    }



    public byte[] getFile() {
        return file;
    }



    public void setFile(byte[] file) {
        this.file = file;
    }


}

这是我从中获取列表的类:

import com.fasterxml.jackson.annotation.JsonInclude;


@JsonInclude(JsonInclude.Include.NON_EMPTY) 
public class List121  {

    List<ClassA> classA;
    List<ClassB> classB;
    List<ClassC> classC;


    public List<ClassA> getClassA() {
        return classA;
    }
    public void setClassA(List<ClassA> classA) {
        this.classA = classA;
    }
    public List<ClassB> getClassB() {
        return classB;
    }
    public void setClassB(List<ClassB> classB) {
        this.classB = classB;
    }
    public List<ClassC> getClassC() {
        return classC;
    }
    public void setClassC(List<ClassC> classC) {
        this.classC = classC;
    }



}

这是用于存储在数据库中的DAO类:

public Response addObj(List121 classA){
    System.out.println("entering");
    ExcelAdd writer=new ExcelAdd();
   List121 e1=new List121();
   ClassA a1=new ClassA();
    try{
        s=sf.openSession();
        tx=s.beginTransaction();
        for(ClassA classa :classA.getClassA() ) {
            a1.setRollNo(classa.getRollNo());
            a1.setName(classa.getName());
            a1.setEnglish(classa.getEnglish());
            a1.setMaths(classa.getMaths());
            a1.setScience(classa.getScience());
            a1.setTotalMarks(classa.getTotalMarks());
            a1.setPercentage(classa.getPercentage());
            a1.setStatus(classa.isStatus());
            a1.setFile(classa.getFile());
            s.save(a1);
        }


        response.setStatus(200);
        response.setMessage("succesfull");
        tx.commit();
        return response;
    }
    catch (Exception e) {
        System.err.println("Exception : " + e.getMessage());
        response.setStatus(500);
        response.setMessage("unsuccesfull");
        tx.rollback();
return response;
    } finally {
        s.close();
    }

}

我将数据发布到邮递员中,但请求不正确:

我无法确定这是什么错误。在邮递员中收到错误的请求。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找类似的东西

Option Explicit

Sub Adjust_col()
    Dim wb As Workbook
    Dim App_list As Long, DAU_col As Long

    Set wb = ThisWorkbook

    App_list = wb.Sheets("App Name Map").Range("B2").End(xlDown).Row

    DAU_col = wb.Sheets("DAU").Range("D4", Range("D4").End(xlToRight)).Count

    MsgBox DAU_col
End Sub

以下代码可用于实现相同目的。

{
    "classList": [
        {
            "rollNumber": "1",
            "name": "Sham1"
        },
        {
            "rollNumber": "2",
            "name": "Sham2"
        }
    ]
}

域对象-学校

package com.example.demo.controller;

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;

import com.example.demo.domain.Clazz;
import com.example.demo.domain.School;

@RestController
@RequestMapping(path = "/v1")
public class TestController {

    @PostMapping("/school")
    public void school(@RequestBody School school) {
        for(Clazz clazz : school.getClassList()) {
            System.out.println(clazz.getName());
        }
    }
}

克拉兹

package com.example.demo.domain;

import java.util.List;

public class School {

    private List<Clazz> classList;

    public List<Clazz> getClassList() {
        return classList;
    }

    public void setClassList(List<Clazz> classList) {
        this.classList = classList;
    }

}

邮递员

enter image description here

更新:以下内容对我有用,与OP分享相同的输入。 。我不确定这里使用的响应返回类型是什么。

package com.example.demo.domain;

public class Clazz {
    String rollNumber;
    String name;

    public String getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(String rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

请分享返回类型的详细信息以进一步调试