Mybatis-plus配置 动态表名
前几天做了datax的定时增量同步,今天要做动态表名,前缀都一样data,但是_后面是按照yyyyMMdd的日期,格式:data_20220530....data_220220531....data_20220601....
我在网上找呀找,不是说一半就是不能用,要不然就是要收钱...现在把具体实现分享给大家,希望能帮到你们
为了执行定时自动同步时能知道数据源是哪个表,所以要匹配当前的表,获取系统日期来拼接,如果是从自定时间开始的话就在配置文件里配置
下面是一个简单的表名组装,组装前缀+时间作为完整表名返回
@Component public class TableNameAssemble { @Value("${table.nameprefix}") private String namePrefix; @Value("${table.time}") private String tableTime; //表名组装,先读取配置文件日期,如日期已配置,则组装表名返回 //如日期未配置,读取本地时间,格式化yyyyMMdd后拼接前缀,最终结构:tableName_yyyyMMdd //调用后将表名返回 public String getSystemctlTime() { SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); return df.format(System.currentTimeMillis()); } public String assembleTableName() { if(Strings.isNullOrEmpty(tableTime)){ String systemctlTime = getSystemctlTime(); return namePrefix + systemctlTime; }else { return namePrefix + tableTime; } }
接下来就是配置动态表名了,只需要在调用的时候传入就可以
首先添加pom依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.3</version> </dependency>
接下来新建一个配置类
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration public class MybatisPlusConfig { public static ThreadLocal<String> myTableName = new ThreadLocal<>(); @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2) {{ put("data", (sql, tableName) -> { return myTableName.get(); }); }}; dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map); interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); return interceptor; } }
也可以将ThreadLocal来封装一下,我这里没有封装直接拿来用了,想封装的封装就行
public enum DynamicTableTreadLocal { INSTANCE; private ThreadLocal<String> tableName = new ThreadLocal<>(); public String getTableName() { return tableName.get(); } public void setTableName(String tableName) { this.tableName.set(tableName); } public void remove() { tableName.remove(); } }
然后是实体类
@Data public class Data implements Serializable { private static final long serialVersionUID = 1L; @TableId(type = IdType.AUTO) private Long id;
private String value; private Date createTime; }
然后是mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@Mapper @CacheNamespace public interface DataMapper extends BaseMapper<Data> { }
然后我们新建一个测试类就可以测试动态表名查询了
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) @Component public class tableNameAssembleTest { @Autowired private TableNameAssemble tableNameAssemble; @Autowired private DataMapper dataMapper; @Test public void select( ) { MybatisPlusConfig.myTableName.set("data_20220531");//这里偷懒了直接输入的,大家自便 QueryWrapper<Data> queryWrapper = new QueryWrapper<>(); List<Data> list = dataMapper.selectList(queryWrapper); for (Data data: list) { System.out.println(data.toString()); } } }
撒花~
学习时的痛苦是暂时的 未学到的痛苦是终生的
作者:卷心菜的奇妙历险
本文版权归作者和博客园共有,遵循 CC 4.0 BY-SA 版权协议,欢迎转载 转载请附上原文出处链接和本声明,否则保留追究法律责任的权利。