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>
相关推荐
想不明白的过度思考者1 小时前
Spring Boot 配置文件深度解析
java·spring boot·后端
第二只羽毛1 小时前
Java图书管理系统的设计与实现
java·大数据·安全·系统安全
cike_y6 小时前
Mybatis之解析配置优化
java·开发语言·tomcat·mybatis·安全开发
是一个Bug8 小时前
Java基础50道经典面试题(四)
java·windows·python
Slow菜鸟8 小时前
Java基础架构设计(三)| 通用响应与异常处理(分布式应用通用方案)
java·开发语言
我是Superman丶8 小时前
《Spring WebFlux 实战:基于 SSE 实现多类型事件流(支持聊天消息、元数据与控制指令混合传输)》
java
廋到被风吹走8 小时前
【Spring】常用注解分类整理
java·后端·spring
是一个Bug8 小时前
Java基础20道经典面试题(二)
java·开发语言
Z_Easen8 小时前
Spring 之元编程
java·开发语言
leoufung9 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode