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>
相关推荐
会编程的林俊杰16 分钟前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet29 分钟前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
百***628540 分钟前
MySQL 常用 SQL 语句大全
数据库·sql·mysql
百***6971 小时前
MySQL数据库(SQL分类)
数据库·sql·mysql
王家羽翼-王羽1 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
影子24012 小时前
oralce创建种子表,使用存储过程生成最大值sql,考虑并发,不考虑并发的脚本,plsql调试存储过程,java调用存储过程示例代码
java·数据库·sql
武子康2 小时前
Java-172 Neo4j 访问方式实战:嵌入式 vs 服务器(含 Java 示例与踩坑)
java·服务器·数据库·sql·spring·nosql·neo4j
程序猿DD2 小时前
深入探索剖析 JVM 的启动过程
java
Arva .2 小时前
ConcurrentHashMap 的线程安全实现
java·开发语言