Jorm use session to implement calls to stored procedures and functions __ stored procedures

Source: Internet
Author: User
Tags assert sessions

The most common operation in Jorm is to use session to implement CRUD operations on the data, because it is more consistent with Jorm's design programming standards. Hibernate is also used to manage database operations in the Jorm, which is very similar to hibernate. I was stuck with this session's stored procedure call and function call a few days ago, so I wrote it here today, to be read here.

1. Write stored procedures and functions

Stored procedure
DROP PROCEDURE   IF      EXISTS  count_has_same_name_proc;

CREATE PROCEDURE Count_has_same_name_proc (xname VARCHAR)
  BEGIN
    SELECT  Count (*) from  T_user WHERE name like  xname;
  End;

Functions
DROP   function   IF      EXISTS  count_has_same_name_func;

CREATE    FUNCTION count_has_same_name_func (xname  VARCHAR) 
RETURNS  INT
    BEGIN     
          DECLARE  s  INT;
	  SELECT   COUNT (ID) into S-  t_user WHERE name like  xname;
	  return s;
    End;

2, write the required SessionManager

Package com.boonya.jorm.service.utils;

Import Org.javaclub.jorm.Jorm;
Import org.javaclub.jorm.Session;

public class SessionManager
{

	private static final session session = Jorm.getsession ();

	public static Sessions getinstance ()
	{return
		sessionmanager.session
	}

}
3, session of the stored procedures and function calls of the unit test case
Package com.boonya.jorm.test;
Import java.sql.SQLException;
Import java.util.List;
Import Junit.framework.Assert;
Import org.javaclub.jorm.Session;
Import org.javaclub.jorm.jdbc.JdbcException;
Import Org.javaclub.jorm.jdbc.callable.ProcedureCaller;
Import Org.junit.Test;
Import Com.boonya.jorm.entity.User;
Import Com.boonya.jorm.service.utils.LogManager;
Import Com.boonya.jorm.service.utils.SessionManager;
Import com.mysql.jdbc.CallableStatement;

Import Com.mysql.jdbc.ResultSet;
		public class Sessiontest {@Test the public void session () {session session=sessionmanager.getinstance ();
	Assert.assertnotnull ("session: Conversation is not empty", sessions);
		@Test public void Getfirtuser () {int minid = 50;
		String sql = "SELECT * from T_user WHERE ID >";
		Session session = Sessionmanager.getinstance ();
		User user = Session.loadfirst (user.class, SQL, MiniD);
	Assert.assertnotnull ("First user eligible:", user);
		@Test public void Deletebetweenids () {int startid = Endid = 100; Session SESsion = Sessionmanager.getinstance ();
		Boolean flag = false;
		Session.begintransaction ();
			try {session.delete (user.class, "ID >" + startid + "and ID <" + endid);
			Flag = true;
		Session.commit ();
		catch (Jdbcexception e) {logmanager.setlog ("Interval ID Delete: Failed!", e);
		finally {session.endtransaction ();
	} assert.asserttrue ("Interval ID deletion: Success", flag);
		@Test public void Getnamelist () {String sql = ' select name from T_user WHERE ID >? ';
		Session session = Sessionmanager.getinstance ();
		list<string> names = session.list (String.class, SQL, 10); SYSTEM.OUT.PRINTLN (names = null?)
	"List is empty": "List Length:" + names.size ());
		@Test public void Sessiontransaction () {Session session = Sessionmanager.getinstance ();
		Session.begintransaction ();
			try {User user = new User ("Peng", "male", "UI designer");
			Session.save (user);
		Session.commit ();
			catch (Exception e) {logmanager.setlog ("Transaction Add User: Failed!", e);
		Session.rollback ();finally {session.endtransaction ();

		@Test public void procedure () {final String pro = ' Call Count_has_same_name_proc (?) ';

		Final session session = Sessionmanager.getinstance (); 
			String res = Session.call (new Procedurecaller () {@Override public callablestatement prepare () throws SQLException
				{CallableStatement cs = (callablestatement) getsession (). getconnection (). Preparecall (PRO);
				Cs.setstring (1, "Boonya");
			Return CS; @Override public Object Callback (Java.sql.CallableStatement CS) throws SQLException {ResultSet rs = (resu
				Ltset) Cs.executequery ();
				Assert.assertnotnull ("Procedure:resultset", RS);
					while (Rs.next ()) {int count = Rs.getint (1);

				System.out.println ("Procedure: Statistics the same user result:" + count);
			} return RS;
		}
		});
	System.out.println (RES);

		@Test public void function () {final String pro = "Select Count_has_same_name_func (?)"; Final session session = Sessionmanager.getinstanCE (); 
			String res = Session.call (new Procedurecaller () {@Override public callablestatement prepare () throws SQLException
				{CallableStatement cs = (callablestatement) getsession (). getconnection (). Preparecall (PRO);
				Cs.setstring (1, "Boonya");
			Return CS; @Override public Object Callback (Java.sql.CallableStatement CS) throws SQLException {ResultSet rs = (resu
				Ltset) Cs.executequery ();
				Assert.assertnotnull ("Function:resultset", RS);
					while (Rs.next ()) {int count = Rs.getint (1);

				System.out.println ("Function: Statistics The result of a user with the same name:" + count);
			return rs.tostring ();
		}
		});
	System.out.println (RES);
 }

}
Note: Misguided invocation notation: pro= "{? = Call Hello_proc (?)}"; It is understandable that the coder is looking at this as an object, and then assigning the results of the query to a default data type to store, but the call is unsuccessful.  You can add "{}" to the SQL statement, which should read: "{call Hello_proc (?)}". This can be found in open source China, address: http://www.oschina.net/p/gerald-jorm


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.