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>
相关推荐
Lris-KK几秒前
【Leetcode】高频SQL基础题--1341.电影评分
sql·leetcode
B612 little star king9 分钟前
力扣29. 两数相除题解
java·算法·leetcode
野犬寒鸦10 分钟前
力扣hot100:环形链表(快慢指针法)(141)
java·数据结构·算法·leetcode·面试·职场和发展
上官浩仁15 分钟前
springboot synchronized 本地锁入门与实战
java·spring boot·spring
Gogo81617 分钟前
java与node.js对比
java·node.js
SmartJavaAI23 分钟前
Java调用Whisper和Vosk语音识别(ASR)模型,实现高效实时语音识别(附源码)
java·人工智能·whisper·语音识别
用户37215742613526 分钟前
Python 高效实现 Word 转 PDF:告别 Office 依赖
java
Qlittleboy30 分钟前
tp5的tbmember表闭包查询 openid=‘abc‘ 并且(wx_unionid=null或者wx_unionid=‘‘)
数据库·sql·php
渣哥31 分钟前
Java ThreadPoolExecutor 动态调整核心线程数:方法与注意事项
java
Miraitowa_cheems42 分钟前
LeetCode算法日记 - Day 38: 二叉树的锯齿形层序遍历、二叉树最大宽度
java·linux·运维·算法·leetcode·链表·职场和发展