关于在mybatis中将表名作为参数传递 [英] About passing table name as a parameter in mybatis
问题描述
当我尝试将表名作为参数传递给sql映射时,例如
public MatchResult get(long id, String tablename);
映射器xml:
<select id="get" parameterType="long" resultType="myresult">
select * from ${1} where id=#{0}
</select>
但是它不起作用.
${}
根据我的测试不支持参数索引.您可以使用Param
批注指定参数映射器API声明中的名称.
public MatchResult get(long id, @Param("tablename") String tablename);
映射器xml:
<select id="get" resultType="myresult">
select * from ${tabelname} where id=#{0}
</select>
一种替代方法是,如果您不想在映射器API声明中使用IBatis/MyBatis
特定注释Param
,则可以使用您自己的类的对象或地图作为参数. >
以地图为例,您的Java API可能是:
public MatchResult get(Map<String, Object> params);
mapper xml语句可以是:
<select id="get" parameterType="map" resultType="myresult">
select * from ${tablename} where id=#{id}
</select>
在调用API之前,使用键"id"和"tablename"将id和tablename放入地图.
When i try to pass a table name as parameter to the sql mapping, such as
public MatchResult get(long id, String tablename);
Mapper xml:
<select id="get" parameterType="long" resultType="myresult">
select * from ${1} where id=#{0}
</select>
But it does not work.
${}
doesn't support the parameter index according to my test. You can use Param
annotation to specify the parameter name in your mapper API declaration.
public MatchResult get(long id, @Param("tablename") String tablename);
Mapper xml:
<select id="get" resultType="myresult">
select * from ${tabelname} where id=#{0}
</select>
An alternative is to use an object of your own class or a map as the parameter if you don't want the IBatis/MyBatis
specific annotation Param
in your mapper API declaration.
Take the map as an example, your java API could be:
public MatchResult get(Map<String, Object> params);
The mapper xml statements could be:
<select id="get" parameterType="map" resultType="myresult">
select * from ${tablename} where id=#{id}
</select>
And put the id and tablename to the map with the key "id" and "tablename", before invoking the API.
这篇关于关于在mybatis中将表名作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!