首先有两个mapper对象,一个是普通的mapper,一个是分库分表的mapper,分别往数据库插入数据。 可以看到分为了三个数据库,分别是test test_01 test_02,然后test库有一张user表,test_01 test_02各有user_000-003个四张表,然后我们运行测试用例: 我们可以看到在test库的user表和test01的user_001表分别插入了一条记录,分库分表成功。 使用起来非常简单,只需要在Mapper接口上加入两个注解: 第一个注解DBRouterStrategy表示是否需要分表,第二个注解DBRouterStrategy表示分库分表的字段。 宏观流程 1.在调用dao层接口时换数据源 -> 更换库 2.在执行sql之前替换原有sql ->更换表 3.执行sql 核心类 AbstractRoutingDataSource类是用于动态切换数据源 AbstractRoutingDataSource#determineCurrentLookupKey用于获取数据源的抽象方法...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with int
Contact int, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack