好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot配置多数据源并集成Druid和mybatis的操作

可以是mysql,oracle等多种不同数据源

项目结构

注意:只有@Primary的数据源所控制的mapper文件加注解@Mapper,否则mybatis无法切换扫描;即本文中的mapper/opener文件夹下mapper加注解

1、pom

驱动之外加入druid和mybatis等pom,整合mybatis自行搜索

?

1

2

3

4

5

< dependency >

      < groupId >com.alibaba</ groupId >

      < artifactId >druid-spring-boot-starter</ artifactId >

      < version >1.1.23</ version >

  </ dependency >

2、properties配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

##################    JDBC 配置    ################

#资讯数据库一配置

spring.datasource.druid.opener.driver- class -name=oracle.jdbc.driver.OracleDriver

spring.datasource.druid.opener.url=jdbc:oracle:thin: @127 .0. 0.1 : 1521 :wljrdb

spring.datasource.druid.opener.username=opener

spring.datasource.druid.opener.password= 123456

 

#舆情数据库二配置

spring.datasource.druid.yq.driver- class -name=oracle.jdbc.driver.OracleDriver

spring.datasource.druid.yq.url=jdbc:oracle:thin: @127 .0. 0.1 : 1521 :wljrdb

spring.datasource.druid.yq.username=YQ

spring.datasource.druid.yq.password= 123456

 

#f10数据库二配置

spring.datasource.druid.f10.driver- class -name=com.mysql.jdbc.Driver

spring.datasource.druid.f10.url=jdbc:mysql: //127.0.0.1:3306/iwin-f10?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8

spring.datasource.druid.f10.username=root

spring.datasource.druid.f10.password= 123456

 

##########################  druid配置   ##########################

# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大

##################    连接池配置    ################

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.druid.initial-size= 5

spring.datasource.druid.max-active= 20

spring.datasource.druid.min-idle= 5

spring.datasource.druid.max-wait= 30000

 

spring.datasource.druid.validation-query=SELECT 1 FROM DUAL

spring.datasource.druid.validation-query-timeout= 30000

# 配置监控统计拦截的filters,去掉后监控界面sql无法统计, 'wall' 用于防火墙

spring.datasource.druid.filters=stat,wall,log4j2

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录

spring.datasource.druid.connectionProperties=druid.stat.mergeSql= true ;druid.stat.slowSqlMillis= 5000

 

# 监控中心 如果设置了StatViewServlet,即配置了监控池认证,

# 要进监控池需要输入http: //127.0.0.1:8083/druid/login.html,如果没配置,直接输入http://127.0.0.1:8083/druid/即可

spring.datasource.druid.stat-view-servlet.enabled= true

spring.datasource.druid.stat-view-servlet.url-pattern= /druid/*

spring.datasource.druid.stat-view-servlet.reset-enable= true

spring.datasource.druid.stat-view-servlet.login-username= admin

spring.datasource.druid.stat-view-servlet.login-password= admin123

spring.datasource.druid.stat-view-servlet.allow= 127.0 . 0.1

3、配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.mapper.MapperScannerConfigurer;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

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 javax.sql.DataSource;

 

/**

  * @Author: admin

  */

@Configuration

public class MultiDataSourceConfig {

 

     private final static Logger logger = LoggerFactory.getLogger(MultiDataSourceConfig. class );

 

     @Bean

     @Primary

     @ConfigurationProperties (prefix = "spring.datasource.druid.opener" )

     public DataSource openerDataSource(){

         logger.info( "opener数据源初始化。。。" );

         return DruidDataSourceBuilder.create().build();

     }

 

     @Bean

     @ConfigurationProperties (prefix = "spring.datasource.druid.yq" )

     public DataSource yqDataSource(){

         logger.info( "YQ数据源初始化。。。" );

         return DruidDataSourceBuilder.create().build();

     }

 

     @Bean

     @ConfigurationProperties (prefix = "spring.datasource.druid.f10" )

     public DataSource f10DataSource(){

         logger.info( "f10数据源初始化。。。" );

         return DruidDataSourceBuilder.create().build();

     }

 

     @Bean

     @Primary

     public SqlSessionFactory openerSqlSessionFactory( @Qualifier ( "openerDataSource" )DataSource openerDataSource) throws Exception{

         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();

         fb.setDataSource(openerDataSource);

         fb.setMapperLocations(

                 new PathMatchingResourcePatternResolver().getResources( "classpath:/mybatis/opener/*.xml" )

         );

         return fb.getObject();

     }

 

     @Bean

     public SqlSessionFactory yqSqlSessionFactory( @Qualifier ( "yqDataSource" )DataSource yqDataSource) throws Exception{

         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();

         fb.setDataSource(yqDataSource);

         fb.setMapperLocations(

                 new PathMatchingResourcePatternResolver().getResources( "classpath:/mybatis/yq/*.xml" )

         );

         return fb.getObject();

     }

 

     @Bean

     public SqlSessionFactory f10SqlSessionFactory( @Qualifier ( "f10DataSource" )DataSource f10DataSource) throws Exception{

         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();

         fb.setDataSource(f10DataSource);

         fb.setMapperLocations(

                 new PathMatchingResourcePatternResolver().getResources( "classpath:/mybatis/f10/*.xml" )

         );

         return fb.getObject();

     }

 

     @Bean (name= "openerMapperScannerConfigurer" )

     @Primary

     public MapperScannerConfigurer openerMapperScannerConfigurer(){

         MapperScannerConfigurer configurer = new MapperScannerConfigurer();

         configurer.setSqlSessionFactoryBeanName( "openerSqlSessionFactory" );

         configurer.setBasePackage( "com.cfzq.lz.mapper.opener" );

         return configurer;

     }

 

     @Bean (name= "yqMapperScannerConfigurer" )

     public MapperScannerConfigurer yqMapperScannerConfigurer(){

         MapperScannerConfigurer configurer = new MapperScannerConfigurer();

         configurer.setSqlSessionFactoryBeanName( "yqSqlSessionFactory" );

         configurer.setBasePackage( "com.cfzq.lz.mapper.yq" );

         return configurer;

     }

 

     @Bean (name= "f10MapperScannerConfigurer" )

     public MapperScannerConfigurer f10MapperScannerConfigurer(){

         MapperScannerConfigurer configurer = new MapperScannerConfigurer();

         configurer.setSqlSessionFactoryBeanName( "f10SqlSessionFactory" );

         configurer.setBasePackage( "com.cfzq.lz.mapper.f10" );

         return configurer;

     }

 

     @Bean (name= "openerTransactionManager" )

     @Primary

     public DataSourceTransactionManager openerTransactionManager( @Qualifier ( "openerDataSource" )DataSource openerDataSource) throws Exception{

         return new DataSourceTransactionManager(openerDataSource);

     }

 

     @Bean (name= "yqTransactionManager" )

     public DataSourceTransactionManager yqTransactionManager( @Qualifier ( "yqDataSource" )DataSource yqDataSource) throws Exception{

         return new DataSourceTransactionManager(yqDataSource);

     }

 

     @Bean (name= "f10TransactionManager" )

     public DataSourceTransactionManager f10TransactionManager( @Qualifier ( "f10DataSource" )DataSource f10DataSource) throws Exception{

         return new DataSourceTransactionManager(f10DataSource);

     }

 

}

代码调用

很简单,使用哪个数据源service注入哪个mapper就可以了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/JGMa_TiMo/article/details/109097411

查看更多关于springboot配置多数据源并集成Druid和mybatis的操作的详细内容...

  阅读:21次