MyBatis if判断无效果 新手求帮助!
新手一枚,刚参加社会培训,远程视频教学,很多功能及代码实现都是自已想的,代码不规范请谅解!
进入正题:
欲实现一个根据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
求大神解答!!