百度站长自动推送wordpress郑州360房产网查询
概括
在最近的开发任务中,涉及到了一些页面的排序,其中最为常见的就是时间的降序和升序。这个有的前端控件就可以完成,但是对于一些无法用前端控件的,只能通过后端来进行解决。
后端的解决方法就是使用mybatis的动态sql拼接。
如何使用
在mybatis中提供了丰富的动态sql拼接的标签,这里采用我们常用的,where、if、choose 、when
 来进行完成。
代码准备
这里先声明一下,可以和前端约定传递的字段,例如用field字段来接受需要排序的字段、用order字段来接收是升序还是降序。
好了,到了这里可以准备书写mybatis的文件了。
假设我们拿到了前端传递过来的数据。
public List<Student> selectStudentList(String field , String order)
 
<select id="selectStudentList" resutlt="Student">select * form student <where>//查询条件</where><choose><if test="field != null and field != '' and order != null and order != '' ">order by<when test="field =='createTime' ">create_time</when>	<when test="field =='updateTime' ">update_time</when>	<when test="order=='desc' ">desc</when>	<when test="order=='asc' ">asc</when>	</if></choose>
<select>
 
这样就可以通过前端的传递的值来动态的进行查询展示排序了。
