Get the App
SLTechnology News&Howtos  ›  Development  › 

How does springboot connect two databases?

Shulou Source: shulou.com Published: 2022-06-01 22:57:58 09月30日 Update

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.

Tags: Data database configuration content two that is structure different articles knowledge articles projects clear not used value multiple most files more steps Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux NVidia Shulou Technology vpn