美文网首页
SpringBoot+Mybatis多数据库

SpringBoot+Mybatis多数据库

作者: 期丨期 | 来源:发表于2020-08-25 11:05 被阅读0次

    配置application.yaml数据源

    在application.yaml中配置数据源,也可使用多环境profiles切换

    spring:
      datasource:
        jdbc-url: jdbc:mysql://192.168.1.26:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: root
    
    #    jdbc-url: jdbc:dm://192.168.1.19:5238/test
    #    username: username
    #    password: password
    

    数据源配置类

    @Configuration
    public class DataSourceConfig {
        @Value("${mybatis.mapper-locations}")
        private String mapperLocations;
    
        @Primary
        @Bean(name = "dataSource")
        @ConfigurationProperties("spring.datasource")
        public DataSource dataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        public JdbcTemplate jdbcTemplate() {
            return new JdbcTemplate(dataSource());
        }
    
        @Bean
        public DatabaseIdProvider databaseIdProvider() {
            DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
            Properties p = new Properties();
            p.setProperty("Oracle", "oracle");
            p.setProperty("MySQL", "mysql");
            p.setProperty("PostgreSQL", "postgresql");
            p.setProperty("DM DBMS", "dm");
            p.setProperty("SQL Server", "sqlserver");
            databaseIdProvider.setProperties(p);
            return databaseIdProvider;
        }
    
        @Primary
        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setDatabaseIdProvider(databaseIdProvider());
            factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
            return factoryBean;
        }
    }
    

    编写mapper.xml

    <!--
        <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap" databaseId="mysql">
            select account
            from SYS_USER
            where ID = #{id,jdbcType=CHAR}
        </select>
        
        <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap" databaseId="dm">
            select dep_code
            from SYS_USER
            where ID = #{id,jdbcType=CHAR}
        </select>
    -->
    

      <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
            select
            <if test="_databaseId == 'mysql'">
              account
            </if>
            <if test="_databaseId == 'dm'">
              dep_code
            </if>
            from SYS_USER
            where ID = #{id,jdbcType=CHAR}
      </select>
    

    相关文章

      网友评论

          本文标题:SpringBoot+Mybatis多数据库

          本文链接:https://www.haomeiwen.com/subject/afbajktx.html