SpringBoot2.x系列教程(四十九)Spring Boot中JdbcTemplate源码分析
2020-03-04 22:38
651 查看
本篇文章带大家了解一下SpringBoot中JdbcTemplate的自动配置相关源码分析,同时简单了解一下在使用JdbcTemplate的方法时可能产生的误区。
JDBCTemplate的方法误解
在使用JdbcTemplate时最容易产生误解的就是queryForList方法。该方法的相关源码如下:
@Override public <T> List<T> queryForList(String sql, Class<T> elementType) throws DataAccessException { return query(sql, getSingleColumnRowMapper(elementType)); }
通过方法的定义,我们很容易以为可以用如下的方式进行使用:
@Override public List<Order> findAllError() { return jdbcTemplate.queryForList("select * from tb_order",Order.class); }
但当你真的如此写的时候,你会发现并没有达到预期的目的,反而会抛出异常。
org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 3 at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:110) at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94) at org.springframework.jdbc.
相关文章推荐
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- SpringBoot-Loader源码分析系列2:启动 new JarLauncher().launch(args)的.launch(args)部分
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- SpringBoot-Loader源码分析系列1:启动&读取MANIFEST.MF文件
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
- SpringBoot系列:自动配置源码分析
- Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- springboot系列教程(六)——整合jdbctemplate
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)