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

Configuration of spring+mybatis multiple data sources

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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

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

Database

Wechat

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

12
Report