Get the App
SLTechnology News&Howtos  ›  Database  › 

Configuration of spring+mybatis multiple data sources

Shulou Source: shulou.com Published: 2022-06-01 06:06:07 10月16日 Update

Solution:

Multiple sets of sqlSessionFactory for different data sources

Data source

Xml code

Peccancy

Jdbc:oracle:thin:@172.16.60.46:1521:orcl

Oracle.jdbc.driver.OracleDriver

...

Yhtepap

Jdbc:oracle:thin:@172.16.60.46:1521:orcl

Oracle.jdbc.driver.OracleDriver

...

1. Method one

Xml code

Org.logicalcobwebs.proxool.ProxoolDriver

Proxool.peccancy

Org.logicalcobwebs.proxool.ProxoolDriver

Proxool.pap

Xml code

Classpath:net/yhte/common/query/mapper/PageData_Mapper.xml

Classpath*:net/yhte/web/peccancy/**/mapper/*Mapper.xml

Classpath*:net/yhte/web/pap/**/mapper/*Mapper.xml

Classpath:net/yhte/common/query/mapper/PageData_Mapper.xml

This solution needs to subpackage * Mapper.xml according to different data sources, so it is easy to configure. If the corresponding data sources of * Mapper.xml under the same package are different, it is not convenient to use wildcards for unified scanning configuration, and the basePackage of sqlSessionFactory packets also needs to be subpackaged.

two。 Use custom annotations to replace spring's repository settings in annotationClass. This injects the corresponding annotation @ PapRepository,@PeccancyRepository into each mapper file

Change to

The configuration file is as follows:

Xml code

Classpath:net/yhte/common/query/mapper/PageData_Mapper.xml

Classpath*:net/yhte/web/**/mapper/*Mapper.xml

Classpath:net/yhte/common/query/mapper/PageData_Mapper.xml

Classpath*:net/yhte/web/**/mapper/*Mapper.xml

Add the annotation @ PapRepository to the mapper file that uses pap as configured above

Java code

Public @ interface PeccancyRepository {

}

Public @ interface PapRepository {

}

Java code

/ / use dataSource_pap data sources

@ PapRepository

Public interface UserMapper {

List find (UserQueryBean userQueryBean)

}

/ / use dataSource_peccancy data sources

@ PeccancyRepository

Public interface ParameterMapper {

List find (ParameterQueryBean queryBean)

}

Get [download address]

The most mainstream Java background framework springmvc spring mybatis SSM project source code

Tags: Data data source code configuration different file annotation mainstream background address multiple sets such as above scheme method framework source code solution wildcard project processing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno MariaDB Shulou Technology Linux