Let's learn about the use and running process of the mybatis call stored procedure. Let's first create a simple stored procedure
DELIMITER $
CREATE PROCEDURE mybatis.ges_user_count (in Age INT, out User_count int)
BEGIN
SELECT count (*) From the users WHERE users.age=age into User_count;
End
The meaning of this stored procedure is simply to enter the age, and then execute SELECT COUNT (*) from the users where users.age = User_count, and the number of people who are equal to ages is assigned to User_count , or relatively simple.
Next is the call to the stored procedure that completes the call to the stored procedure by executing the following command.
Let's take a look at using MyBatis to invoke the stored procedure.
Usermapper.xml Add stored procedure call configuration:
<select id= "Count" statementtype= "callable" parametermap= "Getusercountmap" > Call
mybatis.ges_user_count ( ?,?)
Main function:
public class Learn1main {public
static void main (string [] args) {
//mybatis configuration file
string resource = ' learn/my Batis-config.xml ";
Load the MyBatis configuration file with the class loader (it also loads the associated mapping file)
InputStream is = Learn1Main.class.getClassLoader (). getResourceAsStream ( Resource);
Building sqlsession Factory
sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). Build (IS);
sqlsession session = Sessionfactory.opensession ();
map<string, integer> parametermap = new hashmap<string, integer> ();
Parametermap.put ("Age",);
Parametermap.put ("User_count",-1);
Session.selectone ("Com.tianjunwei.learn.learn1.entity.User.count", parametermap);
Integer result = Parametermap.get ("User_count");
SYSTEM.OUT.PRINTLN (result);
}
Run Result:
Its final execution in Defaultresultsethandler, there is a difference between calling ordinary SQL and stored procedures, and the execution of SQL statements is using CallableStatement.
HANDLE output PARAMETER///Call the stored procedure returns the result, placing the result value in the parameter @Override public void Handleoutputparameters (callablestateme
NT CS) throws SQLException {final Object parameterobject = Parameterhandler.getparameterobject ();
Final MetaObject Metaparam = Configuration.newmetaobject (Parameterobject);
Final list<parametermapping> parametermappings = Boundsql.getparametermappings (); /loop process each parameter for (int i = 0; i < parametermappings.size (); i++) {final parametermapping parametermapping = parameterm
Appings.get (i);
To determine the mode of the parameter if (parametermapping.getmode () = = Parametermode.out | | parametermapping.getmode () = = Parametermode.inout) { if (ResultSet.class.equals (Parametermapping.getjavatype ())) {Handlerefcursoroutputparameter (ResultSet)
Cs.getobject (i + 1), parametermapping, Metaparam);
else {final typehandler<?> Typehandler = Parametermapping.gettypehandler ();
Metaparam.setvalue (Parametermapping.getproperty (), Typehandler.getresult (CS, i + 1));
}}} private void Handlerefcursoroutputparameter (ResultSet rs, parametermapping parametermapping, MetaObject Metapara
m) throws SQLException {try {final String resultmapid = Parametermapping.getresultmapid ();
Final Resultmap Resultmap = Configuration.getresultmap (resultmapid);
Final Defaultresulthandler Resulthandler = new Defaultresulthandler (objectfactory);
Final Resultsetwrapper RSW = new Resultsetwrapper (rs, configuration);
Handlerowvalues (RSW, Resultmap, Resulthandler, New rowbounds (), NULL);
Metaparam.setvalue (Parametermapping.getproperty (), resulthandler.getresultlist ());
Finally {//issue #228 (Close resultsets) Closeresultset (RS); }
}
The above is a small series to introduce the MyBatis source analysis of the stored procedure call and run the process, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!