Question: I want to parameterize table\_name:t\_user\_address\_book(uid/500000).
for example: when uid = 1000, table\_name = t\_user\_address\_book0;
when uid = 500001, table\_name = t\_user\_address\_book1;
How to write?

```
public interface UserAddressBookMapper {
    @Insert("insert into t_user_address_book? values(...)")
    int upsert(Long uid, UserAddressBookMsg userAddressBookMsg);
}

```

Answer: 
You can choose the table with Mybatis XML code:

```
<choose>
  <when test="uid gt 1000000">
    <bind name="tableName" value="t_user_address_book2" />
  </when>
  <when test="uid gt 500000">
    <bind name="tableName" value="t_user_address_book1" />
  </when>
  <otherwise>
    <bind name="tableName" value="t_user_address_book0" />
  </otherwise>
</choose>

```

Or you can compute the table name in the java and pass it as parameter.

Whatever your choice, the table name parameter in the query must be referenced with the `$` notation instead of `#` since the value must replace the place holder as is to be part of the query and not being interpreted/bound/escaped as parameters are:

```
INSERT INTO ${tableName} ...

```

Despite use of XML, you can stick with annotations surrounding the query with `<script>` tags:

```
@Insert({"<script>", 
         "<choose> ...", 
         "INSERT ..."
         "</script>"
})

```

Also when using Mapper interface with annotations, you need to name the parameters with there are more than 1:

```
@Insert("INSERT INTO table VALUES(#{uid}, #{userAddressBookMsg.propertyName1})")
int upsert(upsert(@Param("uid")Long uid, @Param("userAddressBookMsg") UserAddressBookMsg userAddressBookMsg);

```

However, it seems you want to split into multiple tables for volume issues, this is much complexity to handle while it would better be to keep a single table and look around on DB side about indexing and partitioning.
Quick response will be "no". It is not possible to give table name as parameter, because mybatis uses prepared statements.

I would suggest using table name as variable, and giving it to the statement string.
For example:

```
public interface UserAddressBookMapper { 

static String tableName;
static void setTableName(String name) {
  tableName = name;
}

@Insert({"insert into", tableName, "values(...)"})
int upsert(UserAddressBookMsg userAddressBookMsg);

```

You will have to set `tableName` before calling the method.