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 does springboot connect two databases?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "springboot how to connect two databases", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "springboot how to connect two databases" article.

First, start the class

1. Startup class requires @ MapperScan annotation without mybatis

@ SpringBootApplicationpublic class AppPush {public static void main (String [] args) {SpringApplication.run (AppPush.class,args);}} II. Application.yml file

Configure two or more database connections. I use postgresql here, and the same is true with mysql, etc.

Spring: datasource:# driver-class-name: org.postgresql.Driver# url: jdbc:postgresql://127.0.0.1/aaa# username: root# password: root one: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://127.0.0.1/aaa username: root password: root two: driver-class-name: org.postgresql. Driver url: jdbc:postgresql://127.0.0.1/bbb username: root password: root Create a configuration class

1. Note: @ MapperScan's basePackages is the path to your package.

SqlSessionFactoryRef can be named freely, but the two classes cannot be repeated!

One configuration class

Package com.wys.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;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.DriverManagerDataSource;import javax.sql.DataSource;/** * @ program: * @ description: database configuration 1 * @ author: wys * @ create: 2019-12-03 16:20 * * / @ Configuration@MapperScan (basePackages = "com.wys.mapper.**", sqlSessionFactoryRef = "oneSqlSessionFactory") public class OneDataSourceConfig {@ Value ("${spring.datasource.one.driver-class-name}") String driverClass @ Value ("${spring.datasource.one.url}") String url; @ Value ("${spring.datasource.one.username}") String userName; @ Value ("${spring.datasource.one.password}") String passWord; @ Primary @ Bean (name = "oneDataSource") @ ConfigurationProperties ("spring.datasource.one") public DataSource masterDataSource () {DriverManagerDataSource dataSource = new DriverManagerDataSource (); dataSource.setDriverClassName (driverClass) DataSource.setUrl (url); dataSource.setUsername (userName); dataSource.setPassword (passWord); return dataSource;} @ Bean (name = "oneSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("oneDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource) SessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mybatis/mapper-postgre/*.xml")); return sessionFactoryBean.getObject ();} @ Bean (name = "oneSqlSessionTemplate") public SqlSessionTemplate sqlSessionFactoryTemplate (@ Qualifier ("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate (sqlSessionFactory);}}

Two configuration class

Package com.wys.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;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.DriverManagerDataSource;import javax.sql.DataSource;/** * @ program: * @ description: database configuration 2 * @ author: wys * @ create: 2019-12-03 17:03 * * / @ Configuration@MapperScan (basePackages = "com.wys.mappers", sqlSessionFactoryRef = "twoSqlSessionFactory") public class TwoDataSourceConfig {@ Value ("${spring.datasource.two.driver-class-name}") String driverClass @ Value ("${spring.datasource.two.url}") String url; @ Value ("${spring.datasource.two.username}") String userName; @ Value ("${spring.datasource.two.password}") String passWord; @ Bean (name = "twoDataSource") @ ConfigurationProperties ("spring.datasource.two") public DataSource masterDataSource () {DriverManagerDataSource dataSource = new DriverManagerDataSource (); dataSource.setDriverClassName (driverClass) DataSource.setUrl (url); dataSource.setUsername (userName); dataSource.setPassword (passWord); return dataSource;} @ Bean (name = "twoSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("twoDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource) SessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mybatis/mapper-postgres/*.xml")); return sessionFactoryBean.getObject ();} @ Bean (name = "twoSqlSessionTemplate") public SqlSessionTemplate sqlSessionFactoryTemplate (@ Qualifier ("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate (sqlSessionFactory);}} IV. Structure

Some people may not know the structure of the project. I put a class structure diagram below to make it clearer and easier to understand.

Note: the main difference from the previous project is that there is one more mapper package and one more xml package for mapper, and different database mapper can be put into different packages.

The above is the content of this article on "how to connect two databases with springboot". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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

Development

Wechat

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

12
Report