最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡(jiǎn)單使用方法

 更新時(shí)間:2024年07月08日 08:43:07   作者:Variazioni  
本文主要介紹了Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡(jiǎn)單使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

最近有兼容多數(shù)據(jù)庫(kù)的需求,原有數(shù)據(jù)庫(kù)使用的mysql,現(xiàn)在需要同時(shí)兼容mysql和pgsql,后期可能會(huì)兼容更多。

mysql和pgsql很多語(yǔ)法和函數(shù)不同,所以有些sql需要寫(xiě)兩份,于是在全網(wǎng)搜索如何在mapper中sql不通用的情況下兼容多數(shù)據(jù)庫(kù),中文網(wǎng)絡(luò)下,能搜到的解決方案大概有兩種:1.使用@DS注解的動(dòng)態(tài)數(shù)據(jù)源;2.使用數(shù)據(jù)庫(kù)廠商標(biāo)識(shí),即databaseIdProvider。第一種多用來(lái)同時(shí)連接多個(gè)數(shù)據(jù)源,且配置復(fù)雜,暫不考慮。第二種明顯符合需求,只需要指定sql對(duì)應(yīng)的數(shù)據(jù)庫(kù)即可,不指定的即為通用sql。

常規(guī)方法

在全網(wǎng)搜索databaseIdProvider的使用方法,大概有兩種:

1.在mybatis的xml中配置,大多數(shù)人都能搜到這個(gè)結(jié)果:

<databaseIdProvider type="DB_VENDOR">
  <property name="MySQL" value="mysql"/>
  <property name="Oracle" value="oracle" />
</databaseIdProvider>

然后在mapper中:

<select id="selectStudent" databaseId="mysql">
    select * from student where name = #{name} limit 1
</select>
<select id="selectStudent" databaseId="oracle">
    select * from student where name = #{name} and rownum < 2
</select>

2.創(chuàng)建mybatis的配置類:

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class MyBatisConfig {
  @Bean
  public DatabaseIdProvider databaseIdProvider() {
    VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
    Properties props = new Properties();
    props.setProperty("Oracle", "oracle");
    props.setProperty("MySQL", "mysql");
    props.setProperty("PostgreSQL", "postgresql");
    props.setProperty("DB2", "db2");
    props.setProperty("SQL Server", "sqlserver");
    provider.setProperties(props);
    return provider;
  }
  
  @Bean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setMapperLocations(
        new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*Mapper.xml"));
    factoryBean.setDatabaseIdProvider(databaseIdProvider());
    return factoryBean.getObject();
  }
}

這兩種方法,包括在mybatis的github和官方文檔的說(shuō)明,都是看得一頭霧水,因?yàn)榍昂鬅o(wú)因果關(guān)系,DB_VENDOR這種約定好的字段也顯得很奇怪,為什么要配置DB_VENDOR?為什么mysql需要寫(xiě)鍵值對(duì)?鍵值對(duì)的key是從那里來(lái)的?全網(wǎng)都沒(méi)有太清晰的說(shuō)明。

一些發(fā)現(xiàn)

有沒(méi)有更簡(jiǎn)單的辦法?

mybatis的入口是SqlSessionFactory,如果要了解mybatis的運(yùn)行原理,從這個(gè)類入手是最合適的,于是順藤摸瓜找到了SqlSessionFactoryBuilder類,這個(gè)類有很多build方法,打斷點(diǎn)之后發(fā)現(xiàn)當(dāng)前配置走的是

  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

這個(gè)Configuration類就非常顯眼了,點(diǎn)進(jìn)去之后發(fā)現(xiàn)這個(gè)類的成員變量就是可以在application.yml里直接設(shè)置值的變量

