关于org.springframework.beans.BeanInstantiationException: Failed to instantiate这个异常的处理
//1.定义sql语句
String sql = "select * from account where username = ? and password = ?";
//2.使用JdbcTemplate的queryForObject方法查询并赋值对象
try {
account root = template.queryForObject(sql, new BeanPropertyRowMapper<Root>(Root.class),username, password);
//3.返回root对象
return root;
} catch (Exception e) {
return null;
}
一般我们会使用这样的语句来查询,但是其中的BeanPropertyRowMapper会因为account对象没有空的构造函数而出现异常使查询错误
解决方法就是给account(相应的对象)加上无参构造函数。
完美解决。