我正在使用Jdbctemplate从db中检索Bean.这是我的方法.
public List<trackerv3Livedata> getTrackerData() { return List<trackerv3Livedata> live = (List<trackerv3Livedata>) jdbcTemplate.queryForList("select * from mmitrackerv3_livedata where accountid =?",new Object[]{aid},trackerv3Livedata.class); }
并且trackerv3Livedata bean结构正在跟随
public class trackerv3Livedata implements Serializable { /** * */ private static final long serialVersionUID = 2409168269491619888L; private int deviceid; private Long timestamp; private Mmitrackerv3Device mmitrackerv3Device; private Mmitrackerv3Account mmitrackerv3Account; private double latitude; private double longitude; private Double altitude; private Double speedkph; private Double heading; private Double gpssignal; private Integer geozoneid; private Double distancekm; private Double gsmsignal; private Double mainpower; private Integer laststatustime; private Double internalbattry; private Double temperature; private Short dinput1; private Short dinput2; private Short dinput3; private Short dinput4; private Short dinput5; private Short dinput6; private Short dinput7; private Short dinput8; private Short ainput1; private Short ainput2; private Short ainput3; private Short ainput4; private Short doutput1; private Short doutput2; private Short doutput3; private Short doutput4; /* There are Some Getter And Setter Method With Constructor */ }
在我的方案中,完全可能不会得到我的查询,所以我的问题是如何解决以下错误消息.
org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1,actual 38
在我看来,我应该回到null而不是抛出异常.我怎样才能解决这个问题?提前致谢.
解决方法
发生这种情况是因为您使用的queryForList方法不支持多列.从JdbcTemplate查看queryForList的实现
public <T> List<T> More ...queryForList(String sql,Object[] args,Class<T> elementType) throws DataAccessException { return query(sql,args,getSingleColumnRowMapper(elementType)); }
getsingleColumnRowMapper()方法创建一个新的RowMapper,用于从单个列读取结果对象.
您可以使用下面给出的方法.
public <T> List<T> query(String sql,RowMapper<T> rowMapper) throws DataAccessException { return query(sql,new RowMapperResultSetExtractor<T>(rowMapper)); }