MyBatis+sqlserver 分页带条件模糊查询

很多人在开发中用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="userCode!=null">
			    userCode like '%${userCode}%' and//这是用于拼接字符串的
			</if>
			<if test="phone!=null">
				phone like '%${phone}%' and 
			</if>
			<if test="email!=null">
				email like '%${email}%' and
			</if>		
		 	id not in ( select top ${start} id from fdbt.dbo.fdbt_user
		 	<where>
				<if test="userRole!=null">
			 	    userRole=${userRole}  
			    </if>
			    <if test="userCode!=null">
			        userCode like '%${userCode}%' 
			    </if>
			    <if test="phone!=null">
				    phone like '%${phone}%'  
			    </if>
			    <if test="email!=null">
				    email like '%${email}%' 
			    </if>		
			</where>
		 	 ) order by id 
		</where>

注意点就是sqlserver的字符串拼接可以说是非常的粗暴了,我尝试过很多方法,查了贼多资料也没有这样的写法(也不知道我的对不对,反正运行是一切正常的)

其次需要注意的是嵌套在里面的那个where里的条件和前面的条件相同,不然会出现相当多的奇葩查询,有些也不会报错,但就是各种问题

(菜鸟,大神勿喷)

猜你喜欢

转载自blog.csdn.net/weixin_42401867/article/details/81103813