Springboot配置连接两个数据库

更新时间:2023-07-07 22:03:14 阅读: 评论:0

Springboot配置连接两个数据库
背景:
项⽬中需要从两个不同的数据库查询数据,之前实现⽅法是:springboot配置连接⼀个数据源,另⼀个使⽤jdbc代码连接。
为了改进,现在使⽤SpringBoot配置连接两个数据源
实现效果:
⼀个SpringBoot项⽬,同时连接两个数据库:⽐如⼀个是pgsql数据库,⼀个是oracle数据库
(啥数据库都⼀样,连接两个同为oracle的数据库,或两个不同的数据库,只需要更改对应的driver-class-name和jdbc-url等即可)注意:连接什么数据库,要引⼊对应数据库的包
实现步骤:
1、修改l,添加⼀个数据库连接配置
(我这⾥是yml格式,后缀为properties格式是⼀样的)
rver:
port: 7101
spring:
jpa:
show-sql: true
datasource:
test1:
driver-class-name: org.postgresql.Driver
jdbc-url: jdbc:postgresql://127.0.0.1:5432/test  #测试数据库
urname: root
password: root
test2:
driver-class-name: oracle.jdbc.driver.OracleDriver
jdbc-url: jdbc:oracle:thin:@127.0.0.1:8888:orcl  #测试数据库
urname: root
password: root
注意红⾊字体:
(1)使⽤test1、test2区分两个数据库连接
(2)url改为:jdbc-url
2、使⽤代码进⾏数据源注⼊,和扫描dao层路径(以前是在yml⽂件⾥配置mybatis扫描dao的路径)
 新建config包,包含数据库1和数据库2的配置⽂件
riddle(1)第⼀个数据库作为主数据库,项⽬启动默认连接此数据库
  DataSource1Config.java
st.config;
import org.apache.ibatis.ssion.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.SqlSessionTemplate;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.t.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(baPackages = "st1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "st1")
@Primary
public DataSource testDataSource() {
ate().build();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.tDataSource(dataSource);
bean.tMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:test1/*.xml"));
Object();
}
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
haduri@Bean(name = "test1SqlSessionTemplate")
@Primaryroc哪个国家的缩写
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory);
}
}
  主数据库都有 @Primary注解,从数据库都没有
(2)第⼆个数据库作为从数据库
  DataSource2Config.java
st.config;
import org.apache.ibatis.ssion.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.SqlSessionTemplate;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.t.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import t.annotation.Bean;
import t.annotation.Configuration;
成本管理的概念
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(baPackages = "st2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "st2")ssa
public DataSource testDataSource() {
ate().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.tDataSource(dataSource);
bean.tMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:test2/*.xml"));
Object();
}
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
闸管
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory);
}
}
3、在dao⽂件夹下,新建test1和test2两个包,分别放两个不同数据库的dao层⽂件
  (1)TestDao1.java
@Component
public interface TestDao1 {
List<DailyActivityDataMiddle> lectDailyActivity();
}
  (2)TestDao2.java
@Component
public interface TestDao2 {
List<MovieShowTest> lectDailyActivity();
}
4、在resource下新建test1和test2两个⽂件夹,分别放⼊对应dao层的xml⽂件
double shot(我原来项⽬的dao的xml⽂件在resource⽬录下,你们在⾃⼰的项⽬对应⽬录下即可)
注意dao的java⽂件和dao的xml⽂件名字要⼀致
  (1)l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
英语旅游日记
PUBLIC "-//mybatis//DTD Mapper 3.0//EN"
"mybatis/dtd/mybatis-3-mapper.dtd">
<mapper namespace="st1.TestDao1">
<lect id="lectDailyActivity" resultType="st.pojo.DailyActivityDataMiddle">
SELECT * FROM daily_activity_data_middle
</lect>
</mapper>
  (2)l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis//DTD Mapper 3.0//EN"
"mybatis/dtd/mybatis-3-mapper.dtd">
<mapper namespace="st2.TestDao2">
<lect id="lectDailyActivity" resultType="st.pojo.MovieShowTest">
SELECT * FROM movieshowtest
</lect>
</mapper>
5、测试
在controller⽂件⾥,注⼊两个数据库的dao,分别查询数据
@RestController
public class TestController extends BaController{
@Autowired
private PropertiesUtils propertiesUtils;
@Autowired
private TestDao1 testDao1;
@Autowired
private TestDao2 testDao2;
石家庄43中地址
@RequestMapping(value = {"/test/test1"},method = RequestMethod.POST)
public Result<JSONObject> DataStatistics (@RequestBody JSONObject body) throws Exception {
Result<JSONObject> result = new Result<>(ICommon.SUCCESS, (ICommon.SUCCESS));        JSONObject object = new JSONObject();
object.put("data",testDao1.lectDailyActivity());
result.tResult(object);
return result;
}
@RequestMapping(value = {"/test/test2"},method = RequestMethod.POST)
public Result<JSONObject> DataStatisticsaa (@RequestBody JSONObject body) throws Exception {
Result<JSONObject> result = new Result<>(ICommon.SUCCESS, (ICommon.SUCCESS));        JSONObject object = new JSONObject();
object.put("data",testDao2.lectDailyActivity());
e2eresult.tResult(object);
return result;
}
}

本文发布于:2023-07-07 22:03:14,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/170378.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   连接   配置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图