Batch Insert

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:

<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.

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.
}