Under the SSM framework, MyBatis calls Oracle database stored procedures, functions, and uses sequences to add auto-increment columns

This article mainly sorts out operations such as calling functions and stored procedures in the Oracle database in the mybatis framework. And the use of the SSM framework to connect to the Oracle database. The business logic is for test reference only.

1. Create tables, sequences, stored procedures, and return value functions on the Oracle database side

1 The table structure is as follows:

<!--1、试题表-->
create table question(
       qid number(4,0) primary key,--试题ID
       que varchar2(50) not null, --问题
       answer varchar2(2) not null,--标准答案
       score number(2,0) not null --这道题的分数
)

<!--2、试卷详情表-->
create table paper(
       pid varchar2(20) not null,--试卷ID
       qid number(4,0) not null, --对应的试题ID
       useran varchar2(2) not null --用户选择的答案  
)

2 Create a sequence as an add operation when used as an auto-increment column

create sequence seq_stu
start with 1
increment by 1
cache 10

3 Write a stored procedure to add data

This is only used as an exercise and has no practical meaning!

create or replace procedure add_procedure(
       stuname varchar2, --用户名
       stuage number,  --年龄
       address varchar2  --家庭住址
)
is
begin
  begin
    insert into student (stuno,stuname,stuage,address) values
    (seq_stu.nextval,stuname,stuage,address); --这里使用序列做自增id
  end;
end add_procedure;

4 Write a function to calculate the total score of a certain test paper

Pass in the test paper number and calculate the total score of this test paper.

--利用游标计算试卷总分数,返回给程序
create or replace function que_fun(
       input_pid varchar2 --传入试卷ID
)
return number --返回试卷总分
is
begin
   declare
       cursor que_cursor is              
              select pid,paper.qid as qid2,useran,question.qid as qid1,answer,score
              from paper inner join question 
              on question.qid=paper.qid
              where pid=input_pid;
       querow que_cursor%rowtype;
       return_num number(2,0):=0; --声明返回值
   begin
       for querow in que_cursor loop
           if querow.useran = querow.answer then 
               return_num:=return_num+querow.score;
           end if;
       end loop; 
       return  return_num;   
   end;    
end que_fun;

2. Use sequence, stored procedure, and call function to get the return value in the mapper.xml file in MyBatis

<mapper namespace="com.dao.StuDao">
    <!--1、利用存储过程做数据添加操作-->
    <select id="insertStudent" parameterType="Student" statementType="CALLABLE">
        {call add_procedure( #{stuName,mode=IN,jdbcType=VARCHAR}, #{stuAge,mode=IN,jdbcType=DOUBLE},
         #{address,mode=IN,jdbcType=VARCHAR})}
    </select>


	<!--2、利用序列做自增列,添加操作-->
    <insert id="insertStu" parameterType="Student">
        insert into student (stuNo,stuname,stuage,address) values
        <!--seq_stu.nextval表示名叫seq_stu这个序列的下一个值-->
        (seq_stu.nextval,#{stuName},#{stuAge},#{address})
    </insert>


	<!--3、调用函数,传入试卷编号,获取返回值(总分数)-->
    <select id="getAllScore" statementType="CALLABLE" parameterType="java.util.Map">
        {#{callback,mode=OUT,jdbcType=INTEGER} =call que_fun(#{inputPid,mode=IN,jdbcType=VARCHAR})}
        <!--callback用来接收函数返回值、inputPid表示传入函数中的参数、二者均通过map集合的形式传入到预编译好的sql中,详见service中的调用-->
    </select>

</mapper>
  • mode=IN means that it is a parameter passed in a function or a stored procedure, and must be capitalized;
  • mode=OUT means the return value of the function, or the parameter that means the return value in the stored procedure, must be capitalized;
  • To call stored procedures and functions, use statementType=' CALLABLE';
  • Calling the function to get the return value passed in the Map<String,Object> collection, the keys in the collection respectively represent the parameters passed in to the function and the return value of the function.

Three, dao interface and call in service

  • Preparation of dao interface
public interface StuDao {
    
    
	//1、利用存储过程添加数据
    public  Integer insertStudent(Student student);
	//2、常规添加数据,使用序列做自增列
    public Integer insertStu(Student student);
	//3、调用函数,传入试卷编号计算这张试卷的总分数
    public  void getAllScore(Map<String,Object> map);
}
  • service layer call

Calling a function with a return value uses the map collection to pass parameters

@Service
public class StudentServiceImpl implements StudentService {
    
    
    @Autowired
    private StuDao stuDao;

    public Integer addStu(Student student) {
    
    
        return stuDao.insertStudent(student);
    }

    public Integer addStu2(Student student) {
    
    
        return stuDao.insertStu(student);
    }

    public Integer getAllScore(String inputPid) {
    
    
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("inputPid",inputPid);
        map.put("callback", 0);
        //将集合传入dao层
        stuDao.getAllScore(map);
        //调用函数成功,从map中获取结果
        Integer callback=(Integer) map.get("callback");
        return callback;
    }
}

Fourth, the relevant configuration of SSM to connect to the Oracle database

  • jar dependency
    <!--添加oracle依赖-->
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.4.0-atlassian-hosted</version>
    </dependency>
  • The jdbc.properties configuration file for connecting to the Oracle database
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=liugang
jdbc.password=root
maxWait=5000
initialSize=2
maxActive=6
minIdle=2

Common SQL and related PL/SQL of Oracle database:
https://blog.csdn.net/weixin_49702090/article/details/109373379

Guess you like

Origin blog.csdn.net/weixin_49702090/article/details/109407679