Hibernate JDBC 批量插入
public void createRolePermissionBySql(final String roleId,final String[] permIds){
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
if(permIds==null||permIds.length==0)
return null;
Connection conn=session.connection();
PreparedStatement ps=null;
String sql=" insert into base_role_permission (ROLE_PERM_ID,PERM_ID,ROLE_ID,CREATE_DATE)" +
" values (?,?,?,?) ";
ps=conn.prepareStatement(sql);
if(log.isDebugEnabled())
log.debug("batch add base_role_permission:"+sql);
UUIDHexGenerator uuidGenerator=null;
for(String permId:permIds){
uuidGenerator=new UUIDHexGenerator();
ps.setString(1,uuidGenerator.generate(null, null).toString());
ps.setString(2,permId);
ps.setString(3, roleId);
ps.setDate(4, DateUtils.getJavaSqlDate());
ps.addBatch();
}
int[] counts=ps.executeBatch();
ps.close();
session.flush();
session.close();
return null;
}
});
}