You are on page 1of 19

Programering

Home Questions Articles Ask a question


Mybatis basic operation of the stored
procedure

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Making mybatis calls the stored procedure is very convenient, the following began to study

how Mybatis/Ibatis 2 Oracle stored procedure calls. First learn a simple output text

examples:

CREATE OR REPLACE PROCEDURE proc_out(yes IN VARCHAR2,fly OUT VARCHAR2)

AS

begin

dbms_output.put_line(yes);

fly:='return something';

end proc_out;

Making calls to the Mybatis configuration is as follows:

<select id="testOutput" statementType="CALLABLE"

parameterType="hashmap">

<![CDATA[ {call proc_out(#{yes, mode=IN, jdbcType=VARCHAR}, #{gog,

mode=OUT, jdbcType=VARCHAR})}]]>

</select>

The following making test method:

public void TestOutputCallable(SqlSessionFactory ssf2) {

SqlSession sqlSession = ssf2.openSession();


Map params = new HashMap();

// Parameters of calling a stored procedure name can

not define the parameters of the stored procedure name remains the

same, as long as the guarantee of their order is consistent.

params.put("yes", "china");

sqlSession.selectOne("test.testOutput", params);

String result = (String) params.get("gog");

System.out.println(result);

Making Ibatis configuration is as follows:

<parameterMap id="stringOutMap" class="java.util.Map">

<!The mode parameter is used to set -- into parameters

or return parameter>

<parameter property="yes" javaType="String"

jdbcType="VARCHAR"

mode="IN" />

<parameter property="fly" javaType="String"

jdbcType="VARCHAR"

mode="OUT" />

</parameterMap>

<procedure id="testOutput" parameterMap="stringOutMap">

<![CDATA[{call proc_out(?,?)}]]>

</procedure>

Making test method:

public void TestOutputCallable(SqlMapClient sqlMapper) throws

SQLException {

Map mapIn = new HashMap();


mapIn.put("yes", "china");

mapIn.put("fly", "");

sqlMapper.queryForObject("test.testOutput",mapIn);

String result=(String) mapIn.get("fly");

System.out.println(result);

Ps, Ibatis for each input to create a Map to specify the input and output parameters, with

special uncomfortable, or easy to use Mybatis

Here making making began to use the table, first create table Emp:

-- Create table

create table EMP

EMPNO NUMBER not null,

ENAME VARCHAR2(30) not null,

JOB VARCHAR2(15),

MGR NUMBER,

HIREDATE DATE,

SALE NUMBER,

COMM NUMBER,

DEPTNO NUMBER

-- Create/Recreate primary, unique and foreign key constraints

alter table EMP

add primary key (EMPNO)

Making new JavaBean


package bean;

import java.util.Date;

public class EmpBean {

@Override

public String toString() {

return "EmpBean [empNo=" + empNo + ", ename=" + ename +

", job=" + job

+ ", mrg=" + mrg + ", hireDate=" +

hireDate.toLocaleString()

+ ", sale=" + sale + ", comm=" + comm +

", depNo=" + depNo

+ "]";

private long empNo;

private String ename;

private String job;

private long mrg;

private Date hireDate;

private long sale;

private long comm=0;

private long depNo;

public long getEmpNo() {

return empNo;

public void setEmpNo(long empNo) {

this.empNo = empNo;
}

public String getEname() {

return ename;

public void setEname(String ename) {

this.ename = ename;

public String getJob() {

return job;

public void setJob(String job) {

this.job = job;

public long getMrg() {

return mrg;

public void setMrg(long mrg) {

this.mrg = mrg;

public Date getHireDate() {

return hireDate;

public void setHireDate(Date hireDate) {


this.hireDate = hireDate;

public long getSale() {

return sale;

public void setSale(long sale) {

this.sale = sale;

public long getComm() {

return comm;

public void setComm(long comm) {

this.comm = comm;

public long getDepNo() {

return depNo;

public void setDepNo(long depNo) {

this.depNo = depNo;

}
Making a primary key first according to the Emp examples of records, stored procedure

corresponding to:

create or replace procedure getEmpById(V_USERID IN NUMBER,

V_CURSOR OUT SYS_REFCURSOR) is

begin

OPEN V_CURSOR FOR

SELECT * from emp WHERE empno = V_USERID;

end getEmpById;

Making Mybatis configuration is as follows:

<resultMap id="enameMap" type="bean.EmpBean">

<result column="EMPNO" property="empNo" />

<result column="ENAME" property="ename" />

<result column="JOB" property="job" />

<result column="MGR" property="mrg" />

<result column="HIREDATE" property="hireDate" />

<result column="SALE" property="sale" />

<result column="COMM" property="comm" />

<result column="DEPTNO" property="depNo" />

</resultMap>

<update id="selectEmpById" statementType="CALLABLE"

parameterType="map">

<![CDATA[

call getEmpById(#{userid,mode=IN,jdbcType=DECIMAL},

#{emp,mode=OUT,jdbcType=CURSOR,javaType=java.sql.ResultSet,resultMap=e

nameMap})
]]>

</update>

Making method of testing for:

public void TestGetEmpByIdCallable(SqlSessionFactory ssf2) {

SqlSession sqlSession = ssf2.openSession();

Map params = new HashMap();

// Parameters of calling a stored procedure name can

not define the parameters of the stored procedure name remains the

same, as long as the guarantee of their order is consistent.

params.put("userid", 7769);

sqlSession.selectOne("test.selectEmpById", params);

List<EmpBean> list = (List<EmpBean>) params.get("emp");

System.out.println(list.size() + "---" +

list.get(0).toString());

Making Ibatis configuration:

<parameterMap id="empListParam" class="java.util.Map">

<parameter property="userid" javaType="Long"

jdbcType="NUMBER"

mode="IN" />

<parameter property="result" jdbcType="ORACLECURSOR"

javaType="java.sql.ResultSet" mode="OUT" />

</parameterMap>

<procedure id="selectEmpById" parameterMap="empListParam"

resultClass="bean.EmpBean">

<![CDATA[{call getEmpById(?,?)}]]>

</procedure>
Making Ibatis test method:

public void TestEmpByIdCallable(SqlMapClient sqlMapper) throws

SQLException {

Map t=new HashMap();

t.put("userid",7782L);

List<EmpBean> list =

sqlMapper.queryForList("test.selectEmpById",t);

System.out.println(list.size()+"----

="+list.get(0).toString());

Making the following learning to use stored procedures for common CRUD operations, first

of all is new, new stored procedure

create or replace procedure addEmp(e_no in long,

e_name in varchar2,

e_job in varchar2,

e_mgr in long,

e_date in date,

e_sale in long,

e_comm in long,

e_depno in long,

message out varchar2) is

begin

insert into emp

(EMPNO, ENAME, JOB, MGR, HIREDATE, SALE, COMM, DEPTNO)

VALUES

(e_no, e_name, e_job, e_mgr, e_date, e_sale, e_comm, e_depno);

message := 'Insert the user table success';

commit;
EXCEPTION

WHEN OTHERS THEN

message := 'Insert fails the user table';

end addEmp;

Making Mybatis configuration is as follows:

<insert id="addEmp" statementType="CALLABLE">

{call

addEmp(#{empNo},#{ename},#{job},#{mrg},#{hireDate},#{sale},#{co

mm},#{depNo},#{message,

mode=OUT,javaType=string,jdbcType=VARCHAR})}

</insert>

Making test method:

public void TestAddEmpCallable(SqlSessionFactory ssf2) {

SqlSession sqlSession = ssf2.openSession();

Map params = new HashMap();

params.put("empNo", 123);

params.put("ename", "testadd");

params.put("job", "testjob");

params.put("mrg", 123);

params.put("hireDate", new Date());

params.put("sale", 8000);

params.put("comm", 1);

params.put("depNo", 20);

sqlSession.selectOne("test.addEmp", params);

String result = (String) params.get("message");

System.out.println(result);

}
Making configuration for Ibatis:

<parameterMap id="empInfoMap" class="java.util.Map">

<parameter property="empNo" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />

<parameter property="ename" jdbcType="VARCHAR"

javaType="java.lang.String"

mode="IN" />

<parameter property="job" jdbcType="VARCHAR"

javaType="java.lang.String"

mode="IN" />

<parameter property="mrg" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />

<parameter property="hireDate" jdbcType="DATE"

javaType="java.util.Date"

mode="IN" />

<parameter property="sale" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />

<parameter property="comm" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />

<parameter property="depNo" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />

<parameter property="message" jdbcType="VARCHAR"

javaType="java.lang.String"

mode="OUT" />
</parameterMap>

<procedure id="addEmp" parameterMap="empInfoMap">

<![CDATA[{call addEmp(?,?,?,?,?,?,?,?,?)}]]>

</procedure>

Test method for making corresponding to:

public void addEmpCallable(SqlMapClient sqlMapper)throws SQLException{

Map params = new HashMap();

params.put("empNo", 124L);

params.put("ename", "testadd");

params.put("job", "testjob");

params.put("mrg", 124L);

params.put("hireDate", new Date());

params.put("sale", 8000L);

params.put("comm", 2L);

params.put("depNo", 20L);

params.put("message","");

sqlMapper.update("test.addEmp",params);

System.out.println(params.get("message"));

The following making is modified, the stored procedure is as follows:

create or replace procedure updateEmp(e_no in long,

e_name in varchar2,

e_job in varchar2,

e_mgr in long,

e_date in date,

e_sale in long,

e_comm in long,
e_depno in long,

message out varchar2) is

begin

update emp u

set ENAME = e_name,

JOB = e_job,

MGR = e_mgr,

HIREDATE = e_date,

SALE=e_sale,

COMM=e_comm,

DEPTNO=e_depno

where EMPNO = e_no;

message := 'Update the user table success';

commit;

EXCEPTION

WHEN OTHERS THEN

message := 'Update the user table failure';

end updateEmp;

Making Mybatis matching:

<update id="updateEmp" statementType="CALLABLE">

{call

updateEmp(#{empNo},#{ename},#{job},#{mrg},#{hireDate},#{sale},#

{comm},#{depNo},#{message,

mode=OUT,javaType=string,jdbcType=VARCHAR})}

</update>

Making test method:

public void TestUpdateEmpCallable(SqlSessionFactory ssf2) {


SqlSession sqlSession = ssf2.openSession();

Map params = new HashMap();

params.put("empNo", 123);

params.put("ename", "testupdate");

params.put("job", "testjob2");

params.put("mrg", 123);

params.put("hireDate", new Date());

params.put("sale", 8500);

params.put("comm", 1);

params.put("depNo", 20);

sqlSession.selectOne("test.updateEmp", params);

String result = (String) params.get("message");

System.out.println(result);

Making configuration for Ibatis:

<procedure id="updateEmp" parameterMap="empInfoMap">

<![CDATA[{call updateEmp(?,?,?,?,?,?,?,?,?)}]]>

</procedure>

Test method for making corresponding to:

public void updateEmpCallable(SqlMapClient sqlMapper)throws

SQLException{

Map params = new HashMap();

params.put("empNo", 124L);

params.put("ename", "testupdate");

params.put("job", "testjob");

params.put("mrg", 125L);

params.put("hireDate", new Date());

params.put("sale", 8500L);
params.put("comm", 3L);

params.put("depNo", 20L);

params.put("message","");

sqlMapper.update("test.updateEmp",params);

System.out.println(params.get("message"));

Making the query operation, storage process:

create or replace package JUV is

TYPE CUR_GETEMP IS REF CURSOR;

end JUV;

create or replace procedure getAllEmp(empList out JUV.CUR_GETEMP)

as

begin

open empList for select * from emp;

end getAllEmp;

Making Mybatis configuration:

<select id="getAllEmp" statementType="CALLABLE">

<![CDATA[{call

getAllEmp(#{empList,mode=OUT,javaType=java.sql.ResultSet,jdbcType=CURS

OR,resultMap=enameMap})}]]>

</select>

Making test method:

public void TestGetAllEmpsCallable(SqlSessionFactory ssf2) {

SqlSession sqlSession = ssf2.openSession();

Map params = new HashMap();


sqlSession.selectOne("test.getAllEmp", params);

List<EmpBean> beanList = (List<EmpBean>)

params.get("empList");

for (EmpBean empBean : beanList) {

System.out.println(empBean);

Making making Ibatis configuration:

<procedure id="getEmps" parameterMap="searchParam"

resultClass="bean.EmpBean">

<![CDATA[{call getAllEmp(?)}]]>

</procedure>

Test method for making corresponding to:

public void TestEmpListsCallable(SqlMapClient sqlMapper) throws

SQLException {

List<EmpBean> list =

sqlMapper.queryForList("test.getEmps");

for (EmpBean empBean : list) {

System.out.println(empBean);

Making the delete operation, the stored procedure for:

create or replace procedure delEmp(u_id in varchar2, message out

varchar2) is

begin

delete emp where EMPNO = u_id;


message := 'Delete user table success';

commit;

EXCEPTION

WHEN OTHERS THEN

message := 'Failed to delete the user table';

end delEmp;

Making Mybatis configuration:

<delete id="delEmp" statementType="CALLABLE">

{call delEmp(#{id},#{message,

mode=OUT,javaType=string,jdbcType=VARCHAR})}

</delete>

Making method of testing for:

public void TestDelEmpCallable(SqlSessionFactory ssf2) {

SqlSession sqlSession = ssf2.openSession();

Map params = new HashMap();

params.put("id", 7844);

sqlSession.selectOne("test.delEmp", params);

String result = (String) params.get("message");

System.out.println(result);

Making Ibatis configuration:

<parameterMap id="delEmpMap" class="java.util.Map">

<parameter property="empNo" jdbcType="NUMBER"

javaType="java.lang.Long"

mode="IN" />
<parameter property="message" jdbcType="VARCHAR"

javaType="java.lang.String"

mode="OUT" />

</parameterMap>

<procedure id="delEmp" parameterMap="delEmpMap">

<![CDATA[{call delEmp(?,?)}]]>

</procedure>

Test method for making making corresponding to:

public void delEmpCallable(SqlMapClient sqlMapper)throws SQLException{

Map params = new HashMap();

params.put("empNo", 124L);

params.put("message","");

sqlMapper.update("test.delEmp",params);

System.out.println(params.get("message"));

Making over.

Making making digression:

Making making in the testing process, Find Mybatis in the Number type record does not

exist when the automatic return 0, Ibatis error, Don't know Is it right? Ibatis configuration

error, In addition, Ibatis personal feeling of calling a stored procedure should have more

concise, Welcome advice, Write not place, Please forgive me, In addition, The examples in

this article on the net also to, Simply put them together.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from

IPSwitch. Free Download

Posted by Perry at October 21, 2014 - 4:02 PM

Latest Questions
 is there any criteria for garmin map update?
 Have you problem printer in error state?
 Assignment Help
 Get Best 300 savage ammo
 Best CDR Help Service Provider Company in All Country
 Solution

©2019 Programering Contact Us Privacy Policy

You might also like