1、需求描述(常用与拆表场景)
假设有拆分场景的一些表,如:
t_test_1
t_test_2
...
t_test_n
那么如何在mapper
和xml
中配置实现自定义表名,查询到对应表里边的数据呢?
2、代码实现
(1)Mapper类写法
@Mapper
public interface TestMapper{
//param为常规查询参数 tableName为自定义的表名
List<Map<String, Object>> selectMyTest(String param, String tableName);
}
(2)xml配置
<select id="selectMyTest" resultType="java.util.Map">
select * from ${tableName} where param = #{param}
</select>
(3)测试代码
String param = "1001";
String tablePrefix = "t_test_2";
List<Map<String, Object>> resList = testMapper.selectMyTest(param, tableName);