Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

While the typical coordination of the batch insert is complex, doing it using the DAF is similar to that done for simple CRUD operations. The first step is to define the SQL map entries:

Code Block
languagesql
<insert id="EMPLOYEE_BATCH_INSERT_MYAPP" parameterClass="java.util.HashMap" >

...


INSERT INTO EMPLOYEE ( EMP_ID, EMP_NAME, DOB, MOB_NO)

...


VALUES ( #EmployeeId#, #EmployeeName#, #dtBirthDate#, #mobileNum#)

...


</insert>


The next step is to execute the database request. Only difference is that a list of data is passed instead of a single data map.

Code Block
languagejava
try {

...


List<Map> batchData = getAllEmployeesToInsert(); //Prepare the list of records to insert

...


DatabaseRequest dbRequest = new DatabaseRequest();

...

 
dbRequest.setDataAccessMapKey("EMPLOYEE");

...


dbRequest.setOperation(DatabaseConstants.BATCH_INSERT)

...


dbRequest.setOperationExtension("MYAPP");

...

 
dbRequest.setBatchData(paramData); //Add the batch data to the request

...

 
DatabaseResult dbResult = dbRequest.execute(); //Execute the request.

...

 
logger.debug("No. of rows affected = " + dbResult.getNoOfRowsAffected());

...

 
} catch (DatabaseException dbException) {

...


//Handle the exception appropriately.

...


}