Customize Annotation - shi-yuan/dynamic-datasource-spring-boot-starter GitHub Wiki

前言

如果觉得默认的DS注解难看,或者自己就固定几个数据源想自己命名等需求。

建议从3.2.1版本开始使用自定义注解,另外组件自带了@Master和@Slave注解。

使用方法

下面我们自定义一个产品库的注解,方便我们后面程序的使用。

  1. 需要自己定义一个注解并继承自DS。
import com.baomidou.dynamic.datasource.annotation.DS;

import java.lang.annotation.*;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS("product")
public @interface Product {
}
  1. 注解在需要切换数据源的方法上或类上。

@Service
@Product
public class ProductServiceImpl implements ProductService {

    @Product
    public List selectAll() {
      return  jdbcTemplate.queryForList("select * from products");
    }
}