使用JDBC或JDBCTemplate進行批量插入查詢的例子很多。JDBC/JDBCTemplate批處理操作
我想做一個批處理sql操作,包括選擇,插入和刪除。例如,我需要在與數據庫的單一連接中發出以下sql操作。
- 多重選擇(4個查詢到不同的表)
- 多次插入(4個查詢到不同的表)
- 刪除多(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。是否可以在存儲過程或批量操作中重寫它。
謝謝。請幫忙。
任何人有想法如何做到這一點? – peterwkc
jdbcTemplate.batchUpdate僅對同一個sql操作中的多個語句有效,如僅插入。 – peterwkc