MyBatis中使用#{}和${}占位符传递参数的各种报错信息处理

在Mapper层使@Select注解进行SQL语句查询时,往往需要进行参数传入和拼接,一般情况下使用两种占位符#{参数名}和${参数名},两者的区别为:

一、两种占位符的区别

1、参数传入方式的区别

#{}是预编译处理,后台输出的日志会将SQL语句中的#{}占位符输出为?,将传入的Parameter传入SQL语句。

{}是字符串硬替换,会直接将传入的参数直接替换{}占位符,不进行预处理。有SQL注入的风险。

2、参数传入后处理的区别

#{}传入参数后,会自动给参数加上' '(引号),例如:

复制代码
@Select("select name from user where id = #{id}")
String queryNameById(String id);

在传入id为1001之后,输出的sql为:

复制代码
select name from user where id ='1001'

**${}传入的参数会硬替换字符串,不会有其他处理,**例如:

复制代码
@Select("select name from user where id = ${id}")
String queryNameById(String id);

在传入id为1001后输出的sql是:

复制代码
select name from user where id = 1001

参数会直接替换${}而不进行其他处理,如果这里你需要给参数加上' ',则需要这么修改代码:

复制代码
@Select("select name from user where id = '${id}'")
String queryNameById(String id);

这样进行替换后的sql就会变为参数加引号的sql语句。

二、常见报错处理

1、索引超出范围

复制代码
详细报错为:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。] with root cause

日志信息报错索引超出范围,可能是因为语句拼接后出现语法错误,往往造成该错误的是对语句中占位符处的引号处理问题。例如:

复制代码
@Select("select  kunnr,name1 from openquery(hana2,'select top 10 * from SAPHANADB.kna1 where name1 like ''%${name}%'' and name1 not like ''冻结''')")
List<Biz> queryBizListByName(String name);

如果这里使用#{}进行占位符,那么组成的sql会变成 like ''%'name'%'' and,参数会多一个' '进行包裹,语法就会出错。所以不管是使用concat进行拼接,还是直接进行替换,使用两种占位符时都要根据其使用特点,注意包裹的' ',来达到符合自己SQL语法的使用。在出现"索引超出范围"的报错时,可以通过检查自己sql的语法是否出错,来看看是否可以解决问题。

2、"@P0"附近有语法错误

复制代码
详细报错为:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.microsoft.sqlserver.jdbc.SQLServerException: "@P0"附近有语法错误。

日志报这个错误,可能是由于你的sql语句中,使用了不支持#{}占位符的函数,例如Top和Order By等函数,是不支持使用#{}占位符的,可以将#{}改为{},使用字符串替换可以解决问题。但要注意#{}改为{}时引号包裹引起的语法问题。

提醒:代码中尽量使用#{}占位符,尽量避免使用${}占位符,因为#{}会更加安全。

相关推荐
小马爱打代码17 分钟前
Spring AI 实战:Agent 基础搭建与核心能力解析
java·人工智能·spring
csdn2015_18 分钟前
springboot task
java·spring boot·后端
czlczl2002092528 分钟前
Spring Boot :如何高性能地在 Filter 中获取响应体(Response Body)
java·spring boot·后端
sg_knight41 分钟前
抽象工厂模式(Abstract Factory)
java·python·设计模式·抽象工厂模式·开发
春日见42 分钟前
win11 分屏设置
java·开发语言·驱动开发·docker·单例模式·计算机外设
2301_780029041 小时前
支付宝sdk导入错误
java·开发语言·maven
码界奇点1 小时前
基于Spring Boot和Vue3的无头内容管理系统设计与实现
java·spring boot·后端·vue·毕业设计·源代码管理
九皇叔叔1 小时前
【03】微服务系列 之Nacos 注册中心(服务注册)
java·微服务·nacos·架构·注册中心·服务注册
木辰風2 小时前
PLSQL自定义自动替换(AutoReplace)
java·数据库·sql
heartbeat..2 小时前
Redis 中的锁:核心实现、类型与最佳实践
java·数据库·redis·缓存·并发