batch update for updating multiple records in spring mvc with mysql -
i have issue suppose have 100 records initially, , shown them on ui list of users. have given provision deactivate number users clicking "deactivate" button placed against every single record, capture "deactivated" users in list , send dao layer.[the logic of deactivating user set 'isdeleted' flag true, i.e.soft delete updating multiple records ids have placed list],there simple solution that, write loop->iterate through list-> , each record fire query update isdeleted flag true, not feasible solution if have 5000 records deleted @ once. have heard , implemented batchupdate concept "inserting" multiple records @ once, dont understand how can use batch update update several records @ 1 db call, please help, batch update code insertion follows,
private static final string insert_user_permission = "insert permission_transaction(permissionid,userid,isdeleted) " + "values(?,?,?)"; @transactional public void addpermission(final uservo uservo, final list<permissionvo> permissionvolist) throws exception { logger.info("adding user permission, userid: "+uservo.getuserid()); try { jdbc.batchupdate(insert_user_permission, new batchpreparedstatementsetter() { @override public void setvalues(preparedstatement ps, int i) throws sqlexception { permissionvo permissionvo = permissionvolist.get(i); ps.setint(1, permissionvo.getpermissionid()); ps.setint(2, uservo.getuserid()); ps.setboolean(3, false); } @override public int getbatchsize() { return permissionvolist.size(); } }); logger.info("exiting addpermission, userid: "+uservo.getuserid()); }catch (exception e) { logger.error("error in adding user permission: " + e.getmessage(), e); throw e; } }
hey found solution, here did,
private static final string update_client_owner = "update clientowner set " + "clientownername=?," + "clientownerphone=?," + "clientowneremail=?," + "lastupdatedon=now() " + "where clientownerid=?"; @transactional public void updateclientowner(int clientid, list<clientownervo> clientownervolist) throws exception { logger.info("updating client owner(s)"); try{ int[] count = jdbc.batchupdate(update_client_owner, new batchpreparedstatementsetter() { @override public void setvalues(preparedstatement ps, int i) throws sqlexception { clientownervo clientownervo = clientownervolist.get(i); ps.setstring(1, clientownervo.getclientownername()); ps.setstring(2, variableencryption.encrypt(clientownervo.getclientownerphone(), clientownervo.getcreatedon())); ps.setstring(3, variableencryption.encrypt(clientownervo.getclientowneremail(), clientownervo.getcreatedon())); ps.setint(4, clientownervo.getclientownerid()); } @override public int getbatchsize() { return clientownervolist.size(); } }); logger.info("exiting after updating "+count.tostring()+" client owners"); }catch (exception e) { logger.error("error in updating client owners: " + e.getmessage(), e); throw e; }
Comments
Post a Comment