• springboot mybatis多数据库支持


    1.配置 application-dev.properties

    # ================================================
    #                   MyBatis配置
    # ================================================
    #开启驼峰命名
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.database-id=${database.type}
    

    注意database-id的配置
    2.在springboot中配置DatabaseIdProvider

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle", "oracle");
        properties.setProperty("MySQL", "mysql");
        properties.setProperty("DB2", "db2");
        properties.setProperty("Derby", "derby");
        properties.setProperty("H2", "h2");
        properties.setProperty("HSQL", "hsql");
        properties.setProperty("Informix", "informix");
        properties.setProperty("Microsoft SQL Server", "sqlserver");
        properties.setProperty("PostgreSQL", "postgresql");
        properties.setProperty("Sybase", "sybase");
        properties.setProperty("Hana", "hana");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }
    

    注意:数据库版本是否正确
    3.在 mapper.xml 中使用
    方法1

    <select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer" databaseId="sqlserver">
        select top(#{value})*
        from kcjs
      </select>
    
    <select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer" databaseId="mysql">
        select *
        from kcjs limit 0,#{value}
    </select>
    

    方法2

    <select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer">
        select
        <if test="_databaseId==sqlserver">
            top(#{value})*
            from kcjs
        </if>
        <if test="_databaseId==mysql">
            *
            from kcjs limit 0,#{value}
        </if>
    
    </select>
    
  • 相关阅读:
    Oracle 操作数据库(增删改语句)
    web----框架基础
    js----DOM对象
    易错之for循环
    python调用修改变量新方法
    js----基础
    web----Twisted
    web----Socket
    python----面向对象(2)
    python----面向对象
  • 原文地址:https://www.cnblogs.com/tangyouwei/p/14108271.html
Copyright © 2020-2023  润新知