problem
The sql query needs to dynamically modify the sort field and the ascending and descending order according to the conditions; at
the beginning, I tried to directly use choose when in sql to judge:
<choose>
<when test="flag== '5'">
order by A_DATE desc
</when>
<otherwise>
order by B_DATE asc
</otherwise>
</choose>
When this method is executed, no judgment can be made.
solution
Use order by ${order} ${orderType} to judge. When passing query conditions, inject the fields that need to be sorted
if(StringUtil.isBlank(isImport) || "5".equals(flag)){
condition.put("order", "A_DATE ");
condition.put("orderType", "desc");
}else if(Constants.VEHICLE_IMPORT_TYPE.RELATEIMPORT.equals(flag)){
condition.put("order", "B_DATE ");
condition.put("orderType", "asc");
}
After execution, sql is order by A_DATE desc or order by B_DATE asc.
This will solve the problem.
note
order by ${order} ${orderType} can’t use
order by #{order} #{orderType}, whether the sql will be order by'A_DATE''desc' or order by'B_DATE''asc' after execution, resulting in sql execution Report an error.