在 SpringBoot 中实现多数据源访问的最佳实践( 三 )

【在 SpringBoot 中实现多数据源访问的最佳实践】具体如下
import com.ckjava.entity.DbEnum;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 通过 ThreadLocal 来存储当前线程的 数据库 key 变量 */public class MultiDataSourceHolder {private static final Logger logger = LoggerFactory.getLogger(MultiDataSourceHolder.class);/*** 通过 ThreadLocal 来存储当前线程的 dataSource key*/private static final ThreadLocal<DbEnum> contextHolder = new ThreadLocal<>();public static void setDataSource(DbEnum dataSource) {logger.info("Thread {} set datasource {}", Thread.currentThread().getName(), dataSource);contextHolder.set(dataSource);}public static DbEnum getDataSource() {return contextHolder.get();}public static void clearDataSource() {logger.info("Thread {} unset datasource {}", Thread.currentThread().getName(), contextHolder.get());contextHolder.remove();}}4.8 在 TUserService 和 TOrderService 上使用

  • TUserService
import com.ckjava.aop.annotation.MultiDataSource;import com.ckjava.dao.TUserDao;import com.ckjava.entity.DbEnum;import com.ckjava.entity.TUserEntity;import com.ckjava.entity.base.PageParamer;import com.ckjava.service.base.BaseMybatisCrudService;import com.ckjava.xutils.http.Page;import org.springframework.stereotype.Service;import java.util.List;import java.util.Optional;@Servicepublic class TUserService extends BaseMybatisCrudService<TUserEntity, TUserDao> {@Overridepublic Class<TUserEntity> getClassType() {return TUserEntity.class;}@Override@MultiDataSource(DbEnum.user)public Optional<TUserEntity> get(long id) {return super.get(id);}@Override@MultiDataSource(DbEnum.user)public Optional<List<TUserEntity>> getAll() {return super.getAll();}@Override@MultiDataSource(DbEnum.user)public Optional<List<TUserEntity>> getByParam(TUserEntity entity) {return super.getByParam(entity);}@Override@MultiDataSource(DbEnum.user)public Optional<Page<TUserEntity>> getPage(PageParamer pageParamer) {return super.getPage(pageParamer);}@Override@MultiDataSource(DbEnum.user)public Optional<Long> save(TUserEntity entity) {return super.save(entity);}@Override@MultiDataSource(DbEnum.user)public Optional<Long> update(TUserEntity entity) {return super.update(entity);}@Override@MultiDataSource(DbEnum.user)public Optional<Long> saveOrUpdate(TUserEntity entity) {return super.saveOrUpdate(entity);}@Override@MultiDataSource(DbEnum.user)public Optional<Integer> delete(Long id) {return super.delete(id);}@Override@MultiDataSource(DbEnum.user)public Optional<Integer> deletePhysical(Long id) {return super.deletePhysical(id);}}
  • TOrderService
import com.ckjava.aop.annotation.MultiDataSource;import com.ckjava.dao.TOrderDao;import com.ckjava.entity.DbEnum;import com.ckjava.entity.TOrderEntity;import com.ckjava.entity.base.PageParamer;import com.ckjava.service.base.BaseMybatisCrudService;import com.ckjava.xutils.http.Page;import org.springframework.stereotype.Service;import java.util.List;import java.util.Optional;@Servicepublic class TOrderService extends BaseMybatisCrudService<TOrderEntity, TOrderDao> {@Overridepublic Class<TOrderEntity> getClassType() {return TOrderEntity.class;}@Override@MultiDataSource(DbEnum.order)public Optional<TOrderEntity> get(long id) {return super.get(id);}@Override@MultiDataSource(DbEnum.order)public Optional<List<TOrderEntity>> getAll() {return super.getAll();}@Override@MultiDataSource(DbEnum.order)public Optional<List<TOrderEntity>> getByParam(TOrderEntity entity) {return super.getByParam(entity);}@Override@MultiDataSource(DbEnum.order)public Optional<Page<TOrderEntity>> getPage(PageParamer pageParamer) {return super.getPage(pageParamer);}@Override@MultiDataSource(DbEnum.order)public Optional<Long> save(TOrderEntity entity) {return super.save(entity);}@Override@MultiDataSource(DbEnum.order)public Optional<Long> update(TOrderEntity entity) {return super.update(entity);}@Override@MultiDataSource(DbEnum.order)public Optional<Long> saveOrUpdate(TOrderEntity entity) {return super.saveOrUpdate(entity);}@Override@MultiDataSource(DbEnum.order)public Optional<Integer> delete(Long id) {return super.delete(id);}@Override@MultiDataSource(DbEnum.order)public Optional<Integer> deletePhysical(Long id) {return super.deletePhysical(id);}}5 测试
  • TestOrderService 测试如下
import com.ckjava.entity.TOrderEntity;import com.ckjava.service.TOrderService;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.Date;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTestpublic class TestOrderService { @Autowired private TOrderService tOrderService; @Test public void test_get() {tOrderService.get(1L).ifPresent(entity -> {System.out.println(entity.getId());}); }}


推荐阅读