последняя строка вставляется в базу данных при чтении XML-файла с помощью java Jaxb2

#java #spring #xml-parsing #jaxb #spring-batch

Вопрос:

Я пытаюсь прочитать XML-файл, в списке массивов у меня есть все записи, но при сохранении данных в БД MySQL будут вставлены только последние данные списка массивов. Пожалуйста, помогите мне, как сохранить все строки в базе данных. Ниже приведен мой шаг 1

 @Bean  public Step step1() {  return stepBuilderFactory.get("step1")  .lt;XML_File_Model, XML_File_Modelgt; chunk(10)  .reader(reader(null))  .writer(writer())  .build();  }  

В классе XML_File_Model у меня есть список корневых элементов

 @XmlRootElement(name = "XML_FILE")  @XmlAccessorType (XmlAccessType.FIELD)  public class XML_File_Model{  @XmlElement(name = "Transaction")  private List lt;Transactiongt; transaction= new ArrayListlt;gt;();  @XmlElement(name = "Amended_TRN")  private List lt;AmendedTransactiongt; amendedTransaction= new   ArrayListlt;gt;(); public List lt;Transactiongt; getTransaction() {  return transaction;  }   public void setTransaction(List lt;Transactiongt; transaction) {  this.transaction = transaction;  }   public List lt;AmendedTransactiongt; getAmendedTransaction() {  return amendedTransaction;  }   public void setAmendedTransaction(List lt;AmendedTransactiongt; amendedTransaction) {  this.amendedTransaction = amendedTransaction;  }  

Измененный класс модели Transaction

 @XmlRootElement(name = "Amended_TRN")  @XmlAccessorType (XmlAccessType.FIELD)  public class AmendedTransaction{  @XmlElement(name = "Item")  private List lt;InventoryItemgt; inventoryItem= new ArrayListlt;gt;();  private String Amended_TRN_Date;  private String Amended_TRN_Time;  public List lt;InventoryItemgt; getInventoryItem() {  return inventoryItem;  }   public void setInventoryItem(List lt;InventoryItemgt; inventoryItem) {  this.inventoryItem = inventoryItem;  }  @XmlElement(name = "Amended_TRN_Date")  public String getAmended_TRN_Date() {  return Amended_TRN_Date;  }   public void setAmended_TRN_Date(String amended_TRN_Date) {  Amended_TRN_Date = amended_TRN_Date;  }   @XmlElement(name = "Amended_TRN_Time")  public String getAmended_TRN_Time() {  return Amended_TRN_Time;  }   public void setAmended_TRN_Time(String amended_TRN_Time) {  Amended_TRN_Time = amended_TRN_Time;  }  

Transaction Model Class

 @XmlRootElement(name = "Transaction")  @XmlAccessorType (XmlAccessType.FIELD)  public class Transaction{  @XmlElement(name = "Item")  private List lt;InventoryItemgt; inventoryItem= new ArrayListlt;gt;();  private String S_18_Description_Gas_Coupon;  private String Ticket_Number;  private String Trans_Time_HHMMSS;  private String Trans_Date;  public List lt;InventoryItemgt; getInventoryItem() {  return inventoryItem;  }   public void setInventoryItem(List lt;InventoryItemgt; inventoryItem) {  this.inventoryItem = inventoryItem;  } @XmlElement(name = "S_18_Description_Gas_Coupon")  public String getS_18_Description_Gas_Coupon() {  return S_18_Description_Gas_Coupon;  }   public void setS_18_Description_Gas_Coupon(String s_18_Description_Gas_Coupon) {  S_18_Description_Gas_Coupon = s_18_Description_Gas_Coupon;  }  @XmlElement(name = "Ticket_Number")  public String getTicket_Number() {  return Ticket_Number;  }    public void setTicket_Number(String ticket_Number) {  Ticket_Number = ticket_Number;  }    @XmlElement(name = "Trans_Time_HHMMSS")  public String getTrans_Time_HHMMSS() {  return Trans_Time_HHMMSS;  }    public void setTrans_Time_HHMMSS(String trans_Time_HHMMSS) {  Trans_Time_HHMMSS = trans_Time_HHMMSS;  }  @XmlElement(name = "Trans_Date")  public String getTrans_Date() {  return Trans_Date;  }  public void setTrans_Date(String trans_Date) {  Trans_Date = trans_Date;  }  

Writer Code

 @Bean  public JdbcBatchItemWriterlt;XML_File_Modelgt; writer() {  JdbcBatchItemWriterlt;XML_File_Modelgt; writer = new JdbcBatchItemWriterlt;XML_File_Modelgt;();  writer.setDataSource(dataSource);  writer.setSql("insert into xml_salesinsert into xml_sales(S_18_Description_Gas_Coupon,Ticket_Number,Trans_Time_HHMMSS,Trans_Date,Item_Number,English_Description,French_Description,Department,Amended_TRN_Date,Amended_TRN_Time) values (?,?,?,?,?,?,?,?,?,?)");   writer.setItemPreparedStatementSetter(new CompanyDetailsItemPreparedStmSetter());    return writer;  }  

Reader Code

 @StepScope  public StaxEventItemReaderlt;XML_File_Modelgt; reader(@Value("#{jobParameters[file_path]}") String filePath) {  StaxEventItemReaderlt;XML_File_Modelgt; reader = new StaxEventItemReaderlt;gt;();  final FileSystemResource fileResource = new FileSystemResource(filePath);  reader.setResource(fileResource);  reader.setFragmentRootElementNames(new String[] {"XML_FILE"});  Jaxb2Marshaller marshaller = new Jaxb2Marshaller();  marshaller.setClassesToBeBound(XML_File_Model.class);  reader.setUnmarshaller(marshaller); return reader;  }  

CompanyDetailsItemPreparedStmSetter Class

 @Override  public void setValues(XML_File_Model xmlFile, PreparedStatement ps) throws SQLException {  if(xmlFile.getTransaction().size()gt;0) {  for(int i=0; ilt;xmlFile.getTransaction().size(); i  ) {  ps.setString(1, xmlFile.getTransaction().get(i).getS_18_Description_Gas_Coupon());  ps.setString(2, xmlFile.getTransaction().get(i).getTicket_Number());  ps.setString(3, xmlFile.getTransaction().get(i).getTrans_Time_HHMMSS());  ps.setString(4, xmlFile.getTransaction().get(i).getTrans_Date());       if(xmlFile.getTransaction().get(i).getInventoryItem().size()gt;0 amp;amp; xmlFile.getTransaction().get(i).getInventoryItem().get(0).getItem_Number()!="" amp;amp;  xmlFile.getTransaction().get(i).getInventoryItem().get(0).getItem_Number()!=null) {  ps.setString(5, xmlFile.getTransaction().get(i).getInventoryItem().get(0).getItem_Number());  ps.setString(6, xmlFile.getTransaction().get(i).getInventoryItem().get(0).getEnglish_Description());  ps.setString(7, xmlFile.getTransaction().get(i).getInventoryItem().get(0).getFrench_Description());  ps.setString(8, xmlFile.getTransaction().get(i).getInventoryItem().get(0).getDepartment());  ps.setString(9, default value will pass);  ps.setString(10, default value will pass);   }// if item end here    }// for loop end here    }//if transaction end here  else if(xmlFile.getAmendedTransaction().size()gt;0) {  for(int j=0; jlt;xmlFile.getAmendedTransaction().size(); j  ) {  ps.setString(1, xmlFile.getAmendedTransaction().get(j).getS_18_Description_Gas_Coupon());  ps.setString(2, default value will pass);  ps.setString(3, default value will pass);  ps.setString(4, default value will pass);      if(xmlFile.getAmendedTransaction().get(j).getInventoryItem().size()gt;0 amp;amp; xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getItem_Number()!="" amp;amp;  xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getItem_Number()!=null) {  ps.setString(5, xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getItem_Number());  ps.setString(6, xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getEnglish_Description());  ps.setString(7, xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getFrench_Description());  ps.setString(8, xmlFile.getAmendedTransaction().get(j).getInventoryItem().get(0).getDepartment());    }  ps.setString(9, xmlFile.getAmendedTransaction().get(j).getAmended_TRN_Date());  ps.setString(10, xmlFile.getAmendedTransaction().get(j).getAmended_TRN_Time());    }  } }  

XML-ФАЙЛ

 lt;XML_FILEgt;  lt;Transactiongt;  lt;S_18_Description_Gas_Coupongt;lt;/S_18_Description_Gas_Coupongt;  lt;Ticket_Numbergt;lt;/Ticket_Numbergt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt;  lt;Trans_Time_HHMMSSgt;lt;/Trans_Time_HHMMSSgt;  lt;Trans_Dategt;lt;/Trans_Dategt;  lt;/Transactiongt;  lt;Transactiongt;  lt;S_18_Description_Gas_Coupongt;lt;/S_18_Description_Gas_Coupongt;  lt;Ticket_Numbergt;lt;/Ticket_Numbergt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt;  lt;Trans_Time_HHMMSSgt;lt;/Trans_Time_HHMMSSgt;  lt;Trans_Dategt;lt;/Trans_Dategt;  lt;/Transactiongt;  lt;Transactiongt;  lt;S_18_Description_Gas_Coupongt;lt;/S_18_Description_Gas_Coupongt;  lt;Ticket_Numbergt;lt;/Ticket_Numbergt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt;  lt;Trans_Time_HHMMSSgt;lt;/Trans_Time_HHMMSSgt;  lt;Trans_Dategt;lt;/Trans_Dategt;  lt;/Transactiongt; lt;Amended_TRNgt; lt;Amended_TRN_Sequencegt; 1lt;/Amended_TRN_Sequencegt; lt;Amended_TRN_Dategt;20211103lt;/Amended_TRN_Dategt; lt;Amended_TRN_Timegt;133046lt;/Amended_TRN_Timegt; lt;Amended_TRN_REASONgt;CompletionOfPrepaylt;/Amended_TRN_REASONgt; lt;Trans_Time_HHMMSSgt;132924lt;/Trans_Time_HHMMSSgt; lt;S_18_Description_Gas_Coupongt;lt;/S_18_Description_Gas_Coupongt;  lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt; lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt; lt;Transaction_End_Timegt;211103132924lt;/Transaction_End_Timegt; lt;Transaction_Start_Dategt;20211103lt;/Transaction_Start_Dategt; lt;Transaction_Start_Timegt;132910lt;/Transaction_Start_Timegt; lt;/Amended_TRNgt; lt;Amended_TRNgt; lt;Amended_TRN_Sequencegt; 1lt;/Amended_TRN_Sequencegt; lt;Amended_TRN_Dategt;20211103lt;/Amended_TRN_Dategt; lt;Amended_TRN_Timegt;133123lt;/Amended_TRN_Timegt; lt;Amended_TRN_REASONgt;CompletionOfPrepaylt;/Amended_TRN_REASONgt; lt;Trans_Time_HHMMSSgt;132854lt;/Trans_Time_HHMMSSgt;  lt;S_18_Description_Gas_Coupongt;lt;/S_18_Description_Gas_Coupongt;   lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt; lt;Itemgt;  lt;Item_Numbergt;lt;/Item_Numbergt;  lt;English_Descriptiongt;lt;/English_Descriptiongt;  lt;French_Descriptiongt;lt;/French_Descriptiongt;  lt;Departmentgt;lt;/Departmentgt;  lt;/Itemgt; lt;Transaction_End_Timegt;211103132854lt;/Transaction_End_Timegt; lt;Transaction_Start_Dategt;20211103lt;/Transaction_Start_Dategt; lt;Transaction_Start_Timegt;132838lt;/Transaction_Start_Timegt; lt;/Amended_TRNgt;  lt;/XML_FILEgt;  

Комментарии:

1. Не зная и не видя, что делают ваши читатели и писатели, на это будет невозможно ответить.

2. Я обновил код, добавив средства чтения и записи. Спасибо

3. И как выглядит XML и как CompanyDetailsItemPreparedStmSetter выглядит ваш.

4. Я обновил код.

5. Какие-либо изменения, Item_Number используемые в качестве первичного ключа, и что это одно и то же? Я также не понимаю, как вы читаете xml, так как вы читаете весь элемент за один раз? Разве Transaction элемент не должен быть корневым или, может быть, даже Item ? В настоящее время я бы сказал, что ваш код не имеет смысла, если вы не реализовали getItem_Number метод для объекта xml-файла.