Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration
Hi
In this blog I will describe the service level changes for Spring batch update.
I am providing the the details of the batch update using jdbcTemplate. Mainly I am using jdbcTemplate.batchUpdate() method to perform the batch update into mysql database.
I am using BatchPreparedStementSetter to implement the batch update in spring restful webservices.
Two methods are uses for batchupdate i.e. 'BatchPreparedStatementSetter' & 'batchUpdate' . The BatchPreparedStatementSetter' is passed as second argument in 'batchUpdate' method.
The 'getBatchSize' method is used to find the size of the batch. This method is necessary because without it you can't execute 'BatchPreparedStatementSetter' method.
The 'setValues' method is used here to set the values in place of '?'. This method is invoked number of times the value returned by 'getBatchSize' method.
AreaDao :
Here i am defining the interface (AreaDao) with insertArea() method.
public interface AreaDao {
public void insertArea(List areas);
}
AreaDaoImpl :
Here I am defining the AreaDaoImpl class and implement the insertArea() method using BatchPreparedStatementSetter.
public void insertArea(final List areas) {
jdbcTemplate.batchUpdate("INSERT INTO tbl_area " +
"(area_id, area_location, area_land_mark,email) VALUES (?, ?, ?,?)", new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Area area = areas.get(i);
ps.setLong(1, area.getAreaId());
ps.setString(2, area.getAreaLocation());
ps.setString(3, area.getAreaLandMark());
ps.setString(4, area.getEmail());
}
public int getBatchSize() {
return areas.size();
}
});
Area :
This is the Area class that represents the Area in the MySql database. The fields are mapping directly to the database table.
public class Area {
private int areaId;
private String areaLocation;
private String areaLandMark;
private String email;
public int getAreaId() {
return areaId;
}
public void setAreaId(int areaId) {
this.areaId = areaId;
}
public String getAreaLocation() {
return areaLocation;
}
public void setAreaLocation(String areaLocation) {
this.areaLocation = areaLocation;
}
public String getAreaLandMark() {
return areaLandMark;
}
public void setAreaLandMark(String areaLandMark) {
this.areaLandMark = areaLandMark;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
AreaService :
Here i am defining the interface (AreaService) with insertArea() method.
public interface AreaService {
public void insertArea(List areas);
}
AreaServiceImpl :
Here I am defining the AreaServiceImpl class and implement the addArea() method.
public interface AreaServiceImpl implements AreaService {
@Autowired
private AreaDaoImpl areaDao;
public void insertArea(List areas){
areaDao.insertArea(List areas);
}
}
AreaController :
DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controller annotation indicates that a particular class serves the role of a controller. The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.
Here I am using AreaController to delegate all the AJAX Call from JSP to AreaService.
@Controller
@RequestMapping(value = "/areabatch")
public class AreaController {
@Autowired
private AreaServiceImpl AreaService;
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public void insertArea(@RequestBody List areas) {
AreaService.insertArea(List areas);
}
}
I am calling this Rest Full Web Service from JSP using JQuery and Ajax call. This will do the Batch Update for Area in MySql database.
Hi
In this blog I will describe the service level changes for Spring batch update.
I am providing the the details of the batch update using jdbcTemplate. Mainly I am using jdbcTemplate.batchUpdate() method to perform the batch update into mysql database.
I am using BatchPreparedStementSetter to implement the batch update in spring restful webservices.
Two methods are uses for batchupdate i.e. 'BatchPreparedStatementSetter' & 'batchUpdate' . The BatchPreparedStatementSetter' is passed as second argument in 'batchUpdate' method.
The 'getBatchSize' method is used to find the size of the batch. This method is necessary because without it you can't execute 'BatchPreparedStatementSetter' method.
The 'setValues' method is used here to set the values in place of '?'. This method is invoked number of times the value returned by 'getBatchSize' method.
AreaDao :
Here i am defining the interface (AreaDao) with insertArea() method.
public interface AreaDao {
public void insertArea(List areas);
}
AreaDaoImpl :
Here I am defining the AreaDaoImpl class and implement the insertArea() method using BatchPreparedStatementSetter.
public void insertArea(final List areas) {
jdbcTemplate.batchUpdate("INSERT INTO tbl_area " +
"(area_id, area_location, area_land_mark,email) VALUES (?, ?, ?,?)", new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Area area = areas.get(i);
ps.setLong(1, area.getAreaId());
ps.setString(2, area.getAreaLocation());
ps.setString(3, area.getAreaLandMark());
ps.setString(4, area.getEmail());
}
public int getBatchSize() {
return areas.size();
}
});
Area :
This is the Area class that represents the Area in the MySql database. The fields are mapping directly to the database table.
public class Area {
private int areaId;
private String areaLocation;
private String areaLandMark;
private String email;
public int getAreaId() {
return areaId;
}
public void setAreaId(int areaId) {
this.areaId = areaId;
}
public String getAreaLocation() {
return areaLocation;
}
public void setAreaLocation(String areaLocation) {
this.areaLocation = areaLocation;
}
public String getAreaLandMark() {
return areaLandMark;
}
public void setAreaLandMark(String areaLandMark) {
this.areaLandMark = areaLandMark;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
AreaService :
Here i am defining the interface (AreaService) with insertArea() method.
public interface AreaService {
public void insertArea(List areas);
}
Here I am defining the AreaServiceImpl class and implement the addArea() method.
public interface AreaServiceImpl implements AreaService {
@Autowired
private AreaDaoImpl areaDao;
public void insertArea(List areas){
areaDao.insertArea(List areas);
}
}
DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controller annotation indicates that a particular class serves the role of a controller. The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.
Here I am using AreaController to delegate all the AJAX Call from JSP to AreaService.
@Controller
@RequestMapping(value = "/areabatch")
public class AreaController {
@Autowired
private AreaServiceImpl AreaService;
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public void insertArea(@RequestBody List areas) {
AreaService.insertArea(List areas);
}
}
I am calling this Rest Full Web Service from JSP using JQuery and Ajax call. This will do the Batch Update for Area in MySql database.