有许多使用 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。是否可以在存储过程或批量操作中重写它。
谢谢。请帮忙。