Spring Boot MyBatis配置多种数据库

栏目: Java · 发布时间: 5年前

内容简介:mybatis-config.xml是支持配置多种数据库的,本文将介绍在Spring Boot中使用配置类来配置。如果有兴趣,请给

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微服务。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical Django Projects, Second Edition

Practical Django Projects, Second Edition

James Bennett / Apress / 2009 / 44.99

Build a django content management system, blog, and social networking site with James Bennett as he introduces version 1.1 of the popular Django framework. You’ll work through the development of ea......一起来看看 《Practical Django Projects, Second Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码