在 MyBatis 中动态表名查询可以通过使用 MyBatis 的动态 SQL 功能来实现。具体实现方法如下:
public interface YourMapper {
List<YourDataType> selectDataByTableName(@Param("tableName") String tableName);
}
<choose>
和 <when>
标签来实现根据不同条件选择不同的表名:<select id="selectDataByTableName" resultType="YourDataType">
SELECT * FROM
<choose>
<when test="tableName == 'table1'">
table1
</when>
<when test="tableName == 'table2'">
table2
</when>
</choose>
</select>
YourMapper yourMapper = sqlSession.getMapper(YourMapper.class);
List<YourDataType> data = yourMapper.selectDataByTableName("table1");
通过以上步骤,就可以实现在 MyBatis 中根据动态表名进行查询数据的功能。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: mybatis set标签内部为空怎么解决