版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/46681509
Spring JDBC提供了一个BeanPropertyRowMapper的实现,能够自动将数据库表中读出的字段与Domain对象的属性进行映射
public List<GoodsListBean> findGoodsListBean(String goodsnum, Integer vmid, Integer omid, String rusjstart, String rusjend) throws Exception
{
StringBuilder sbstr = new StringBuilder( "SELECT g.*,u.name acceptancename,c.name companyname,vm.name vmname" + " ,om.name omname,cc.name contactsname FROM goods g,pus_sys_user u,companyinfors c " + " ,variety_management vm,origin_management om ,companycontacts cc WHERE g.acceptanceid=u.id" + " AND g.companyid=c.id AND g.vmid=vm.id AND g.omid=om.id " + " AND g.contactsid=cc.id AND g.state=1");
List<GoodsListBean> list = jdbcTemplate.query(sbstr.toString(), new BeanPropertyRowMapper(GoodsListBean.class));
return list;
}
jdbctemplate 返回map或list:
List sampleClassList = jdbcTemplate.query(query, new Object[]{parameters.....},
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
SampleClass sampleClass = new SampleClass();
sampleClass.setValue1(rs.getString("column1"));
sampleClass.setValue2(rs.getString("column2"));
return sampleClass;
}
});
Map map = (Map)jdbcTemplate.query(query, new Object[]{parameters....},
new ResultSetExtractor() {
public Object extractData(ResultSet rs) throws SQLException {
Map map = new LinkedHashMap();
while (rs.next()) {
String col1 = rs.getString("col1");
String col2 = rs.getString("col2");
map.put(col1, col2);
}
return map;
};
});