如何在 mybatis xml 中基于變量值執(zhí)行動(dòng)態(tài) sql
mybatis 提供了多種方法來(lái)根據(jù)變量值動(dòng)態(tài)執(zhí)行 sql 語(yǔ)句。
使用數(shù)據(jù)庫(kù)廠商標(biāo)識(shí)
mybatis 具有內(nèi)置的數(shù)據(jù)庫(kù)廠商標(biāo)識(shí),您可以使用它來(lái)指定不同的 sql 語(yǔ)句,具體取決于所使用的數(shù)據(jù)庫(kù)類(lèi)型。例如:
<select id="selectone" databaseid="mysql"> ... </select> <select id="selectone" databaseid="dameng"> ... </select>
登錄后復(fù)制
使用 if 標(biāo)簽
您還可以使用 if 標(biāo)簽根據(jù)變量值有條件地執(zhí)行 sql 語(yǔ)句。例如:
<select id="selectone"> <if test="databasetype == 1"> ... </if> <if test="databasetype == 2"> ... </if> </select>
登錄后復(fù)制
使用 choose 標(biāo)簽
choose 標(biāo)簽允許您根據(jù)多個(gè)條件執(zhí)行不同的 sql 語(yǔ)句。例如:
<select id="selectOne"> <choose> <when test="databaseType == 1"> ... </when> <when test="databaseType == 2"> ... </when> <otherwise> ... </otherwise> </choose> </select>
登錄后復(fù)制