Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement druid Multi-data Source configuration in spring boot

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to achieve druid multi-source configuration in spring boot", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve druid multi-source configuration in spring boot" this article.

Application.ymlmybatis: configLocation: classpath:mybatis_config.xmlspring: # data: # mongodb: # host: 10.0.5.126 # port: 27017 # database: xx # authentication-database: xx # password: # username: datasource: druid: min-idle: 3 initial-size: 5 max-active: 10 # connection wait timeout max-wait: 10000 # configuration detects the interval between idle connections that can be closed time-between-eviction-runs-millis: 60000 # configure the minimum lifetime of connections in the pool min-evictable-idle-time-millis: 300000 validation-query: select'1' test-while-idle: true test-on-borrow: true test-on-return: false # turn on PSCache And specify the size of the PSCache on each connection pool-prepared-statements: true max-open-prepared-statements: 20 max-pool-prepared-statement-per-connection-size: 20 filter: stat: merge-sql: true # slow log query log-slow-sql: true # slow SQL recording sql for more than 5 seconds is marked red on the druid console Slow-sql-millis: 5000 wall: enabled: false commons-log: enabled: false log4j: connection-log-enabled: false slf4j: statement-log-enabled: false log4j2: statement-log-enabled: false # configure filters for monitoring statistics interception After the monitoring interface is removed, the sql cannot be counted. 'wall' is used for firewalls. Delete log4j filters: stat,wall # configure Druid Spring monitoring aspect # aop-patterns: com.xitor.service.*,com.xonitor.schedule.*,com.xonitor.controller.* # configure DruidStatFilter web-stat-filter: enabled: true url-pattern: "/ *" exclusions: "* .js,*.gif,*.jpg if the logback is used in the project * .bmp,*.png,*.css,*.ico,/druid/*,/slife_server/druid/* "# configure DruidStatViewServlet stat-view-servlet: enabled: true url-pattern:" / druid/* "allow:" # allow: 127.0.1192.168.163.1 IP whitelist (not configured or empty Then all access is allowed) # IP blacklist (when there is a common Deny takes precedence over allow) # deny: 192.168.1.73 # disable the "Reset All" function on the HTML page. True does not disable reset-enable: true login-username: admin login-password: xx use-global-data-source-stat: false clear-filters-enable: true time-between-log-stats-millis: 3600000 # configure to output statistical logs every hour After statistics, the log oms: name: oms driver-class-name: org.postgresql.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:postgresql://127.0.0.1:5432/eagle username: xx password: xx star-talk: name: starTalk driver-class-name: org.postgresql.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:postgresql: / / 10.0.5.105:5432/ejabberd username: xx password: xx logging: name: logging driver-class-name: org.postgresql.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:postgresql://127.0.0.1:5432/eagle username: xx password: xx wpms: name: wpms driver-class-name: oracle.jdbc.driver.OracleDriver type: com.alibaba .druid.pool.DruidDataSource url: jdbc:oracle:thin:@localhost:1521:ORCL username: xx password: xxDataSourceConfigurer@Configurationpublic class OMSDataSourceConfigurer {@ Value ("${mybatis.configLocation}") private Resource configLocation Primary @ Bean @ ConfigurationProperties (prefix = "spring.datasource.oms") public DataSourceProperties omsDataSourceProperties () {return new DataSourceProperties ();} @ Bean (initMethod = "init") @ Primary @ ConfigurationProperties ("spring.datasource.druid") public DruidDataSource omsDataSource (@ Qualifier ("omsDataSourceProperties") DataSourceProperties dataSourceProperties) {return (DruidDataSource) dataSourceProperties.initializeDataSourceBuilder () .build () } @ Bean @ Primary public SqlSessionFactory omsSqlSessionFactory (@ Qualifier ("omsDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource); Assert.notNull (this.configLocation, "); Assert.isTrue (this.configLocation.exists (),"); bean.setConfigLocation (configLocation); bean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources ("classpath:mybatis_mapper/oms/*.xml")) Return bean.getObject ();} @ Bean @ Primary public DataSourceTransactionManager omsTransactionManager (@ Qualifier ("omsDataSource") DataSource dataSource) {return new DataSourceTransactionManager (dataSource);} @ Bean @ Primary public SqlSessionTemplate omsSqlSessionTemplate (@ Qualifier ("omsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate (sqlSessionFactory);}} WPMSDataSourceConfigurer

Same as above configuration, do not add @ Primary

MybatisConfigurer@Configuration@ComponentScan ("com.gridvo.eagle.repository") @ MapperScan (basePackages = "com.xx.mybatis.oms.dao", sqlSessionTemplateRef = "omsSqlSessionTemplate") @ MapperScan (basePackages = "com.xx.mybatis.startalk.dao", sqlSessionTemplateRef = "starTalkSqlSessionTemplate") @ MapperScan (basePackages = "com.xx.mybatis.logging.dao", sqlSessionTemplateRef = "loggingSqlSessionTemplate") @ MapperScan (basePackages = "com.xxmybatis.wpms.dao" SqlSessionTemplateRef = "wpmsSqlSessionTemplate") @ EnableTransactionManagement (proxyTargetClass = true) public class MybatisConfigurer {} above are all the contents of the article "how to implement druid Multi-data Source configuration in spring boot" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report