zoukankan      html  css  js  c++  java
  • Spring Boot 项目中使用 Mybatis 连接多个数据库

    在项目开发中我们经常需要从多个数据库中查询数据,目前在 Java 领域应用比较广泛的就是 Mybatis,MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs 映射成数据库中的记录。

    下面我们就来看下在项目中使用 MyBatis 连接多个数据库的办法。

    首先在 application.yml 中配置连接字符串,这里需要注意的是多数据库的写法和标准写法有点不一样:

    datasource:
        post:
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          jdbc-url: jdbc:sqlserver://test.charles.com:1433;DatabaseName=postdata;useunicode=true;characterEncoding=UTF-8
          username: testuser
          password: user123456
        user:
          driver-class-name: com.mysql.jdbc.Driver
          jdbc-url: jdbc:mysql://test.charles.com/userdata?useUnicode=true&characterEncoding=UTF-8&useSSL=false
          username: testuser
          password: user123456
    

    数据库驱动:

    <!--sqlserver-->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>
    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.10</version>
    </dependency>
    

     User 模块的数据库配置:

    package com.charles.testlibrary.datasource;
    
    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.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.charles.testlibrary.mapper.user", sqlSessionTemplateRef = "userSqlSessionTemplate")
    public class UserDataSourceConfig {
    
        @Bean(name = "userDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.user")
        public DataSource userDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "userSqlSessionFactory")
        public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/user/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "userTransactionManager")
        public DataSourceTransactionManager userTransactionManager(@Qualifier("userDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "userSqlSessionTemplate")
        public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

     Post 模块的数据库配置:

    package com.charles.testlibrary.datasource;
    
    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.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.charles.testlibrary.mapper.post", sqlSessionTemplateRef = "postSqlSessionTemplate")
    public class PostDataSourceConfig {
    
        @Bean(name = "postDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.post")
        public DataSource postDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "postSqlSessionFactory")
        public SqlSessionFactory postSqlSessionFactory(@Qualifier("postDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/post/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "postTransactionManager")
        public DataSourceTransactionManager postTransactionManager(@Qualifier("postDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "postSqlSessionTemplate")
        public SqlSessionTemplate postSqlSessionTemplate(@Qualifier("postSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    在项目的 mybatis/mapper/post/ 和 mybatis/mapper/user/ 创建对应的 mapper.xml 文件和对应的 Mapper 接口。

    程序会根据配置自动扫描对应的目录和映射。

    作者:Charles Zhang


    出处:https://www.cnblogs.com/weisenz/


    本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。

  • 相关阅读:
    Capture CIS:Capture was not found错误
    Memcached FAQ
    MTK META工具的使用和注意事项(MT6252)
    关于maps.google.com和ditu.google.cn地图偏差的说明
    VSS2005的二次开发
    同学PB经历的面试题
    给定一个字符串,包含中文字符和英文字符,取给定大小字节的子串。
    一些笔试题目和整理的答案 腾讯(Tencent)
    redhat面试题目
    Ubuntu远程链接Ubuntu之ssh
  • 原文地址:https://www.cnblogs.com/weisenz/p/2920390.html
Copyright © 2011-2022 走看看