日期:2014-05-20  浏览次数:20975 次

ibatis2动态传参的问题
XML code

<select id="queryAllWdByCondition" parameterClass="map"  resultMap="allWdResultMap">       
      select distinct ai.accept_content,pc.app_no,pc.accept_time,si.ser_user,pc.current_nodes,si.linkman,si.linkman_address
    from tbl_accept_info ai,tabl_process pc,tbl_ser_info si
    <dynamic prepend="where">
        ai.app_id=pc.app_no and ai.app_id=si.app_no and si.app_no=pc.app_no
        <isNotEmpty prepend="and" property="appNo">
            ai.app_id = #appNo#
        </isNotEmpty>
        <isNotEmpty prepend="and" property="busiTypeCode">
            si.busi_type_code = #busiTypeCode#
        </isNotEmpty>
        <isNotEmpty prepend="and" property="serUser">
            si.ser_user like '%$serUser$%'
        </isNotEmpty>
        <isNotEmpty prepend="and" property="startAcceptTime">
            pc.accept_time &gt;= #startAcceptTime#
        </isNotEmpty>
        <isNotEmpty prepend="and" property="endAcceptTime">        
            pc.accept_time &lt;= #endAcceptTime#                
        </isNotEmpty>
        <isNotEmpty prepend="and" property="currentNodes">
            pc.current_nodes = #currentNodes#
        </isNotEmpty>
        <isNotEmpty prepend="and" property="linkManAddress">
            si.linkman_address like '%$linkManAddress$%'
        </isNotEmpty>
        <isNotEmpty prepend="and" property="acceptContent">
            ai.accept_content like '%$acceptContent$%'
        </isNotEmpty>
    </dynamic>
    order by app_no
  </select>


上面的动态查询语句,传入的参数都放在了一个map里,在action中只要一往map里传入参数,就会报错
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the tabl_process.countQueryAllWdByCondition-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ai.app_id = '1329803278205'' at line 1
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:201)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForObject(MappedStatement.java:120)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:518)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:493)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:273)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
... 87 more
1、用debug跟踪调试,可以确保参数的确是传了进来,map里面是有参数值的,也可以排除sql语句语法的问题,因为在Mysql数据库里执行过这个sql语句了事正确的;
2、去掉"<isNotEmpty>"等动态拼接语句,这样写
XML code

select distinct ai.accept_content,pc.app_no,pc.accept_time,si.ser_user,pc.current_nodes,si.linkman,si.linkman_address
    from tbl_accept_info ai,tabl_process pc,tbl_ser_info si
where ai.app_id=pc.app_no and ai.app_id=si.app_no and si.app_no=pc.app_no and ai.app_id=#appNo#

这样就不会报错。
那么是否是我ibatis的sqlmap文件里动态拼接语句语法有问题呢,求高手指点

------解决方案--------------------
[code=SQL]<select id="queryAllWdByCondition" parameterClass="map" resultMap="allWdResultMap">