• Oracle 安装及 Spring 使用 Oracle


    参考内容:

    docker安装oracle数据库史上最全步骤(带图文)

    Mac下oracle数据库客户端

    Docker安装Oracle

    docker能安装oracle吗

    Batch script for add a auto-increased primary key for exist table with records

    Docker 安装 Oracle11g

    注意:下列安装方式仅适用于x86架构服务器,不适用于arm架构服务器

    # 拉取 oracle11,镜像有点大,需要花一些时间
    docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
    
    # 查看镜像是否拉取成功
    docker images
    
    # 给镜像重新打 tag,原来的名字太长了
    docker tag registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g oracle11g:latest
    
    # 启动 oracle11g 容器
    docker run --name=oracle11g -itd -p 1521:1521
    
    # 进入容器进行配置
    docker exec -it oracle11g /bin/bash
    
    # 切换到 root 用户,密码为:helowin
    su root
    
    # 编辑配置文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    编辑/etc/profile,在其中增加如下内容:

    export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
    export ORACLE_SID=helowin
    export PATH=$ORACLE_HOME/bin:$PATH
    
    • 1
    • 2
    • 3

    编辑完成后,需要刷新上述环境变量才能使用。

    # 刷新环境变量
    source /etc/profile
    
    # 创建软链接
    ln -s $ORACLE_HOME/bin/sqlplus /usr/bin
    
    # 切换到 oracle 用户
    su - oracle
    
    # 登陆 sqlplus
    sqlplus /nolog
    conn /as sysdba
    
    # 修改 system 用户密码
    alter user system identified by system;
    # 修改 sys 用户密码
    alter user sys identified by system;
    
    # 创建内部管理员账号
    create user test identified by test;
    
    # 将 dba 权限授权给内部管理员账号和密码
    grant connect,resource,dba to test;
    
    # 修改密码规则策略为密码永不过期
    ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
    
    # 修改数据库最大连接数据
    alter system set processes=1000 scope=spfile;
    
    • 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

    修改上述信息后,需要重新启动数据库才会生效。

    conn /as sysdba
    
    # 关闭数据库
    shutdown immediate;
    
    # 启动数据库
    startup;
    
    # 退出软链接
    exit;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    客户端连接 Oracle

    以 Navicat 客户端为例,新建连接时按下图方式填写连接信息即可,密码即为system。需要注意的是,在 Windows 下选择 SID 或是服务名均可连接成功,但是在 Mac 下需要选择 SID 方式才能连接成功。

    在这里插入图片描述

    Oracle 实现主键自增

    Oracle 在创建表的时候,不能像 MySQL 那样选择主键直接自增,但是我们可以通过给表创建序列和触发器去实现自增。下文以创建 USER 表为例。

    -- 删除原有 USER 表
    DROP TABLE "TEST"."USER";
    -- 创建 USER 表
    CREATE TABLE "TEST"."USER" (
      "id" NUMBER NOT NULL,
      "gmt_create" DATE NOT NULL,
      "gmt_modified" DATE NOT NULL,
      "is_deleted" NUMBER NOT NULL,
      "login" NVARCHAR2(255) NOT NULL,
      "passwd" NVARCHAR2(255) NOT NULL,
      "nick" NVARCHAR2(255) NOT NULL,
      "phone" NVARCHAR2(255),
      "head_img" NVARCHAR2(255),
      "status" NVARCHAR2(255),
      "remark" NCLOB
    );
    
    -- 删除原有序列
    DROP SEQUENCE "TEST"."USER_SEQ";
    -- 创建 USER_SEQ 序列,最小值为 1
    CREATE SEQUENCE "TEST"."USER_SEQ" 
    -- 最小值为 1
    MINVALUE 1 
    -- 最大值为 9999999999999999999999999999
    MAXVALUE 9999999999999999999999999999 
    -- 每次增加 1
    INCREMENT BY 1 
    -- 将 20 个序列值放入缓存
    CACHE 20;
    
    -- 创建触发器
    CREATE TRIGGER "TEST"."USER_TRIGGER" 
    -- 在插入数据前执行
    BEFORE INSERT ON "TEST"."USER" 
    -- 命名老数据为 OLD,新数据为 NEW
    REFERENCING OLD AS "OLD" NEW AS "NEW" 
    -- 针对表的每一行都执行触发器
    FOR EACH ROW 
    -- 将序列值赋值给 id
    BEGIN
    	:NEW."id" := USER_SEQ.NEXTVAL;
    END;
    /
    
    • 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

    需要注意的是,上面的/符号不能少。执行插入语句时可以发现,id会自动增加。

    INSERT INTO "TEST"."USER" ("gmt_create", "gmt_modified", "is_deleted", "login", "passwd", "nick", "phone", "head_img", "status", "remark") VALUES (TO_DATE('2023-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2023-04-01 17:04:30', 'SYYYY-MM-DD HH24:MI:SS'), '0', 'user', '123', 'Jack', '1111', 'head.jpg', '激活', '测试');
    
    • 1

    Java Spring+Mybatis 使用 Oracle 及配置分页

    application.properties文件配置信息:

    # mybatis
    spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
    spring.datasource.url=jdbc:oracle:thin:@8127.0.0.1:1521:helowin
    spring.datasource.username=system
    spring.datasource.password=system
    mybatis.mapper-locations=classpath*:mybatis/*.xml
    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    
    # pageHelper
    pagehelper.helperDialect=oracle
    pagehelper.reasonable=true
    pagehelper.supportMethodsArguments=true
    pagehelper.params=count=countSql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    pom.xml配置文件关键信息。

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <properties>
            <java.version>1.8java.version>
            <maven.compiler.target>1.8maven.compiler.target>
            <maven.compiler.source>1.8maven.compiler.source>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    
            <spring.boot-version>2.1.3.RELEASEspring.boot-version>
        properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>${spring.boot-version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
    
                <dependency>
                    <groupId>org.mybatis.spring.bootgroupId>
                    <artifactId>mybatis-spring-boot-starterartifactId>
                    <version>2.1.0version>
                dependency>
    
                <dependency>
                    <groupId>com.oracle.ojdbcgroupId>
                    <artifactId>ojdbc8artifactId>
                    <version>19.3.0.0version>
                dependency>
    
                <dependency>
                    <groupId>com.github.pagehelpergroupId>
                    <artifactId>pagehelper-spring-boot-starterartifactId>
                    <version>1.4.3version>
                dependency>
    
                <dependency>
                    <groupId>com.github.pagehelpergroupId>
                    <artifactId>pagehelper-spring-boot-starterartifactId>
                dependency>
            dependencies>
        dependencyManagement>
    project>
    
    • 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
  • 相关阅读:
    使用postman做接口测试
    UDS - 深论Security Access Service
    【学习笔记77】ajax的函数封装
    如何使用zx编写shell脚本
    1312. 让字符串成为回文串的最少插入次数
    面试中的批判性思维:如何展示你的分析能力
    JAVA基础(三十二)——反射之创建对象
    数据结构简答题综合
    Linux 内存管理 pt.3
    【校招VIP】前端JS语言之语法相关
  • 原文地址:https://blog.csdn.net/heuguangxu/article/details/134276664