Mybatis中limit用法与分页查询

错误示范

错误示范一:
java 复制代码
<select id="fileInspectionList" resultType="map">
		SELECT <include refid="aip_n_static_cols"/>
		FROM sys_inspection_form  WHERE
		<if test=" type == 'admin'.toString() ">
			dept_id = #{deptid}
			order by id
			limit  #{start},#{pageSize}
		</if>
		<if test=" type != 'admin'.toString() ">
			dept_id = #{deptid}
			AND status='已发布'
			or user_id = #{userid}
			order by id
			limit  #{start},#{pageSize}
		</if>
	</select>
错误示范二:
java 复制代码
<select id="fileInspectionList" resultType="map">
		SELECT <include refid="aip_n_static_cols"/>
		FROM sys_inspection_form  WHERE
		<if test=" type == 'admin'.toString() ">
			dept_id = #{deptid}
			order by id
			limit = #{start},#{pageSize}
		</if>
		<if test=" type != 'admin'.toString() ">
			dept_id = #{deptid}
			AND status='已发布'
			or user_id = #{userid}
			order by id
			limit = #{start},#{pageSize}
		</if>
	</select>

这里先要了解一下:

#{}和${}的区别:

#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换。#{}可以有效防止sql注入。 #{}可以接收简单类型值或pojo属性值。 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。

{}表示拼接sql串,通过{}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, {}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,{}括号中只能是value。

正确写法:

java 复制代码
<select id="fileInspectionList" resultType="map">
		SELECT <include refid="aip_n_static_cols"/>
		FROM sys_inspection_form  WHERE
		<if test=" type == 'admin'.toString() ">
			dept_id = #{deptid}
			order by id
			limit  ${start},${pageSize}
		</if>
		<if test=" type != 'admin'.toString() ">
			dept_id = #{deptid}
			AND status='已发布'
			or user_id = #{userid}
			order by id
			limit  ${start},${pageSize}
		</if>
	</select>
相关推荐
J_liaty18 分钟前
Spring Boot拦截器与过滤器深度解析
java·spring boot·后端·interceptor·filter
亲爱的非洲野猪1 小时前
Java锁机制八股文
java·开发语言
rgeshfgreh1 小时前
C++字符串处理:STL string终极指南
java·jvm·算法
Zoey的笔记本1 小时前
「支持ISO27001的GTD协作平台」数据生命周期管理方案与加密通信协议
java·前端·数据库
lpfasd1231 小时前
Spring Boot 4.0.1 时变更清单
java·spring boot·后端
N***H4861 小时前
SpringBoot3.3.0集成Knife4j4.5.0实战
java
程序员欣宸2 小时前
LangChain4j实战之十三:函数调用,低级API版本
java·人工智能·ai·langchain4j
Java新手村2 小时前
【订单超时取消怎么设计】
java
阿蒙Amon3 小时前
C#每日面试题-常量和只读变量的区别
java·面试·c#
寻星探路3 小时前
【算法专题】滑动窗口:从“无重复字符”到“字母异位词”的深度剖析
java·开发语言·c++·人工智能·python·算法·ai