查询代码 in Spring JdbcTemplate
package com.cm.main.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.cm.main.dao.AbstractDao;
import com.cm.main.dao.TestJdbcTemplateDao;
import com.cm.main.bo.User;
public class TestJdbcTemplateDaoImpl
extends AbstractDao
implements TestJdbcTemplateDao {
public void updateDescription(
int id, String description) { JdbcTemplate jt =
new JdbcTemplate(getDataSource()); jt.update(
"update oa_test_1 set description = ? where id = ?",
new Object[] { description,
new Integer(id) }); }
public int getCount() { JdbcTemplate jt =
new JdbcTemplate(getDataSource());
int count = jt.queryForInt(
"select count(*) from oa_test_1");
return count; }
public String getDescription() { JdbcTemplate jt =
new JdbcTemplate(getDataSource()); String name = (String) jt.queryForObject(
"select description from oa_test_1 where id=1", String.
class);
return name; }
public List getTestList(
int id) { String sql =
"select id, description from oa_test_1 where id = ?"; RowMapper mapper =
new RowMapper() {
public Object mapRow(ResultSet rs,
int rowNum)
throws SQLException { User user =
new User(); user.setUserid(
new Integer(rs.getInt(
"id"))); user.setUsername(rs.getString(
"description"));
return user; } }; JdbcTemplate jt =
new JdbcTemplate(
this.getDataSource());
return jt.query(sql,
new Object[] {
new Integer(id) }, mapper); } }
查询代码2public 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; }