zoukankan      html  css  js  c++  java
  • Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)易错使用--转载

    原文地址:

    http://blog.csdn.net/will_awoke/article/details/12617383

     一直用ORM,今天用JdbcTemplate再次抑郁了一次。

               首先看下这个方法:

              乍一看,我想传个泛型T(实际代码执行中,这个T可以是我自定义的一个Bean),然后就能返回个List<T>,也即泛型的集合(纯ORM思想啊!殊不知又挖了个大坑~)

              于是乎,出现下面代码:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. List<Student> list = jdbcTemplate.queryForList(sql, Student.class);  


              一执行,发现出异常了:

             ERROR [com.ruhuiyun.studentmanager.aop.LogAdvice] - org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1,     actual 8:Incorrect column count: expected 1, actual 8

             异常很明了,需要一个,给人家整成了八个,也就是人家不是存List的。甚为蹊跷,后一查,发现不是这样的,又跑偏了~

            原来这个T,只支持Integer.class String.class 这种单数据类型的,自己定义的Bean不支持。所以我这ORM的想法over了。

     
            感情如果有封装成某个对象的需求,还得自己动手 。

            这个JdbcTemplate还真是无力吐槽~ 彷佛回来了上古时期~!

           

           update :写了一个轮子,用于 Spring JdbcTemplate 查询结果集Map反向生成Java实体, http://blog.csdn.net/will_awoke/article/details/27683703,有兴趣的可以看下。

    参考文章:

    http://forum.spring.io/forum/spring-projects/data/122343-comparison-between-queryforlist-and-query-working-with-a-list-of-objects

  • 相关阅读:
    分布式文件系统-glusterfs
    Centos7防火墙使用
    Centos7使用Python3
    ldap认证服务的搭建
    kafka使用
    Python基本数据类型
    Linux 内核参数优化
    MHA+atlas(数据库的高可用与读写分离)
    插槽(slot)
    常用组件通信方式
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4511013.html
Copyright © 2011-2022 走看看