Oracle入门--PL/SQL、游标、存储过程、自定义函数、触发器、MyBatis 操作(6)

本文深入解析了PL/SQL的基本语法、游标、存储过程、自定义函数及触发器,并介绍了如何通过Java的MyBatis框架进行数据库操作,包括环境搭建、基本操作和测试方法。

1. PL/SQL

1.1 概述

  • PL/SQL ,Oracle 对SQL扩展

1.2 语法:

  • 基本语法
-- 基本语法,(declare、exception可省略)
declare
   -- 声明,定义变量
begin
   -- 代码
   exception
     --异常处理
end;

在这里插入图片描述

2.1 PLSQL–游标

2.1.1 什么是游标

  • 游标是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。我们可以把游标理解为PL/SQL中的结果集
    在这里插入图片描述
  • 游标就是将查询结果存放到缓存区,可以通过游标依次获得数据

2.1.2 语法

declare
	--声明游标
	cursor 游标名称 is 查询语句;
begin
	open 游标名称;
	loop;
		-- 给游标指向当前行进行命名,方便获得当前行的数据
		fetch 游标名称 into 变量;
		-- 使用当前行数据
		-- 结束信息
		exit when 游标名称%notfound;
	end loop;
	close 游标名称;
end;
-- for循环
declare
   -- 声明游标
   cursor cur_pricetable is select * from t_pricetable where ownertypeid = 2;
   -- 变量
   v_pricetable t_pricetable%rowtype;
begin
   -- for遍历
   for v_pricetable in cur_pricetable
     loop
       dbms_output.put_line(v_pricetable.price);
     end loop;
end;

2.1.4 有参数游标

--- 具有参数的游标
declare
   -- 声明游标
   cursor cur_pricetable(v_ownertypeid number) is select * from t_pricetable where ownertypeid = v_ownertypeid;
   -- 变量
   v_pricetable t_pricetable%rowtype;
begin
   -- for遍历
   for v_pricetable in cur_pricetable(3)
     loop
       dbms_output.put_line(v_pricetable.price);
     end loop;
end;

2.2 存储过程

2.2.1 概述

  • 存储过程是被命名的PL/SQL块,存储于数据库中,是数据库对象的一种。应用程序可以调用存储过程,执行相应的逻辑
    在这里插入图片描述

2.2.2 语法

create [or replace] procedure 存储过程名称
(参数名 参数模式 类型,参数名2 参数模式 类型,....)
is|as
  --变量声明
begin
  --逻辑代码
end;
--参数模式:in、out、 in out

2.2.3 创建存放过程排错

  • 创建成功和失败效果图
    在这里插入图片描述
  • 检查错误原因:存储过程右键/View
    在这里插入图片描述
  • 根据提示排错
    在这里插入图片描述

2.3 自定义函数

2.3.1 概述

  • 函数分为内置函数和自定义函数。可以接收一个或多个参数,返回一个结果

  • 在函数中我们可以使用PL/SQL进行逻辑的处理
    在这里插入图片描述

2.3.2 语法

-- 语法
create function 函数名
(参数名 参数类型, ....)
	--注意:return 没有分号
	return 返回值类型
is
	--变量
begin
	--代码
	return 返回值;
end;
--需求: 创建函数,根据地址ID查询地址名称。
create or replace function getaddressname
(v_id number)
    return varchar2
is
  v_name t_address.name%type;  
begin
  -- 查询
  select name into v_name from t_address where id = v_id;
  -- 返回
  return v_name;
end;

-- 测试
select getaddressname(1) from dual;

2.4 触发器

2.4.1 什么是触发器

  • 数据库触发器是一个与表相关联的、存储的PL/SQL程序。每当一个特定的数据操作语句(Insert,update,delete)在指定的表上发出时,Oracle自动地执行触发器中定义的语句
    在这里插入图片描述

2.4.2 语法

create trigger 触发器名称
before | after 			--前置触发器、后置触发器
update of 列名			--更新时触发,取值:insert/update/delete
on--触发器检测的表
for each row			 -- 检测表中的每一行数据
declare
	--声明
begin
	--可以通过规定的关键字,获得对应数据
		-- :old 之前的数据(一行)
		-- :new 现在的数据(一行)
end;

2. Java操作

2.1 MyBatis 操作

2.1.1 搭建环境

  • 步骤一:创建项目 test_oracle
  • 步骤二:修改pom.xml文件(MyBatis相关依赖、Oracle驱动、测试依赖)
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>changgou3_parent</artifactId>
        <groupId>com.czxy.changgou3</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test_oracle</artifactId>

    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--通用mapper起步依赖-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!--mybatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!--简化 javabean-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--Oracle驱动-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>12.1.0.1-atlassian-hosted</version>
        </dependency>
    </dependencies>


</project>
  • 步骤三:创建yml文件(数据库基本4项 – Oracle驱动+Oracle路径)
spring:
  datasource:           #数据源配置
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:xe
    username: czxy56
    password: czxy56
  • 步骤四:启动类
package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestOracleApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestOracleApplication.class, args);
    }
}

2.1.2 MyBatis基本操作 + 测试

  • 步骤一:编写JavaBean,, t_area --> Area
package com.czxy.domain;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="t_area")
@Data
public class Area {
    @Id
    private Integer id;
    private String name;
}

  • 步骤二:编写Mapper
    在这里插入图片描述
package com.czxy.mapper;

import com.czxy.domain.Area;
import tk.mybatis.mapper.common.Mapper;
@org.apache.ibatis.annotations.Mapper
public interface AreaMapper extends Mapper<Area> {
}

  • 步骤三:测试类
    在这里插入图片描述
package com.czxy;

import com.czxy.mapper.AreaMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestOracleApplication.class)
public class AreaTest {
    @Resource
    private AreaMapper areaMapper;

    @Test
    public void testDemo01(){
        System.out.println(areaMapper);
    }
}

2.1.3 测试:

package com.czxy;

import com.czxy.domain.Area;
import com.czxy.mapper.AreaMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestOracleApplication.class)
public class AreaTest {
    @Resource
    private AreaMapper areaMapper;

    @Test
    public void testInsert(){
        //添加
        Area area = new Area();
        area.setName("测试");
        area.setId(7);

        areaMapper.insert( area );
    }

    @Test
    public void testUpdate(){
        Area area = new Area();
        area.setName("测试2");
        area.setId(7);

        areaMapper.updateByPrimaryKey( area );
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值