1. 程式人生 > >Column count doesn't match value count at row 1 問題Column count doesn't match value count at row 1

Column count doesn't match value count at row 1 問題Column count doesn't match value count at row 1

今天在搞Spring的時候發生的問題 ,自己是剛接觸spring ,然後找了找解決了問題 

這個問題的意思是 ,查出N列資料但是他只需要一列資料,spring不會自動封裝到Bean中

解決方案,就是寫一個類去實現RowMapper介面,然後手動的去給他set值,然後返回student

這裡的student就是自己定義的Bean

public class MyRowMapper implements RowMapper<Student>{


//在這裡resultSet只是一行資料

//在這裡容器會自動的來遍歷
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAge(rs.getInt(3));
return student;
}

}

下面來看查詢的方法

這裡的new MyRowMapper()就是我們實現rowmapper介面的類

@Override
public List<Student> selectAllStudents() {
return this.getJdbcTemplate().query("select * from student", new MyRowMapper());

}


@Override
public Student selectStudentById(int id) {
return  this.getJdbcTemplate().queryForObject("select * from student where id = ?", new MyRowMapper(),id);
}

這樣查出來的就不會出問題了