Created
April 18, 2019 09:39
-
-
Save yoqu/a26102f6c281571faf48137683ac0f91 to your computer and use it in GitHub Desktop.
sense dbBUilder配置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello.support.fast.datasource; | |
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; | |
import org.apache.ibatis.annotations.Mapper; | |
import org.mybatis.spring.SqlSessionFactoryBean; | |
import org.mybatis.spring.SqlSessionTemplate; | |
import org.mybatis.spring.mapper.MapperScannerConfigurer; | |
import org.senseframework.support.orm.mybatis.datasource.MultipleDataSource; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import javax.sql.DataSource; | |
import java.util.HashMap; | |
import java.util.Map; | |
@Configuration | |
public class DBBuilderConfig { | |
@Bean | |
@ConfigurationProperties("spring.datasource.druid.first") | |
public DataSource firstDataSource(){ | |
return DruidDataSourceBuilder.create().build(); | |
} | |
@Bean | |
@ConfigurationProperties("spring.datasource.druid.second") | |
public DataSource secondDataSource(){ | |
return DruidDataSourceBuilder.create().build(); | |
} | |
@Bean | |
public MultipleDataSource multipleDataSource(DataSource firstDataSource, DataSource secondDataSource) { | |
MultipleDataSource dataSource = new MultipleDataSource(); | |
dataSource.setDefaultTargetDataSource(firstDataSource); | |
dataSource.setDefaultReadKey(ReadWriteKeys.READ); | |
dataSource.setDefaultWriteKey(ReadWriteKeys.WRITE); | |
Map<Object, Object> targetDataSources = new HashMap<>(); | |
targetDataSources.put(ReadWriteKeys.READ, firstDataSource); | |
targetDataSources.put(ReadWriteKeys.WRITE, secondDataSource); | |
dataSource.setTargetDataSources(targetDataSources); | |
return dataSource; | |
} | |
@Bean | |
public SqlSessionFactoryBean sqlSessionFactory(MultipleDataSource multipleDataSource) { | |
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); | |
bean.setDataSource(multipleDataSource); | |
return bean; | |
} | |
@Bean | |
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactoryBean bean) throws Exception { | |
return new SqlSessionTemplate(bean.getObject()); | |
} | |
@Bean | |
public MapperScannerConfigurer mapperScannerConfigurer() { | |
MapperScannerConfigurer configurer = new MapperScannerConfigurer(); | |
configurer.setBasePackage("com.hello.support.fast.module"); | |
configurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); | |
configurer.setAnnotationClass(Mapper.class); | |
return configurer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment