您的位置:首页 > 产品设计 > UI/UE

Column count doesn't match value count at row 1 问题Column count doesn't match value count at row 1

2017-08-21 17:59 645 查看
今天在搞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);
}

这样查出来的就不会出问题了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: