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>
相关推荐
ACGkaka_17 分钟前
SimpleDateFormat 线程安全问题及修复方案
java·jvm·安全
leo_messi9424 分钟前
多线程(五) -- 并发工具(二) -- J.U.C并发包(八) -- CompletableFuture组合式异步编程
android·java·c语言
m0_380113841 小时前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
Gofarlic_OMS2 小时前
SolidEdge专业许可证管理工具选型关键评估标准
java·大数据·运维·服务器·人工智能
清华都得不到的好学生2 小时前
数据结构->1.稀疏数组,2.数组队列(没有取模),3.环形队列
java·开发语言·数据结构
weyyhdke2 小时前
基于SpringBoot和PostGIS的省域“地理难抵点(最纵深处)”检索及可视化实践
java·spring boot·spring
ILYT NCTR2 小时前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
weixin_425023002 小时前
PG JSONB 对应 Java 字段 + MyBatis-Plus 完整实战
java·开发语言·mybatis
不早睡不改名@3 小时前
Netty源码分析---Reactor线程模型深度解析(二)
java·网络·笔记·学习·netty
子非鱼@Itfuture3 小时前
`<T> T execute(...)` 泛型方法 VS `TaskExecutor<T>` 泛型接口对比分析
java·开发语言