通过MyBatis中的参数设置FROM子句

dem*_*lem 2 ibatis dynamic-queries mybatis

我没有在文档中看到任何与我的问题有关的内容,在部署它时,我的应用程序运行不正常(更多内容在一秒内).我想做点什么

<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
   select id, name, active, valid
   from #{table}
</select>
Run Code Online (Sandbox Code Playgroud)

在MyBatis.我有许多具有共享列的查找表,因此视图级别的用户确定最终使用哪个查找表.我尝试执行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)
Run Code Online (Sandbox Code Playgroud)

我的mapper界面如下:

List<Lookup> getLookupRows(@Param("specificColumn") String specificColumn, 
                           @Param("table") String table);
Run Code Online (Sandbox Code Playgroud)

所以我们知道我正在尝试将String传递给此查询,没什么特别的.我有专门的专栏,因为这将是我的下一个任务.实际上每个查找表的其中一列是唯一的,因此我必须调用相应的specificColumn,但如果我能使table参数和FROM子句工作,我会非常高兴.

dem*_*lem 8

<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
   select id, name, active, valid
   from ${table}
</select>
Run Code Online (Sandbox Code Playgroud)

诀窍.实际注入列名称和表的值然后说明列值有不同的表示法.如果要在where子句中注入值,则#符号是正确的.

如果此查询中用于表的值未转义,则可能发生SQL注入问题.对于我的用例,数据库在我之前,虽然我可以做任何我想要的Java和View部分,但我不允许改变表的基本结构.

如果有人想进一步解释我得到的堆栈跟踪(即myBatis认为表是什么类型),我很乐意阅读并接受进一步的教育.