Using Ibatis to invoke Oracle stored procedures and functions in Java

Source: Internet
Author: User
Tags ticket


Hibernate provides a comprehensive database encapsulation mechanism of the "fully automated" ORM implementation, "fully automatic" ORM implementation of POJO and database tables mapping between, as well as the automatic generation and execution of SQL.
Ibatis is "semi-automatic" and does not automatically generate SQL execution for programmers during run time. The specific SQL needs to be written by the programmer and mapped to the specified POJO by mapping the configuration file to the parameters required by SQL and the resulting field returned. More flexible to give programmers more space to play.

The following code uses Ibatis-sqlmap-2.jar, which is tested in Windows Tomcat and passed in WebLogic under Linux.

First, use Ibatis to invoke Oracle stored procedures
1, create test stored procedures, name: Testpro

CREATE OR REPLACE PROCEDURE testpro (mylevel in VARCHAR2, myresult out  pls_integer) is

BEGIN
  myresult:=0;  
  If mylevel = ' A ' then 
     myresult:=0;
  End If;
  
  If mylevel = ' B ' then 
     myresult:=-1;
  End If;
  
  If mylevel = ' C ' then 
     myresult:=1;
  End If;
End;

2, call the stored procedure sqlmap file configuration

<parametermap id= "Testproparammap" class= "Java.util.HashMap" >
	<parameter property= "Mylevel" jdbctype= "VARCHAR" javatype= "java.lang.String" mode= "in"/> <parameter property=
	"Myresult" jdbctype= "NUMERIC" Javatype= "Java.lang.Integer" mode= "Out"/>
</parameterMap>
<procedure id= "Testoraclepro" parametermap= "Testproparammap" >
	{call Testpro (?,?)} 
</procedure>

3. Invoke the DAO layer code of the stored procedure

Public Object Testprodao (map map) {return
	getsqlmapclienttemplate (). queryForObject ("Ticket.testoraclepro", map) ;
}

4, Front call

Map Mappro = new HashMap ();
Mappro.put ("Mylevel", "B");
Object Rs=xxxxdao.testprodao (Mappro);
Logutil.debug ("rs=" +rs);
Logutil.debug ("myresult=" +mappro.get ("Myresult"));

Results:
Rs=null
Myresult=-1
From the test results, the return result is in the map, the output parameter is accessed as a key to access the

Second, using Ibatis to invoke Oracle functions

1, create test function, Name: Testfun

CREATE OR REPLACE FUNCTION testfun (mylevel in VARCHAR2) return number as

  myresult      Pls_integer: = 0;

BEGIN
  If mylevel = ' A ' then 
     myresult:=0;
  End If;
  
  If mylevel = ' B ' then 
     myresult:=-1;
  End If;
  
  If mylevel = ' C ' then 
     myresult:=1;
  End If;
  return myresult;
End;
2, call the function of the Sqlmap file configuration

<parametermap id= "Testfunparammap" class= "Java.util.HashMap" >
	<parameter property= "Myresult" Jdbctype= "NUMERIC" javatype= "Java.lang.Integer" mode= "Out"/> <parameter property= "mylevel" jdbctype= "
	VARCHAR "javatype=" java.lang.String "mode=" in "/>
</parameterMap>
<procedure id=" Testoraclefun "parametermap=" Testfunparammap ">
	{? = Call Testfun (?)} 
</procedure>
3, call the function of the DAO layer code

Public Object Testfundao (map map) {return
	getsqlmapclienttemplate (). queryForObject ("Ticket.testoraclefun", map) ;
}
4, Front call

Map mapfun = new HashMap ();
Mapfun.put ("Mylevel", "B");
Object Rs=xxxxdao.testprodao (mapfun);
Logutil.debug ("rs=" +rs);
Logutil.debug ("myresult=" +mapfun.get ("Myresult"))
Results:
Rs=null
Myresult=-1
From the test results, the return result is in the map, the output parameter is accessed as a key to access the

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.