我无法在文档中看到任何关于我的问题的内容,并且在部署它时,我的应用程序无法正常工作(稍后会详细介绍)。我正在尝试做类似的事情
<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
select id, name, active, valid
from #{table}
</select>
在 MyBatis 中。我有许多具有共享列的查找表,因此 View 级别的用户决定最终使用哪个查找表。我尝试执行 getLookupRows 时遇到的错误是
Cause: org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter table of statement info.pureshasta.mapper.LookupMapper.getLookupRows
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:77)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:69)
org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:85)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
$Proxy15.getLookupRows(Unknown Source)
info.pureshasta.service.FieldTitleService.getLookupRows(FieldTitleService.java:33)
我的mapper界面如下:
List<Lookup> getLookupRows(@Param("specificColumn") String specificColumn,
@Param("table") String table);
所以我们知道我正在尝试将一个字符串传递给这个查询,没什么特别的。我有特定的专栏,因为那将是我的下一个任务。实际上,每个查找表的其中一列是唯一的,因此我必须调用适当的 specificColumn,但如果表参数和 FROM 子句能够正常工作,我会非常高兴。
请您参考如下方法:
<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
select id, name, active, valid
from ${table}
</select>
成功了。与实际注入(inject)列名和表的值然后说出列值有不同的表示法。如果您在 where 子句中注入(inject)一个值,那么 # 符号是正确的使用方法。
如果此查询中用于表的值未转义,则可能会发生 SQL 注入(inject)问题。对于我的用例,数据库先于我,虽然我可以对 Java 和 View 部分做任何我想做的事,但我不能改变表的基本结构。
如果有人想进一步解释我得到的堆栈跟踪(即 myBatis 思想表是什么类型),我很乐意阅读并接受进一步的教育。