luc*_*yee 5 parameters mybatis
当我尝试将表名作为参数传递给sql映射时,例如
public MatchResult get(long id, String tablename);
Run Code Online (Sandbox Code Playgroud)
映射器xml:
<select id="get" parameterType="long" resultType="myresult">
select * from ${1} where id=#{0}
</select>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
${}
根据我的测试,不支持参数索引.您可以使用Param
注释在mapper API声明中指定参数名称.
public MatchResult get(long id, @Param("tablename") String tablename);
Run Code Online (Sandbox Code Playgroud)
映射器xml:
<select id="get" resultType="myresult">
select * from ${tabelname} where id=#{0}
</select>
Run Code Online (Sandbox Code Playgroud)
如果您不希望在mapper API声明中使用IBatis/MyBatis
特定注释Param
,则可以使用您自己的类的对象或地图作为参数.
以地图为例,您的Java API可以是:
public MatchResult get(Map<String, Object> params);
Run Code Online (Sandbox Code Playgroud)
mapper xml语句可以是:
<select id="get" parameterType="map" resultType="myresult">
select * from ${tablename} where id=#{id}
</select>
Run Code Online (Sandbox Code Playgroud)
在调用API之前,使用键"id"和"tablename"将id和tablename放到映射中.