About skipping table name as parameter in mybatis

When I try to pass the table name as a parameter to display sql like

public MatchResult get(long id, String tablename);

Mapper xml:

<select id="get" parameterType="long" resultType="myresult">
    select * from ${1} where id=#{0}
</select>

But that will not work.

+4
source share
1 answer

${}does not support parameter index according to my test. You can use annotation Paramto specify the parameter name in the 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 map as a parameter if you do not want to use a IBatis/MyBatisspecific annotation Paramin the mapper API declaration.

Take a map as an example, your java API might be:

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

xml- mapper :

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

id tablename "id" "tablename" API.

+4

All Articles