MyBatis if判断无效果 新手求帮助!

l371890934 2015-09-15 09:34:17
新手一枚,刚参加社会培训,远程视频教学,很多功能及代码实现都是自已想的,代码不规范请谅解!
进入正题:
欲实现一个根据select下拉列表中的value值进行排序,当select无选中时整个表进行排序。现在问题是依据下拉列表进行排序正常,select无选中项再点击排序无效果
代码如下:
DAO代码:
//依据传入的order(列名) 自定义类Cost进行排序
public List<Cost> orderDesc(@Param("order") String order,@Param("cost")Cost cost);
XML文件:
<select id="orderDesc" resultType="com.entity.Cost">
select cost_id as costId,name,base_duration as baseDuration,base_cost as baseCost,
unit_cost as unitCost,status,descr,creatime,startime,cost_type as costType
from cost
<choose>
<when test="#{cost.costType}!=null">
where cost_type=#{cost.costType} order by ${order} desc
</when>
<otherwise>
order by ${order} desc
</otherwise>
</choose>
</select>
想要的效果是当 cost.costType为null时 直接order By 列名 排序
两次代码执行时log4j的输出是这样的:
select选中时:
DEBUG - ==> Preparing: select cost_id as costId,name,base_duration as baseDuration,base_cost as baseCost, unit_cost as unitCost,status,descr,creatime,startime,cost_type as costType from cost where cost_type=? order by baseCost desc
DEBUG - ==> Parameters: 1(String)
select无选中时:
DEBUG - ==> Preparing: select cost_id as costId, name, base_duration as baseDuration, base_cost as baseCost, unit_cost as unitCost, status, descr, creatime, startime, cost_type as costType from cost where cost_type=? order by baseCost asc
DEBUG - ==> Parameters: null(String)
DEBUG - <== Total: 0

求大神解答!!
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
suciver 2015-09-16
  • 打赏
  • 举报
回复
<when test="#{cost.costType}!=null">不要用#{}在标签中直接用<when test="cost.costType!=null"> #{}是用在sql中的标签中不用
火阳邪神 2015-09-16
  • 打赏
  • 举报
回复
XML中判断不用#{} ,sql中不用cost.costType,直接costType
小雷同学 2015-09-16
  • 打赏
  • 举报
回复
还有 似乎可以直接写costType 不用写cost.costType的吧?
  • 打赏
  • 举报
回复
if在哪………………?
  • 打赏
  • 举报
回复
恩,楼上正解,标签里面不用#{} 直接写属性名称判断就行了

679

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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