2017-07-24 83 views
0

有人可以幫助我,因爲我在Spring視圖中顯示了內部聯接查詢的結果。結果在表(視圖)中查詢內部聯接(jdbc)Spring MVC

如何返回所有內部連接表中的所有字段並顯示在視圖中?

防爆從模型類:

@NotEmpty(message="Destino não pode estar vazio") 
private Integer idDestinoAgendamento; 
private Integer idMotoristaAgendamento; 
private Integer idAutomovelAgendamento; 
private Integer idRotaAgendamento; 

@NotEmpty(message="Data não pode estar vazia") 
private String dataSolicitacaoPacApp; 

@NotEmpty(message="Hora não pode estar vazia") 
private String horaSolicitacaoPacApp; 

@NotEmpty(message="Status não pode estar vazio") 
private String statusAgendamento; 
private Destino destino; 
private Motorista motorista; 
private Automovel automovel; 

防爆做DAO:

public List buscarTodosAgendamentos() 
{ 
    String sql = "SELECT * FROM bAgendamento AS agendamento INNER JOIN bsDestino AS destino" 
    + " ON agendamento.idDestinoAgendamento = destino.idDestino INNER JOIN" 
    + " bsMotorista AS motorista ON agendamento.idMotoristaAgendamento = motorista.idMotorista " 
    + " INNER JOIN bsAutomovel AS automovel ON agendamento.idAutomovelAgendamento = " 
    + " automovel.idAutomovel INNER JOIN bsRota AS rota ON agendamento.idRotaAgendamento =" 
    + " rota.idRota ORDER BY agendamento.dataSolicitacaoPacAPP ASC " 
    List list = 

     namedParameterJdbcTemplate.query(sql,getSqlParameterByModel(null), new AgendamentoMapper()); 

    return list; 
} 

的視圖(返回對象的字段命運之,Automovel ...):

c:forEach items="${listAgendamento}" var="agendamento" 

${agendamento.idAgendamento} 
${agendamento.cpfPacienteSolicitacaoPacApp} 
${agendamento.idDestinoAgendamento} 
${agendamento.idMotoristaAgendamento} 
${agendamento.idAutomovelAgendamento} 
${agendamento.idRotaAgendamento} 
${agendamento.dataSolicitacaoPacApp} 
${agendamento.horaSolicitacaoPacApp} 
${agendamento.statusAgendamento} 

回答

0
public class SiteJoinCategory { 
private Category category; 
private Site site; 
public Category getCategory() { 
return category; 
} 
public void setCategory(Category category) { 
this.category = category; 
} 
public Site getSite() { 
return site; 
} 
public void setSite(Site site) { 
this.site = site; 
} 

} 
public class Site { 
private Integer siteId; 
private String siteName; 
private Integer categoryId; 

public Integer getSiteId() { 
return siteId; 
} 
public void setSiteId(Integer siteId) { 
this.siteId = siteId; 
} 
public String getSiteName() { 
return siteName; 
} 
public void setSiteName(String siteName) { 
this.siteName = siteName; 
} 
public Integer getCategoryId() { 
return categoryId; 
} 
public void setCategoryId(Integer categoryId) { 
this.categoryId = categoryId; 
} 
} 
public class Category { 
private Integer categoryId; 
private String categoryName; 

public Integer getCategoryId() { 
return categoryId; 
} 
public void setCategoryId(Integer categoryId) { 
this.categoryId = categoryId; 
} 
public String getCategoryName() { 
return categoryName; 
} 
public void setCategoryName(String categoryName) { 
this.categoryName = categoryName; 
} 
} 
@Component 
public class SiteJoinCategoryDao { 

@Resource 
private JdbcTemplate jdbcTemplate; 

public List findAll() { 
String sql = "select site., cat. " + 
"from t_site site inner join m_category cat " + 
"ON site.category_id = cat.category_id"; 
RowMapper rowMapper = new SiteCategoryRowMapper(); 

List list = jdbcTemplate.query(sql, rowMapper); 
return list; 
} 

/** 
* RowMapper 
*/ 
protected class SiteCategoryRowMapper implements RowMapper { 

@Override 
public SiteJoinCategory mapRow(ResultSet rs, int rowNum) throws SQLException { 
SiteJoinCategory siteJoinCategory = new SiteJoinCategory(); 
Site site = new Site(); 
Category category = new Category(); 

site.setSiteId(rs.getInt("site.site_id")); 
site.setSiteName(rs.getString("site.site_name")); 
site.setCategoryId(rs.getInt("site.category_id")); 

category.setCategoryId(rs.getInt("cat.category_id")); 
category.setCategoryName(rs.getString("cat.category_name")); 

siteJoinCategory.setSite(site); 
siteJoinCategory.setCategory(category); 
return siteJoinCategory; 
} 
} 
} 
View (exemplo: 

${siteJoinCategory.site.siteName} 
${siteJoinCategory.category.categoryName}