• springcloud适配mysql和oracle数据库


    mysql和oracle数据库部分语法不相同,为了适配两种数据库(修改配置文件方式),可以先操作sql,然后通过代码级别再实现。比如oracle限制返回的条数:rownum<=1 但是mysql的语法是:limit 1,你可以通过返回所有数据,然后再get(0)去除第一条数据,

    今天的重点:springcloud+tk.mybatis+oracle/mysql

    1.先上依赖:

    父工程部分依赖:

    复制代码

    org.springframework.boot
    spring-boot-starter-parent
    2.2.2.RELEASE

    复制代码



    com.xxx
    xxxxxxxx
    0.0.1-SNAPSHOT

    4.0.0
    xxxxx-xxxx
    xxxx-xxxxx

    
        
            org.springframework.boot
            spring-boot-starter-web
            ${springboot.version}
        
    
        
            org.springframework.boot
            spring-boot-starter-test
            ${springboot.version}
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
    
        
            tk.mybatis
            mapper-spring-boot-starter
            2.1.5
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
        
        
        
        
    
    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-autoconfigure
            1.3.2
        
    
    
        
            com.zhdgps.oracle
            ojdbc6
            11.2.0.1.0
        
    
        
            com.alibaba
            druid
            1.1.9
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
    
        
    
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.13
        
    
        
            mysql
            mysql-connector-java
            8.0.21
        
    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
        
    
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
    
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
    
        
        
            com.auth0
            java-jwt
            3.10.0
        
    
        
            org.projectlombok
            lombok
            1.18.10
        
    
        
            com.alibaba
            easyexcel
            2.2.6
        
    
        
            com.alibaba
            fastjson
            1.2.62
        
    
        
            org.springframework.data
            spring-data-redis
            ${springboot.version}
        
    
        
            org.apache.commons
            commons-pool2
            2.9.0
        
    
        
        
            redis.clients
            jedis
            3.3.0
        
    
        
        
            org.apache.commons
            commons-lang3
            3.11
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
        
            org.springframework
            spring-tx
            5.2.9.RELEASE
            compile
        
    
        
            commons-io
            commons-io
            2.8.0
            compile
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${springboot.version}
                
                    com.zhd.eoms.EomsApplication
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
                
                
                    
                        
                            repackage
                        
                    
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    复制代码 根据上面的依赖选择,不要都贴上,

    然后使用mybatis-generator自动生成entity、mapper和xml映射文件,

    然后就是今天的重点:怎样配置数据库id的类型

    第一种:自增ID,可以这样配置:

    @Id
    @Column(insertable = false, name = "ID")
    @GeneratedValue( generator = "JDBC")
    private Integer id;
    
    • 1
    • 2
    • 3
    • 4

    这样oracle和mysql都适用;

    自增适合哪种经常查询信息,因为id是天然有序,效率高一些,

    如果是UUID:

    @Id
    @Column(name = "ID")
    @KeySql(genId = UUIDGenId.class)
    
    • 1
    • 2
    • 3

    复制代码
    public class UUIDGenId implements GenId{
    @Override
    public String genId(String s, String s1) {
    return UUID.randomUUID().toString().replace(“-”,“”);
    }
    }
    复制代码
    UUIDGenId 类
    复制代码
    public class UUIDGenId implements GenId{//
    @Override
    public String genId(String s, String s1) {
    return UUID.randomUUID().toString().replace(“-”,“”);
    }
    }

  • 相关阅读:
    做个小工具显示UE里地形的高度图和权重图
    大语言模型中常见小模型LLM垂直领域应用微调数据集
    加密货币三箭的崩盘华尔街LTCM的历史重演
    PyQt,PySide-槽函数被执行了两次
    Git使用方法与IDEA集成Git
    Python图像处理:局部直方图均衡化和自动色彩均衡化
    字符串和内存函数
    Kotlin FileInputStream read File
    Docker 安装 MySQL、Redis、Nginx
    SVG: 可伸缩的矢量图形
  • 原文地址:https://blog.csdn.net/weixin_43214644/article/details/126322963