springboot项目配置多数据源
//关键:mybatis文件的目录需要区分开来
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis.myProjectOne/*.xml"));
#从数据库配置,数据库的配置 以spring.datasource.myProjectOne前缀,需要配置驱动程序名称。 spring.datasource.myProjectOne.driver-class-name = com.mysql.jdbc.Driver spring.datasource.myProjectOne.url = jdbc:mysql://IP:3306/databaseName?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true&useSSL=false spring.datasource.myProjectOne.username = username spring.datasource.myProjectOne.password = password
从数据源
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.support.TransactionTemplate; import javax.sql.DataSource; /** * 数据源配置 */ @Configuration @MapperScan(basePackages = "com.my.projectOne.mapper", sqlSessionFactoryRef = "myProjectOneDBSessionFactory") public class AutoInsureDBConfig { @Bean(name = "myProjectOneDataSource") @ConfigurationProperties(prefix = "spring.datasource.myProjectOne") @Primary public DataSource fundsDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "myProjectOneDBSessionFactory") @Primary public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("myProjectOneDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); //关键:mybatis文件的目录需要区分开来 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis.myProjectOne/*.xml")); //子模块中的类路径 mybatis.myProjectOne sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } @Bean(name = "myProjectOneTransactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("myProjectOneDataSource") DataSource dataSource) { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager; } @Bean(name = "myProjectOneTransactionTemplate") @Primary public TransactionTemplate transactionTemplate(@Qualifier("myProjectOneTransactionManager") DataSourceTransactionManager transactionManager) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); return transactionTemplate; } }
主数据库配置,数据库的配置 以spring.datasource前缀,需要配置驱动程序名称。 spring.datasource.driver-class-name = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://IP:3306/databaseName?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true&useSSL=false spring.datasource.username = username spring.datasource.password = password
主数据源
import com.autoyol.mybatis.encrypt.intercept.ResultDecryptSetInterceptor; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * 数据源配置 */ @Configuration @MapperScan(basePackages = "com.my.projectTwo.mapper", sqlSessionFactoryRef = "myProjectTwodbSessionFactory") public class InsurancePurchaseDBConfig { @Bean(name = "myProjectTwodbDataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource myProjectTwoDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "myProjectTwodbSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("myProjectTwodbDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml")); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setPlugins(new Interceptor[]{new ResultDecryptSetInterceptor()}); return sqlSessionFactoryBean.getObject(); } }
标签:springboot,spring,配置,springframework,datasource,数据源,org,import,sqlSessionFactory From: https://www.cnblogs.com/oktokeep/p/17966092