各位GG.JJ们好:
   最近小弟在学ibatis框架,自己在写一个论坛的web项目,这段时间遇到了个问题,辗转反侧的折磨了我很久,在实在没有独自解决的办法下,只有请求大家的力量了.谢谢各位先
  来说一下问题吧, 我现在在用ibatis查询的时候,查一张表的记录,在xml中配置如下:
  <typeAlias alias="topVO" type="com.xxx.vo.TopicVO"/>
  <resultMap class="topVO" id="topMap">
<result property="topId" column="TOP_ID"/>
<result property="topSid" column="TOP_SID"/>
<result property="topUid" column="TOP_UID"/>
<result property="topName" column="TOP_NAME"/>
<result property="topText" column="TOP_TEXT"/>
<result property="topTime" column="TOP_TIME"/>
<result property="topCountclick" column="TOP_COUNTCLICK"/>
<result property="topReplacecount" column="top_replycount"/>
<result property="topPy" column="TOP_PY"/>
<result property="topBiaoqing" column="TOP_BIAOQING"/>
<result property="topFlag" column="TOP_FLAG"/>
<result property="isDelete" column="is_delete"/>
<result property="createDate" column="create_date"/>
<result property="createBy" column="create_by"/>
<result property="lastUpdateBy" column="last_update"/>
<result property="lastUpdateDate" column="lasg_logindate"/>
</resultMap>
   当我执行以下这条sql:
<select id="getTopinfoById" parameterClass="java.lang.String" resultMap="topMap">
select t.top_id,
       t.top_sid,
       t.top_uid,
       t.top_name,
       t.top_text,
       t.top_time,
       t.top_countclick,
       t.top_replycount,
       t.top_biaoqing,
       t.top_flag,
       t.create_date   
  from topic t where t.is_delete=1 and t.top_id=#sid#
</select>
出现列名无效的异常,javax.servlet.ServletException: org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/sie/config/bbs.xml.  
--- The error occurred while applying a result map.  
--- Check the bbs.topMap.  
--- Check the result mapping for the 'topPy' property.  
--- Cause: java.sql.SQLException: 列名无效
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.sie.filter.Chinese.doFilter(Chinese.java:26)
但是当我执行这条sql的时候却能成功:
<select id="showTopic" parameterClass="java.lang.String" resultMap="topMap">
select top_id,
       t.top_sid,
       t.top_uid,
       t.top_name,
       t.top_text,
       t.top_time,
       t.top_py,
       t.top_countclick,
       t.top_replycount,
       t.top_biaoqing,
       t.top_flag,
       t.is_delete,
       t.create_date,
       t.create_by,
       t.last_update,
       t.lasg_logindate
  from topic t , sessioninfo s where t.top_sid = #sid# and t.is_delete=1 and t.top_sid = s.session_id
</select>
2条sql的区别在于查询的字段不同,以及查询条件不同.当我将下面这条查询的列名复制到与第一条报错的sql中时,却又不报错,想请问,这是需要配置什么才能不出现这种情况呢? 小弟愚昧.还想请大家能帮帮我,或者是能够说出你们的想法. 谢谢大家了

解决方案 »

  1.   

    其实,提示信息挺清楚的啊:
    --- Check the result mapping for the 'topPy' property. 
    --- Cause: java.sql.SQLException: 列名无效就是说'topPy'没找到对应字段呗
      <result property="topPy" column="TOP_PY"/>而你的语句里面,确实没有top_py这个字段:
    select t.top_id,
       t.top_sid,
       t.top_uid,
       t.top_name,
       t.top_text,
       t.top_time,
       t.top_countclick,
       t.top_replycount,
       t.top_biaoqing,
       t.top_flag,
       t.create_date   
      from topic t where t.is_delete=1 and t.top_id=#sid#
    其实偷懒点,对于单表查询的,直接:
      Select * From topic Where is_delete=1 And top_id=#sid#
      

  2.   

    确实是这样,
    不过我想要的处理结果是, 没有查询TOP_PY字段的时候, 其他的字段能够映射到,
    如果这张表有多个sql语句的话,每个sql查询的字段又不一样,那么性能会受到影响;
    如果使用 SELECT * 的查询方式也会对SQL的性能有影响额.
    谢谢你哦.嘿嘿继续等待GG JJ们回复的参考意见
      

  3.   

    自己Ding一个, 求解决 , 
      

  4.   

    我正好最近也在使用,看了下你的配置,建议如下,供参考
    1.像你那样说查出自己需要的字段,这个支持我也是那样做的,确实可以实现,但是你的配置resultMap没有     <id column="" property="" jdbcType="" />主键,....不清楚跟这个有关吗?2.实在不行再定义一个resultMap,呵呵 祝你好运
      

  5.   

    resultMap 本来就是固定结构,sql不固定还用它不是吃饱了撑的吗。用resultClass,字段名和属性名之间的差异 用SQL别名搞定。
      

  6.   


    按照你的理解,是否是可以理解为 一个resultMap只能对应一条sql?
    这样有些说不过去吧?
      

  7.   

    一个resultMap用来对应一种结果集规则
    动态填写,用类反射另:ibatis已经过期,现在学的话应该看mybatis