Mybatis使⽤⾃定义注解动态配置多数据源需求说明
⽬前在项⽬中有多个数据源,需要多个配置。有好⼏种办法来配置,⽐如:
jdbcTemplate
@DS
⾃定义注解
下⾯说明⽤⾃定义注解来添加切换多个数据源。
pom的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-autoconfigure</artifactId> <groupId>org.springframework.boot</groupId> </exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
配置⽂件数据库配置信息
多个数据源就配置多个,⽬前以⼀个为例。
pe=com.alibaba.druid.pool.DruidDataSource
spring.datasource.size=1
#master
spring.datasource.druid.master.url=jdbc:mysql://localhost:3306/git-demo?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.druid.master.username=root
spring.datasource.druid.master.password=jiaojiao
spring.datasource.druid.master.sql.jdbc.Driver
spring.datasource.druid.master.initial-size=5
spring.datasource.druid.master.max-active=30
spring.datasource.druid.master.min-idle=5
spring.datasource.druid.master.max-wait=6000
spring.datasource.druid.master.time-between-eviction-runs-millis=600000
spring.datasource.druid.master.validation-query=SELECT 1springboot aop
spring.datasource.druid.master.validation-query-timeout=300000
spring.datasource.st-on-borrow=true
spring.datasource.st-on-return=false
spring.datasource.st-while-idle=true
spring.datasource.ve-abandoned=true
spring.datasource.ve-abandoned-timeout=30
spring.datasource.druid.master.stat-view-servlet.login-username=ytostation
spring.datasource.druid.master.stat-view-servlet.login-password=~!@#$%`12345
mybatis.mapper-locations=classpath*:mapper/*.xml
设置⾃定义注解
多个数据源就写多个注解。
@Documented
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public@interface MyStyleMasterAnnotation {
}
配置动态数据源:继承AbstractRoutingDataSource
Spring boot提供了AbstractRoutingDataSource 根据⽤户定义的规则选择当前的数据源,这样我们可以在执⾏查询之前,设置使⽤的数据源。实现可动态路由的数据源,在每次数据库查询操作前执⾏。它的抽象⽅法 determineCurrentLookupKey() 决定使⽤哪个数据源。
查看源码可知:AbstractRoutingDataSource的多数据源动态切换的核⼼逻辑是:在程序运⾏时,把数据源数据源通过AbstractRoutingDataSource 动态织⼊到程序中,灵活的进⾏数据源切换。
基于AbstractRoutingDataSource的多数据源动态切换,可以实现读写分离,这么做缺点也很明显,⽆法动态的增加数据源。
public class MyStyleRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey(){
return getDbType();
}
public enum DbType{
MASTER
}
private static final ThreadLocal<DbType> contextHolder =new ThreadLocal<>();
public static void setDbType(DbType dbType){
if(dbType ==null){
throw new NullPointerException();
}
contextHolder.set(dbType);
}
public static DbType getDbType(){
()==null? DbType.MASTER : ();
}
public static void clearDbType(){
}
}
绑定⾃定义注解
每个注解⼀个切⾯。
@Component
@Slf4j
public class MyStyleMasterAspect {
@Around("@annotation(myStyleMasterAnnotation)")
public Object invoke(ProceedingJoinPoint joinPoint, MyStyleMasterAnnotation myStyleMasterAnnotation)throws Throwable { try{
MyStyleRoutingDataSource.setDbType(MyStyleRoutingDataSource.DbType.MASTER);
return joinPoint.proceed();
}finally{
MyStyleRoutingDataSource.clearDbType();
}
}
}
数据源配置类
@Configuration
@Slf4j
@EnableTransactionManagement
public class MyStyleDataSourceConfiguration extends MybatisAutoConfiguration {
@Value("${pe}")
private Class<?extends DataSource> dbSourceType;
public MyStyleDataSourceConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]>
interceptorsProvider, ObjectProvider<TypeHandler[ ]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvi der> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider){
super(properties, interceptorsProvider, typeHandlersProvider, languageDriversProvider, resourceLoader, databaseIdProvider, configurationCustomiz ersProvider);
}
//多个数据源就再多写多个实例
@Bean
@Primary
@ConfigurationProperties(prefix ="spring.datasource.druid.master")
public DataSource masterDataSource(){
ate().type(dbSourceType).build();
}
@Bean
@Override
public SqlSessionFactory sqlSessionFactory(DataSource dataSource)throws Exception {
return super.sqlSessionFactory(dataSource());
}
@Bean
public DataSource dataSource(){
AbstractRoutingDataSource routingDataSource =new MyStyleRoutingDataSource();
Map<Object,Object> targetDataSources =new HashMap<>(5);
//多少个数据源就添加到map中
targetDataSources.put(MyStyleRoutingDataSource.DbType.MASTER,masterDataSource());
routingDataSource.setTargetDataSources(targetDataSources);
routingDataSource.afterPropertiesSet();
return routingDataSource;
}
}
启动类中添加@MapperScan
@MapperScan(basePackages ={"cn.mystyle.*.dao"})
涉及到的mapper的xml⽂件
<select id="queryStudentInfo"resultType="cn.ity.Student">
select id,name,id_card_no,address from student
</select>
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student {
private int id;
private String name;
private String idCardNo;
private String address;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论