ibatis的sqlmap不太会写,帮忙看一下这个错误. 写order by 子句用变量处理出错

ronanljy2 2009-03-10 09:42:26
sqlmap 语句:
<select id="schedule_getScheduleAll" resultClass="java.util.HashMap" parameterClass="java.util.Map">
<![CDATA[
select a.*,b.dept_name as center_dept from tb_schedule a left join tb_dept b on a.center_dept_sn=b.sn
where a.center_dept_sn=#center_dept_sn#
]]>
<dynamic prepend="and">
<isNotEmpty prepend="and" property="a.status">
<![CDATA[ a.status in #status# ]]>
</isNotEmpty>
</dynamic>
<![CDATA[
order by #search_order# desc
]]>

</select>



java action语句:
HashMap map = new HashMap();
map.put("center_dept_sn", "dwdefe234769222");
map.put("search_status", "0,1");
map.put("search_order", "a.create_datetime");
List list0 = (List)this.getScheduleService().getScheduleAll(map);


提示出错:
2009-03-10 09:30:19,968 INFO [org.springframework.jdbc.support.SQLErrorCodesFactory] - SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
2009-03-10 09:30:19,984 WARN [org.apache.struts.action.RequestProcessor] - Unhandled Exception thrown: class org.springframework.jdbc.UncategorizedSQLException
2009-03-10 09:30:19,984 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/blmis].[action]] - Servlet.service() for servlet action threw exception
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation: encountered SQLException [
--- The error occurred while applying a parameter map.
--- Check the schedule_getScheduleAll-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: ORDER BY 子句所标识的第 1 个 SELECT 项包含了一个变量,该变量位于标识列位置的表达式中。只有排序依据表达式引用的是列名时,才允许在该表达式中使用变量。]; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the schedule_getScheduleAll-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: ORDER BY 子句所标识的第 1 个 SELECT 项包含了一个变量,该变量位于标识列位置的表达式中。只有排序依据表达式引用的是列名时,才允许在该表达式中使用变量。
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the schedule_getScheduleAll-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: ORDER BY 子句所标识的第 1 个 SELECT 项包含了一个变量,该变量位于标识列位置的表达式中。只有排序依据表达式引用的是列名时,才允许在该表达式中使用变量。
Caused by: java.sql.SQLException: ORDER BY 子句所标识的第 1 个 SELECT 项包含了一个变量,该变量位于标识列位置的表达式中。只有排序依据表达式引用的是列名时,才允许在该表达式中使用变量。




...全文
197 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
天剑斋主 2009-03-10
  • 打赏
  • 举报
回复
#search_order#里面定义的字段名是不是都已经select出来了呢?
niuxinlong 2009-03-10
  • 打赏
  • 举报
回复
呵呵,sql写的太乱了。
首先, <![CDATA[ 这个东西在每个sql用一次足矣,它的作用lz可以查一下,没记错的话应该是跟大于小于号有关的东西
<dynamic prepend="and"> 还有这个,只提供了个关联符,完全可以换种方式,它在这只能添乱,
<dynamic prepend="and">
<isNotEmpty prepend="and" property="a.status">
这两个一块用,可能是有问题的,两次提供了连接符and
a.status in #status# ]]> ,<isNotEmpty prepend="and" property="a.status">
这个,lz既然是通过HashMap传的参数,那么#status#中间和property="a.status"中的东西因该是HashMap对应的Key键,应该是一个
parameterClass="java.util.Map"
Map没用过,不过最好用HashMap,这样操作起来会简单



ronanljy2 2009-03-10
  • 打赏
  • 举报
回复
查出来了,应该是用$search_order$
## 会自动加上''

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