public class Configuration {
  protected Environment environment;
  protected boolean safeRowBoundsEnabled;
  protected boolean safeResultHandlerEnabled;
  protected boolean mapUnderscoreToCamelCase;
  protected boolean aggressiveLazyLoading;
  protected boolean multipleResultSetsEnabled;
  protected boolean useGeneratedKeys;
  protected boolean useColumnLabel;
  protected boolean cacheEnabled;
  protected boolean callSettersOnNulls;
  protected boolean useActualParamName;
  protected boolean returnInstanceForEmptyRow;
  protected String logPrefix;
  protected Class<? extends Log> logImpl;
  protected Class<? extends VFS> vfsImpl;
  protected LocalCacheScope localCacheScope;
  protected JdbcType jdbcTypeForNull;
  protected Set<String> lazyLoadTriggerMethods;
  protected Integer defaultStatementTimeout;
  protected Integer defaultFetchSize;
  protected ResultSetType defaultResultSetType;
  protected ExecutorType defaultExecutorType;
  protected AutoMappingBehavior autoMappingBehavior;
  protected AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior;
  protected Properties variables;
  protected ReflectorFactory reflectorFactory;
  protected ObjectFactory objectFactory;
  protected ObjectWrapperFactory objectWrapperFactory;
  protected boolean lazyLoadingEnabled;
  protected ProxyFactory proxyFactory;
  protected String databaseId;
  protected Class<?> configurationFactory;
  protected final MapperRegistry mapperRegistry;
  protected final InterceptorChain interceptorChain;
  protected final TypeHandlerRegistry typeHandlerRegistry;
  protected final TypeAliasRegistry typeAliasRegistry;
  protected final LanguageDriverRegistry languageRegistry;
  protected final Map<String, MappedStatement> mappedStatements;
  protected final Map<String, Cache> caches;
  protected final Map<String, ResultMap> resultMaps;
  protected final Map<String, ParameterMap> parameterMaps;
  protected final Map<String, KeyGenerator> keyGenerators;
  protected final Set<String> loadedResources;
  protected final Map<String, XNode> sqlFragments;
  protected final Collection<XMLStatementBuilder> incompleteStatements;
  protected final Collection<CacheRefResolver> incompleteCacheRefs;
  protected final Collection<ResultMapResolver> incompleteResultMaps;
  protected final Collection<MethodResolver> incompleteMethods;
  protected final Map<String, String> cacheRefMap;
……

這里面的配置有些非常眼熟,比如logImpl,可以使用mybatis.configuration.log-impl直接設(shè)置值,那么同理,databaseId是不是也可以使用mybatis.configuration.databaseId設(shè)置值?答案是肯定的,而且這樣設(shè)置值,繞過(guò)了databaseIdProvider也可以生效。

最簡(jiǎn)單的方法

如果你的springboot偏向使用application.yml配置或者使用了spring cloud config,又要兼容多數(shù)據(jù)庫(kù),那么你可以加一條配置

mybatis.configuration.database-id: mysql
或者
mybatis.configuration.database-id: orcale

然后在你的mapper中

<select id="selectStudent" databaseId="mysql">
    select * from student where name = #{name} limit 1
</select>
<select id="selectStudent" databaseId="oracle">
    select * from student where name = #{name} and rownum < 2
</select>
或者
<select id="selectStudent">
    select * from student where 
    <if test="_databaseId=='mysql'">
        name = #{name} limit 1
    </if>
    <if test="_databaseId=='oracle'">
        name = #{name} and rownum < 2
    </if>
</select>

即可切換數(shù)據(jù)庫(kù),不影響其他任何配置,而且也不用糾結(jié)databaseIdProvider里的key應(yīng)該怎么填寫(xiě)了。

到此這篇關(guān)于Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡(jiǎn)單使用方法的文章就介紹到這了,更多相關(guān)Mybatis兼容多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)菜單樹(shù)的示例代碼

    java實(shí)現(xiàn)菜單樹(shù)的示例代碼

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)菜單樹(shù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Java調(diào)用Python代碼實(shí)現(xiàn)Word轉(zhuǎn)化為PDF格式

    Java調(diào)用Python代碼實(shí)現(xiàn)Word轉(zhuǎn)化為PDF格式

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)調(diào)用Python代碼實(shí)現(xiàn)Word轉(zhuǎn)化為PDF格式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2026-03-03
  • SpringBoot輕松實(shí)現(xiàn)ip解析(含源碼)

    SpringBoot輕松實(shí)現(xiàn)ip解析(含源碼)

    IP地址一般以數(shù)字形式表示,如192.168.0.1,IP解析是將這個(gè)數(shù)字IP轉(zhuǎn)換為包含地區(qū)、城市、運(yùn)營(yíng)商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實(shí)現(xiàn)ip解析,需要的朋友可以參考下
    2023-10-10
  • Java接口防抖/冪等性解決方案(redis)

    Java接口防抖/冪等性解決方案(redis)

    在Java項(xiàng)目開(kāi)發(fā)過(guò)程中并發(fā)處理與冪等性問(wèn)題緊密相關(guān),這也導(dǎo)致了一些人認(rèn)為解決冪等性就是解決高并發(fā)的問(wèn)題,這篇文章主要介紹了Java接口防抖/冪等性(redis)的相關(guān)資料,需要的朋友可以參考下
    2025-07-07
  • SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的項(xiàng)目實(shí)踐

    SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的項(xiàng)目實(shí)踐

    在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常遇到需要同時(shí)操作多個(gè)數(shù)據(jù)源的情況,本文主要介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • IDEA EasyCode 一鍵幫你生成所需代碼

    IDEA EasyCode 一鍵幫你生成所需代碼

    這篇文章主要介紹了IDEA EasyCode 一鍵幫你生成所需代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 基于springboot+enum配置化的方法

    基于springboot+enum配置化的方法

    本文主要介紹利用Springboot結(jié)合枚舉類enum進(jìn)行自定義參數(shù)的初始化和應(yīng)用,通過(guò)@Value注解實(shí)現(xiàn)參數(shù)的動(dòng)態(tài)注入,以實(shí)現(xiàn)靈活可維護(hù)的配置管理,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • 詳解RSA加密算法的原理與Java實(shí)現(xiàn)

    詳解RSA加密算法的原理與Java實(shí)現(xiàn)

    這篇文章主要和大家分享非對(duì)稱加密中的一種算法,那就是 RSA 加密算法。本文介紹了RSA算法的原理與Java實(shí)現(xiàn),感興趣的小伙伴可以嘗試一下
    2022-10-10
  • IntelliJ IDEA中設(shè)置數(shù)據(jù)庫(kù)連接全局共享的步驟詳解

    IntelliJ IDEA中設(shè)置數(shù)據(jù)庫(kù)連接全局共享的步驟詳解

    本文詳解IntelliJ IDEA數(shù)據(jù)庫(kù)連接全局共享設(shè)置,通過(guò)創(chuàng)建/選擇數(shù)據(jù)源并設(shè)為全局,實(shí)現(xiàn)多項(xiàng)目間統(tǒng)一連接,提升效率、減少配置錯(cuò)誤,感興趣的可以了解一下
    2025-07-07
  • springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例

    springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例

    本文主要介紹了springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論

怀仁县| 临沧市| 成都市| 丰顺县| 乌兰察布市| 称多县| 普安县| 博白县| 南岸区| 洪雅县| 龙里县| 永丰县| 永和县| 陆良县| 东平县| 京山县| 泰和县| 丰顺县| 永福县| 滦平县| 察哈| 明星| 垣曲县| 古浪县| 手游| 菏泽市| 广丰县| 临洮县| 平顺县| 津南区| 芜湖县| 晋江市| 辽宁省| 涿州市| 洛扎县| 长春市| 浮梁县| 西青区| 泰安市| 斗六市| 岳阳市|