Spring Boot MyBatis配置多種資料庫

五毛程式設計師發表於2019-02-18

mybatis-config.xml是支援配置多種資料庫的,本文將介紹在Spring Boot中使用配置類來配置。

1. 配置application.yml

# mybatis配置
mybatis:
  check-config-location: false
  type-aliases-package: ${base.package}.model
  configuration:
    map-underscore-to-camel-case: true
    # 二級快取的總開關
    cache-enabled: false
  mapper-locations: classpath:mapping/*.xml
複製程式碼

2. 新增資料來源配置類

/**
 * 資料來源配置
 * @author simon
 * @date 2019-02-18
 */
@Configuration
public class DataSourceConfig {
    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSource(){
        return DruidDataSourceBuilder.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("DB2", "db2");
        p.setProperty("SQL Server", "sqlserver");
        databaseIdProvider.setProperties(p);
        return databaseIdProvider;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setDatabaseIdProvider(databaseIdProvider());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        return factoryBean;
    }
}

複製程式碼

3. 在mapper.xml中使用

方法1

  <select id="findAuthorityByUrl" resultType="java.lang.String" databaseId="mysql">
    SELECT
      group_concat( tsma.authority ) as authority
    FROM
        t_side_menu tsm
        LEFT JOIN t_side_menu_authority tsma ON tsm.id = tsma.side_menu_id
  </select>

    <select id="findAuthorityByUrl" resultType="java.lang.String" databaseId="postgresql">
        SELECT
        string_agg( tsma.authority, ',') as authority
        FROM
        t_side_menu tsm
        LEFT JOIN t_side_menu_authority tsma ON tsm.id = tsma.side_menu_id
    </select>
複製程式碼

方法2

  <select id="selectByPids" parameterType="String" resultMap="SuperResultMap">
    SELECT
        tsm.*,
      <if test="_databaseId == 'mysql'">
          group_concat( tsma.authority ) as authority
      </if>
      <if test="_databaseId == 'postgresql'">
          string_agg( tsma.authority, ',') as authority
      </if>
    FROM
        t_side_menu tsm
        LEFT JOIN t_side_menu_authority tsma ON tsm.id = tsma.side_menu_id
    WHERE pid IN (#{pids})
    GROUP BY
        tsm.id
  </select>
複製程式碼

題外話

如果有興趣,請給oauthserer專案一個star。oauthserver是一個基於Spring Boot Oauth2的完整的獨立的Oauth2 Server微服務。專案的目的是,僅僅需要建立相關資料表,修改資料庫的連線資訊,你就可以得到一個Oauth2 Server微服務。

相關文章