您可以選擇使用MyBatis的XML代碼表:
<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>
或者你可以在java計算表名和它傳遞的參數。
無論你選擇什麼,在查詢中的表名參數必須與$
符號代替#
引用因爲該值必須替換佔位符的是要查詢的一部分,而不是被解釋/綁定/轉義參數:使用映射器接口W¯¯時
@Insert({"<script>",
"<choose> ...",
"INSERT ..."
"</script>"
})
另外:
INSERT INTO ${tableName} ...
儘管使用XML,你可以與周圍的註釋與<script>
標記的查詢棒第i個註解,您需要命名的參數與有超過1:
@Insert("INSERT INTO table VALUES(#{uid}, #{userAddressBookMsg.propertyName1})")
int upsert(upsert(@Param("uid")Long uid, @Param("userAddressBookMsg") UserAddressBookMsg userAddressBookMsg);
然而,它似乎要拆分成卷問題多個表,這是非常複雜處理,同時也將更好地是保留一張表,並在數據庫端查看索引和分區。
第一條語句完全錯誤且代碼片段不能編譯,它不能:未初始化靜態字段+方法與接口中的主體 – blackwizard