很多人在开发中用mysql或是oracle数据库用习惯了(我就是其中之一,初次接触sqlserver),在分页问题上sqlserver和前边说的两个数据库有很大的区别
话不多说,直接上代码
//end和start是传入从第几条开始查到第几条,我这里带着三种模糊条件查询和一个条件查询
<select id="findAllUser" parameterType="map" resultType="user_role">
SELECT top ${end} * from fdbt.dbo.fdbt_user
<where>
<if test="userRole!=null">
userRole=${userRole} and
</if>
<if test="search">
(userCode like '%${search}%' or phone like '%${search}%' or email like '%${search}%') and
<!--这是拼接查询字符的,还有另一种写法'%'+#{search}+'%' 同样也可以实现功能 -->
</if>
id not in ( select top ${start} id from fdbt.dbo.fdbt_user
<where>
<if test="userRole!=null">
userRole=${userRole}
<if test="search!=null">
and
</if>
</if>
<if test="search">
userCode like '%${search}%' or phone like '%${search}%' or email like '%${search}%'
</if>
</where>
order by id ) order by id
</where>
</select>
注意点就是sqlserver的字符串拼接可以说是非常的粗暴了,我尝试过很多方法,查了贼多资料也没有这样的写法(也不知道我的对不对,反正运行是一切正常的)
其次需要注意的是嵌套在里面的那个where里的条件和前面的条件相同,不然会出现相当多的奇葩查询,有些也不会报错,但就是各种问题
(菜鸟,大神勿喷)