File tree Expand file tree Collapse file tree 8 files changed +187
-1
lines changed
lab-12-mybatis/lab-12-mybatis-plus-tenant
main/java/cn/iocoder/springboot/lab12/mybatis
test/java/cn/iocoder/springboot/lab12/mybatis/service Expand file tree Collapse file tree 8 files changed +187
-1
lines changed Original file line number Diff line number Diff line change 3838 <scope >test</scope >
3939 </dependency >
4040
41+ <dependency >
42+ <groupId >com.alibaba</groupId >
43+ <artifactId >transmittable-thread-local</artifactId >
44+ <version >2.12.2</version >
45+ </dependency >
46+
4147 </dependencies >
4248
4349</project >
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .config ;
2+
3+ import com .alibaba .ttl .TtlRunnable ;
4+ import org .springframework .beans .BeansException ;
5+ import org .springframework .beans .factory .config .BeanPostProcessor ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .scheduling .annotation .EnableAsync ;
9+ import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
10+
11+ @ Configuration
12+ @ EnableAsync
13+ public class AsyncConfig {
14+
15+ @ Bean
16+ public BeanPostProcessor executorBeanPostProcessor () {
17+ return new BeanPostProcessor () {
18+
19+ @ Override
20+ public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
21+ if (!(bean instanceof ThreadPoolTaskExecutor )) {
22+ return bean ;
23+ }
24+ ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor ) bean ;
25+ executor .setTaskDecorator (TtlRunnable ::get );
26+ return executor ;
27+ }
28+
29+ };
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 11package cn .iocoder .springboot .lab12 .mybatis .config ;
22
3+ import cn .iocoder .springboot .lab12 .mybatis .context .TenantHolder ;
34import com .baomidou .mybatisplus .extension .plugins .MybatisPlusInterceptor ;
45import com .baomidou .mybatisplus .extension .plugins .handler .TenantLineHandler ;
56import com .baomidou .mybatisplus .extension .plugins .inner .TenantLineInnerInterceptor ;
@@ -21,7 +22,8 @@ public MybatisPlusInterceptor mybatisPlusInterceptor() {
2122
2223 @ Override
2324 public Expression getTenantId () {
24- return new LongValue (10 );
25+ Integer tenantId = TenantHolder .getTenantId ();
26+ return new LongValue (tenantId );
2527 }
2628
2729 // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .context ;
2+
3+ import com .alibaba .ttl .TransmittableThreadLocal ;
4+
5+ public class TenantHolder {
6+
7+ private static final ThreadLocal <Integer > TENANT_ID = new TransmittableThreadLocal <>();
8+ // private static final ThreadLocal<Integer> TENANT_ID = new ThreadLocal<>();
9+ // private static final ThreadLocal<Integer> TENANT_ID = new InheritableThreadLocal<>();
10+
11+ public static void setTenantId (Integer tenantId ) {
12+ TENANT_ID .set (tenantId );
13+ }
14+
15+ public static Integer getTenantId () {
16+ return TENANT_ID .get ();
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .core ;
2+
3+ import com .alibaba .ttl .TtlCallable ;
4+ import com .alibaba .ttl .TtlRunnable ;
5+ import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
6+ import org .springframework .util .concurrent .ListenableFuture ;
7+
8+ import java .util .Objects ;
9+ import java .util .concurrent .Callable ;
10+ import java .util .concurrent .Future ;
11+
12+ @ Deprecated
13+ public class TtlThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
14+
15+ @ Override
16+ public void execute (Runnable task ) {
17+ super .execute (Objects .requireNonNull (TtlRunnable .get (task )));
18+ }
19+
20+ @ Override
21+ public void execute (Runnable task , long startTimeout ) {
22+ super .execute (Objects .requireNonNull (TtlRunnable .get (task )), startTimeout );
23+ }
24+
25+ @ Override
26+ public Future <?> submit (Runnable task ) {
27+ return super .submit (Objects .requireNonNull (TtlRunnable .get (task )));
28+ }
29+
30+ @ Override
31+ public <T > Future <T > submit (Callable <T > task ) {
32+ return super .submit (Objects .requireNonNull (TtlCallable .get (task )));
33+ }
34+
35+ @ Override
36+ public ListenableFuture <?> submitListenable (Runnable task ) {
37+ return super .submitListenable (Objects .requireNonNull (TtlRunnable .get (task )));
38+ }
39+
40+ @ Override
41+ public <T > ListenableFuture <T > submitListenable (Callable <T > task ) {
42+ return super .submitListenable (Objects .requireNonNull (TtlCallable .get (task )));
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .service ;
2+
3+ import cn .iocoder .springboot .lab12 .mybatis .dataobject .UserDO ;
4+ import cn .iocoder .springboot .lab12 .mybatis .mapper .UserMapper ;
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
7+ import org .springframework .scheduling .annotation .Async ;
8+ import org .springframework .scheduling .annotation .AsyncResult ;
9+ import org .springframework .stereotype .Service ;
10+
11+ import javax .annotation .Resource ;
12+ import java .util .concurrent .Future ;
13+
14+ @ Service
15+ public class UserService {
16+
17+ private final Logger log = LoggerFactory .getLogger (UserService .class );
18+
19+ @ Resource
20+ private UserMapper userMapper ;
21+
22+ @ Async
23+ public Future <UserDO > getUserAsync (Integer id ) {
24+ UserDO userDO = userMapper .selectById (id );
25+ log .info ("[getUserAsync][id({}) user({})]" , id , userDO );
26+ return AsyncResult .forValue (userDO );
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .util ;
2+
3+ import com .alibaba .ttl .spi .TtlEnhanced ;
4+ import com .alibaba .ttl .threadpool .agent .TtlAgent ;
5+ import org .springframework .lang .Nullable ;
6+
7+ import java .util .concurrent .Executor ;
8+
9+ /**
10+ * {@link com.alibaba.ttl.threadpool.TtlExecutors} 工具类
11+ */
12+ @ Deprecated
13+ public class TtlExecutorsUtil {
14+
15+ public static Executor getTtlThreadPoolTaskExecutor (@ Nullable Executor executor ) {
16+ if (TtlAgent .isTtlAgentLoaded () || null == executor || executor instanceof TtlEnhanced ) {
17+ return executor ;
18+ }
19+ // return new ExecutorTtlWrapper(executor, true);
20+ return null ;
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package cn .iocoder .springboot .lab12 .mybatis .service ;
2+
3+ import cn .iocoder .springboot .lab12 .mybatis .Application ;
4+ import cn .iocoder .springboot .lab12 .mybatis .context .TenantHolder ;
5+ import cn .iocoder .springboot .lab12 .mybatis .dataobject .UserDO ;
6+ import org .junit .Test ;
7+ import org .junit .runner .RunWith ;
8+ import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .springframework .test .context .junit4 .SpringRunner ;
10+
11+ import javax .annotation .Resource ;
12+ import java .util .concurrent .ExecutionException ;
13+ import java .util .concurrent .Future ;
14+
15+ import static org .junit .Assert .*;
16+
17+ @ RunWith (SpringRunner .class )
18+ @ SpringBootTest (classes = Application .class )
19+ public class UserServiceTest {
20+
21+ @ Resource
22+ private UserService userService ;
23+
24+ @ Test
25+ public void testGetUserAsync () throws ExecutionException , InterruptedException {
26+ TenantHolder .setTenantId (10 ); // TODO 芋艿:写死
27+ Future <UserDO > future = userService .getUserAsync (9 );
28+ future .get ();
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments