- packagecom.cm.main.dao.impl;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.util.List;
- importorg.springframework.jdbc.core.JdbcTemplate;
- importorg.springframework.jdbc.core.RowMapper;
- importcom.cm.main.dao.AbstractDao;
- importcom.cm.main.dao.TestJdbcTemplateDao;
- importcom.cm.main.bo.User;
- publicclassTestJdbcTemplateDaoImplextendsAbstractDaoimplements
- TestJdbcTemplateDao{
- publicvoidupdateDescription(intid,Stringdescription){
- JdbcTemplatejt=newJdbcTemplate(getDataSource());
- jt.update("updateoa_test_1setdescription=?whereid=?",newObject[]{
- description,newInteger(id)});
- }
- publicintgetCount(){
- JdbcTemplatejt=newJdbcTemplate(getDataSource());
- intcount=jt.queryForInt("selectcount(*)fromoa_test_1");
- returncount;
- }
- publicStringgetDescription(){
- JdbcTemplatejt=newJdbcTemplate(getDataSource());
- Stringname=(String)jt.queryForObject(
- "selectdescriptionfromoa_test_1whereid=1",String.class);
- returnname;
- }
- publicListgetTestList(intid){
- Stringsql="selectid,descriptionfromoa_test_1whereid=?";
- RowMappermapper=newRowMapper(){
- publicObjectmapRow(ResultSetrs,introwNum)throwsSQLException{
- Useruser=newUser();
- user.setUserid(newInteger(rs.getInt("id")));
- user.setUsername(rs.getString("description"));
- returnuser;
- }
- };
- JdbcTemplatejt=newJdbcTemplate(this.getDataSource());
- returnjt.query(sql,newObject[]{newInteger(id)},mapper);
- }
- }
查询代码2
public Map getSumCountAllTop48(Date beginDate, Date endDate) {
Map map = null;
try {
map = (Map) this.query(strsqlByDay, new Object[] { beginDate,
endDate }, new ResultSetExtractor() {
public Object extractData(ResultSet rs) throws SQLException,
DataAccessException {
// TODO Auto-generated method stub
Map map = new HashMap();
if (rs != null) {
long oldId = 0;
long sum = 0;
long count = 0;
while (rs.next()) {
long newId = rs.getLong("id");
long value = rs.getLong("value");
if (oldId != newId) {
ChannelStatVO vo = new ChannelStatVO();
vo.getReg().setId(oldId);
vo.setSum(sum);
vo.setCount(count);
map.put(oldId, vo);
sum = 0;
count = 0;
oldId = newId;
}
if (count < 48) {
count++;
sum = sum + value;
} else {
continue;
}
}
ChannelStatVO vo = new ChannelStatVO();
vo.getReg().setId(oldId);
vo.setSum(sum);
vo.setCount(count);
map.put(oldId, vo);
}
return map;
}
});
} catch (Exception e) {
// TODO: handle exception
map = new HashMap();
System.out.println(e.getMessage());
logger.error("getSumCountAllTop48:" + e.getMessage(), e);
} finally {
}
return map;
}