我正在使用Spring Boot和JDBCTemplates開發用於DB查詢的應用程序。Spring JDBCTemplates:通過Join執行查詢
問題是這樣的:如果我不得不問問單個表上的db,我沒有問題。但是,如果我有加入,我該如何執行此任務?
更具體地,SQL命令創建表是這些:
CREATE TABLE firewall_items
(
id INT NOT NULL AUTO_INCREMENT,
firewall_id INT NOT NULL,
date DATE,
src VARCHAR(15),
src_port INT,
dst VARCHAR(15),
dst_port INT,
protocol VARCHAR(4),
PRIMARY KEY(id)
);
CREATE TABLE firewalls (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
ip VARCHAR(15) NOT NULL,
info TEXT,
PRIMARY KEY(id)
);
的correspondings Java類是這些:
import java.util.Date;
public class FirewallItems
{
private Date date;
private String id;
private String protocol;
private String src;
private String dst;
private String src_port;
private String dst_port;
private String firewall_id;
public FirewallItems() {}
public FirewallItems(Date data, String identificativo, String protocollo, String sorgente, String destinazione,
String porta_sorgente, String porta_destinazione, String firewall_id)
{
super();
this.date = data;
this.id = identificativo;
this.protocol = protocollo;
this.src = sorgente;
this.dst = destinazione;
this.src_port = porta_sorgente;
this.dst_port = porta_destinazione;
this.firewall_id = firewall_id;
}
/**
* Return the date of the report
* @return date
*/
public Date getDate()
{
return date;
}
/**
* Set the date of the report
* @param date the report's date
*/
public void setDate(Date date)
{
this.date = date;
}
/**
* Return the id of the report
* @return id
*/
public String getId()
{
return id;
}
/**
* Set the id of the report
* @param id the report's id
*/
public void setId(String id)
{
this.id = id;
}
/**
* Return the protocol cecked by report
* @return protocol
*/
public String getProtocol()
{
return protocol;
}
/**
* Set the protocol cecked by report
* @param protocol
*/
public void setProtocol(String protocol)
{
this.protocol = protocol;
}
/**
* Return the source of firewall's drop
* @return Src
*/
public String getSrc()
{
return src;
}
/**
* Set the source of firewall's drop
* @param src the firewall's source drop
*/
public void setSrc(String src)
{
this.src = src;
}
/**
* Return the firewall's destionation drop
* @return dst
*/
public String getDst()
{
return dst;
}
/**
* Set the firewall's destination drop
* @param dst the firewall's destination drop
*/
public void setDst(String dst)
{
this.dst = dst;
}
/**
* Return the source's port
* @return src_port
*/
public String getSrc_port()
{
return src_port;
}
/**
* Set the source's port
* @param src_port the source's port
*/
public void setSrc_port(String src_port)
{
this.src_port = src_port;
}
/**
* Return the destination's port
* @return dst_port
*/
public String getDst_port()
{
return dst_port;
}
/**
* Set the destination's port
* @param dst_port the destination's port
*/
public void setDst_port(String dst_port)
{
this.dst_port = dst_port;
}
/**
* Return the id of firewall associated to report
* @return firewall_id
*/
public String getFirewall_id()
{
return firewall_id;
}
/**
* Set the id of firewall associated to report
* @param firewall_id the id of firewall associated to report
*/
public void setFirewall_id(String firewall_id)
{
this.firewall_id = firewall_id;
}
}
public class Firewall
{
private String id;
private String ip;
private String info;
private String name;
/**
* Empty constructor, which instantiates a Firewall specimen without setting default values
*/
public Firewall() {}
/**
* Constructor instantiating a Firewall specimen specifying its initial values
*
* @param id the firewall's id code
* @param ip the firewall's ip code
* @param info the info about firewall
* @param name firewall's name
*/
public Firewall(String id, String ip, String info, String nome)
{
super();
this.id = id;
this.ip = ip;
this.info = info;
this.name = nome;
}
/**
* Return the firewall's id
* @return id
*/
public String getId()
{
return id;
}
/**
* Set firewall's id
* @param id the firewall's id
*/
public void setId(String id)
{
this.id = id;
}
/**
* Return the firewall's ip
* @return ip
*/
public String getIp()
{
return ip;
}
/**
* Set firewall's ip
* @param ip the firewall's ip
*/
public void setIp(String ip)
{
this.ip = ip;
}
/**
* Return firewall's info
* @return info
*/
public String getInfo()
{
return info;
}
/**
* Set firewall's info
* @param info firewall's info fields
*/
public void setInfo(String info)
{
this.info = info;
}
/**
* Return firewall's name
* @return name
*/
public String getName()
{
return name;
}
/**
* Set firewall's name
* @param name firewall's name
*/
public void setName(String name)
{
this.name = name;
}
}
的約束是firewall_Items.firewall_id = firewall.id(所以,這些是我必須用來執行連接的變量)。
現在,如果我想執行這個查詢:
SELECT info, src
FROM firewalls, firewall_items
WHERE firewall_items.firewall_id = firewalls.id;
如何我的Java代碼必須使用JdbcTemplate的? 我應該添加到防火牆類收集FirewallsItems的對象,像一個ArrayList?
注1:我必須使用jdbctemplate項目規範。我無法使用Hibernate或其他樂器。注2:我知道哪些rowmapper和結果集,我regolary使用它們與單個表上的查詢。我所知道的是瞭解如何將它們用於帶有連接的查詢,就像示例一樣。
非常感謝您的回覆!
我建議你遵循1)標準的Java字段命名轉換; 2)使用Lombok並且不要手動生成getter/setter; 3)使用BeanRowPropertyMapper而不是RowMapper實現中的明顯映射 –
首先,感謝回覆。 我的問題不是如何制定字符串查詢;我已經想過要使用連接。 我的困難是如何正確實施RowMapper以及如何爲列表- 選擇一個好的類型。 –
我編輯了我的回覆,這可能會讓你失望。 –