繁体   English   中英

关于在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>

如果您不希望Mapper API声明中包含IBatis/MyBatis特定注释的Param ,则可以使用您自己的类的对象或地图作为参数。

以地图为例,您的Java API可能是:

public MatchResult get(Map<String, Object> params);

映射器xml语句可以是:

<select id="get" parameterType="map" resultType="myresult">
    select * from ${tablename} where id=#{id}
</select>

并在调用API之前,使用键“ id”和“ tablename”将id和tablename放入映射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM