关于两个不同数据库的两张表建立数据库链接,关联查询数据

一、数据库链接

数据库链接(database link)是用于跨不同数据库之间进行连接和数据传输的工具或方法。它允许在一个数据库中访问另一个数据库中的对象和数据。

二、具体操作 以Oracle数据库为例

--1.建立链接tjpt

CREATE DATABASE LINK tjpt 
CONNECT TO pt_user
IDENTIFIED BY pt_password 
USING '10.70.231.130/orcl';

上述示例中的 pt_userpt_password 和 10.70.231.130/orcl需要替换为实际的用户、密码和数据库信息。

创建链接后,您就可以在查询中使用链接名(如 @tjpt)来引用 pt 库的表,并通过设置连接条件来关联两个表。

请注意,在使用数据库链接进行关联时,确保两个数据库之间的网络连接是可用的,并且您有适当的权限来访问链接的数据库。

--2.删除链接

drop  database link tjpt


--3.查询链接是否建立成功

select a.* from all_db_links a where db_link='tjpt'


--4.查询数据
 

select t.test_no,
       t.item_class,
       t.item_name,
       t.send_time,
       t1.opinion_doctor,
       t1.opinion_doctor_code,
       t1.opinion_doctor_name,
       t1.opinion_doctor_time
  from EXAM.LIS_RESULT_ALARM t, exam.result_alarm@tjpt t1
 where t.patient_id = t1.patient_id
   and t.test_no = t1.test_no;

--5测试查询结果

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Mybatis-Plus提供的多数据源配置,然后在查询时指定使用哪个数据源。以下是示例代码: 1. 配置多数据源 在application.yml文件中配置两个数据源: ```yaml spring: datasource: master: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root slave: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root ``` 在配置类中添加多数据源配置: ```java @Configuration @MapperScan(basePackages = "com.example.mapper") public class DataSourceConfig { @Bean("master") @ConfigurationProperties("spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean("slave") @ConfigurationProperties("spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("master") DataSource masterDataSource, @Qualifier("slave") DataSource slaveDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(routingDataSource(masterDataSource, slaveDataSource)); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/**/*.xml")); return sessionFactory.getObject(); } @Bean public DataSource routingDataSource(@Qualifier("master") DataSource masterDataSource, @Qualifier("slave") DataSource slaveDataSource) { Map<Object, Object> dataSourceMap = new HashMap<>(); dataSourceMap.put("master", masterDataSource); dataSourceMap.put("slave", slaveDataSource); RoutingDataSource routingDataSource = new RoutingDataSource(); routingDataSource.setDefaultTargetDataSource(masterDataSource); routingDataSource.setTargetDataSources(dataSourceMap); return routingDataSource; } } ``` 2. 实现两个关联查询 在mapper中编写SQL语句,可以使用Mybatis-Plus提供的Wrapper类来实现多条件查询。 ```java @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user u JOIN order o ON u.id = o.user_id WHERE u.id = #{userId}") @Results({ @Result(column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "id", property = "orders", many = @Many(select = "com.example.mapper.OrderMapper.selectByUserId")), }) User selectUserWithOrders(@Param("userId") Long userId); } @Mapper public interface OrderMapper extends BaseMapper<Order> { @Select("SELECT * FROM order WHERE user_id = #{userId}") List<Order> selectByUserId(Long userId); } ``` 在service中调用mapper方法即可实现两个关联查询。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserWithOrders(Long userId) { return userMapper.selectUserWithOrders(userId); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值