spring框架学习记录 5 jdbcTemplate出现异常记录

在测试jdbctemplate的query方法和queryForObject方法时 按要求传参 报错

代码

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("jdbcTemplate.xml");
        JdbcTemplate jdbcTemplateBean = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        StringBuilder sql = new StringBuilder();
        sql.append("select id,name,tel,createTime from tb_user where id = '1' ");
        RowMapper rowMapper = new BeanPropertyRowMapper<>(TbUser.class);
        List query = jdbcTemplateBean.query(sql.toString(), rowMapper);
        TbUser tbUser = jdbcTemplateBean.queryForObject(sql.toString(), rowMapper);
        System.out.println(query.toString());
        System.out.println(tbUser);

报错信息

Exception in thread "main" org.springframework.beans.BeanInstantiationException: Failed to instantiate [jdbcTemplate.entity.TbUser]: Is it an abstract class?; nested exception is java.lang.InstantiationException: jdbcTemplate.entity.TbUser
	at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:81)
	at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:236)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
	at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:455)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:400)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:476)
	at jdbcTemplate.test.Main.main(Main.java:18)
Caused by: java.lang.InstantiationException: jdbcTemplate.entity.TbUser
	at java.lang.Class.newInstance(Class.java:427)
	at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78)
	... 8 more
Caused by: java.lang.NoSuchMethodException: jdbcTemplate.entity.TbUser.()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.newInstance(Class.java:412)
	... 9 more

关键信息nested exception is java.lang.InstantiationException

网上搜索问题给到的答案是在把查询结果映射成实体类时,无法调用实体类的无参构造方法,在TbUser表中加无参构造方法后问题解决

你可能感兴趣的:(spring框架学习记录 5 jdbcTemplate出现异常记录)