mybatis中使用ognl的扩展,实现判断传入的字段:
Mapper文件中:
<select id="getRecentQuestionTitle" parameterType="java.lang.String" resultType="java.lang.String">
select title from song_question where questionState = #{value}
<if test="@Ognl@isSolve(value[0],0)">
order by questionTime desc
</if>
<if test="@Ognl@isSolve(value[0],1)">
order by answerTime desc
</if>
limit 0,1
</select>
Ognl.java文件:
/**使用ognl扩展
* @return
*/
public static boolean isSolve(Object o,String soleState){
if(o == null)
return false;
String str = null;
if(o instanceof String[]){
String[]objects = (String[])o;
str = objects[0];
}else if(o instanceof Character){
Character c = (Character) o;
str = Character.toString(c);
}
if(StringUtils.equals(str, soleState))
return true;
return false;
}
该功能为,根据传入的值,
如果值为0,则order by questionTime desc 根据字段questionTime排序。
如果值为1,则order by answerTime desc根据字段answerTime排序。
Ognl.java 必须放在class目录,也就是没有包名。