2012-11-27 163 views
0

使用JDBC或JDBCTemplate進行批量插入查詢的例子很多。JDBC/JDBCTemplate批處理操作

我想做一個批處理sql操作,包括選擇,插入和刪除。例如,我需要在與數據庫的單一連接中發出以下sql操作。

  1. 多重選擇(4個查詢到不同的表)
  2. 多次插入(4個查詢到不同的表)
  3. 刪除多(4個查詢到不同的表)

請問JDBC或JdbcTemplate的支持?

編輯問:

TicketServiceEnforce ticketDao = TicketServiceEnforceImpl.Factory.getInstance(); 

     // ================================ SELECT ================================ 
     if (unknownTicketId > ZERO) { 
      unknownTicketList = ticketDao.selectUnknownTicket(unknownTicketId); 
      if (!unknownTicketList.isEmpty()) { 
       attachmentList = ticketDao.selectAttachment(QueryString.SELECT_UNKNOWN_TICKET_ATTACHMENT_BY_ID.toString(), unknownTicketId); 
       ticketCodeList = ticketDao.selectTicketCode(QueryString.SELECT_UNKNOWN_TICKET_CODE_BY_ID.toString(), unknownTicketId); 
       ticketCommentList = ticketDao.selectComment(QueryString.SELECT_UNKNOWN_TICKET_COMMENT_BY_ID.toString(), unknownTicketId); 
      } 
     } 
     // ================================ INSERT ================================ 
     // Retrieve customer_id 
     if (!unknownTicketList.isEmpty()) { 
      // Just display all customers's name in UI 
      customerId = ticketDao.selectCustomerIdByName(genericTicket.getCustomerName()); 

      genericTicket.setCustomerId(customerId); 
      genericTicket.setSubject(unknownTicketList.get(ZERO).getSubject()); 
      genericTicket.setDetails(unknownTicketList.get(ZERO).getDetails()); 
      genericTicket.setCreationDate(unknownTicketList.get(ZERO).getCreationDate()); 

      // ===================================================================== 
      ticketId = ticketDao.createTicket(genericTicket); 
      if (ticketId > ZERO) { 

       if (!attachmentList.isEmpty()) { 
        ticketDao.createTicketAttachment(ticketId, attachmentList); 
       } 

       /* 
       * Insert new ticket code 
       */ 
       newTicketCode = generateTicketCode(ticketId); 
       if (!ticketCodeList.isEmpty()) { 
        oldTicketCode = ticketCodeList.get(ZERO); 

        ticketDao.createTicketCode(ticketId, newTicketCode); 
       } 

       /* Insert old unknown ticket code into ticket_email_mapping 
       * This table used to identify the parent child ticket from email - unknown_ticket 
       * by using two queries 
       * 
       * 1. Select ticket code - select ticket code from ticket_email_mapping using id 
       * 2. Select ticket id that has previous ticket code - Select id from ticket_email_mapping tem where tem. 
       * 
       */ 
       if (oldTicketCode != null) { 
        String from_sender = null; 
        if (unknownTicketList.size() > ZERO) { 
         from_sender = unknownTicketList.get(ZERO).getFrom(); 
        } 

        ticketDao.createTicketEmailMapping(ticketId, oldTicketCode, from_sender); 
       } 

       if (!ticketCommentList.isEmpty()) { 
        for (GenericTicketComment comment : ticketCommentList) { 
         comment.setTicketId(ticketId); 
        } 

        ticketDao.createTicketComment(QueryString.INSERT_TICKET_COMMENT.toString(), ticketCommentList); 
       } 

       if (genericTicket.getAssigneeName() != null) { 
        int assigneeId = -1; 
        String firstName = ""; 
        TicketAssignee assignee = new TicketAssignee(); 

        firstName = genericTicket.getAssigneeName(); 
        assigneeId = ticketDao.selectUserId(firstName); 

        assignee.setTicketId(ticketId); 
        assignee.setAssigneeId(assigneeId); 

        ticketDao.createTicketAssignee(assignee); 
       } 

       // If all successfull 
       ticketCreationSuccessful = true; 
      } 
     } 

     // ================================ DELETE ================================ 
     if (ticketCreationSuccessful) { 
      if (!attachmentList.isEmpty()) { 
       affectedRow = ticketDao.removeUnknownTicket(QueryString.DELETE_UNKNOWN_TICKET_ATTACHMENT_BY_ID.toString(), unknownTicketId); 
      } 

      if (!ticketCommentList.isEmpty()) { 
       affectedRow = ticketDao.removeUnknownTicket(QueryString.DELETE_UNKNOWN_TICKET_COMMENT_BY_ID.toString(), unknownTicketId); 
      } 

      affectedRow = ticketDao.removeUnknownTicket(QueryString.DELETE_UNKNOWN_TICKET_CODE_BY_ID.toString(), unknownTicketId); 
      affectedRow = ticketDao.removeUnknownTicket(QueryString.DELETE_UNKNOWN_TICKET_BY_ID.toString(), unknownTicketId); 
     } 

這是使用正常的查詢操作的DAO。是否可以在存儲過程或批量操作中重寫它。

謝謝。請幫忙。

+0

任何人有想法如何做到這一點? – peterwkc

+1

jdbcTemplate.batchUpdate僅對同一個sql操作中的多個語句有效,如僅插入。 – peterwkc

回答

2

如名稱所示jdbcTemplate.batchUpdate您不能在批處理操作中進行選擇。如果您想一次完成所有操作,只需創建一個實現邏輯的大SQL,並使用JdbcTemplate.#execute(java.lang.String)方法。

+0

JdbcTemplate。#execute(java.lang.String)不提供佔位符綁定。我需要一個用來執行不同的sql操作的方法,比如選擇,插入,刪除一起去佔位符綁定。謝謝 – peterwkc

+0

看看[NamedParameterJdbcTemplate。#execute](http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html#execute%28java .lang.String,%20org.springframework.jdbc.core.namedparam.SqlParameterSource,%20org.springframework.jdbc.core.PreparedStatementCallback%29) – Jayamohan