当前位置: 首页 > 知识库问答 >
问题:

不正确的列计数:预期为1,实际为38

崔涵亮
2023-03-14

我正在使用 Jdbc 模板从数据库中检索一个 Bean。这是我的方法

public List<trackerv3Livedata>  getTrackerData() {
    return List<trackerv3Livedata> live = (List<trackerv3Livedata>) jdbcTemplate.queryForList("select * from mmitrackerv3_livedata where accountid =?",new Object[]{aid}, trackerv3Livedata.class);
}

跟踪v3活数据豆结构如下:

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,而不是抛出一个异常。我怎样才能解决这个问题?提前谢谢。

共有3个答案

厍浩广
2023-03-14

实现Bean RowMapper接口和maprow函数解决了这个问题

public class Mmitrackerv3LivedataMapper implements RowMapper<Mmitrackerv3Livedata> {

@Override
public Mmitrackerv3Livedata mapRow(ResultSet rs, int rowNum)
        throws SQLException {
}

现在我有了JDBC模板中的更改

 List<Mmitrackerv3Livedata> live = jdbcTemplate.query("select * from mmitrackerv3_livedata mlive " + 
 "join mmitrackerv3_device mdevice on mlive.accountid = mdevice.accountid where mlive.accountid = " +
     aid, new Mmitrackerv3LivedataMapper());

谢谢@abhishek

佟阳云
2023-03-14

发生这种情况是因为您使用的queryForList方法不支持多列。查看JdbcTemplate中queryForList的实现

public <T> List<T> More ...queryForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException 
{
    return query(sql, args, getSingleColumnRowMapper(elementType));
}

方法创建一个新的行映射器,用于从单个列读取结果对象。您可以改用下面给出的方法。

public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException        
 {
    return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
}
苏宏逸
2023-03-14

JdbcTemplate方法

queryForList(String sql, Class<T> elementType)

对于单列查询很有用,您只能指定列类型。如果您需要在结果集中使用多个列,则使用该列会更准确

query(String sql, RowMapper<T> rowMapper)

作为RowMapper的实现,您可以使用自己的,或

jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(clazz));

所以在你的情况下,它可能是:

java prettyprint-override">public List<Trackerv3Livedata> getTrackerData() {
    String sql = "SELECT * FROM mmitrackerv3_livedata mlive " +
                 "JOIN mmitrackerv3_device mdevice ON mlive.accountid = " +
                 "mdevice.accountid WHERE mlive.accountid = " + aid;

    return jdbcTemplate.query(sql,
        new BeanPropertyRowMapper<Trackerv3Livedata>(Trackerv3Livedata.class));
}
 类似资料: