hibernatejdbc:MySQL存储过程中的Hibernate JDBC



  、如何认识Hibernate JDBC存储过程

  存储过程是在数据库中预编译好SQL语句只需次编译即可大大提高了sql 语句执行速度

  好处:提高了速度;

  坏处:不便于移植

   2、存储过程语法:

  a) 创建个存储过程

  无参:    

Create procedure creatp   
    Begin  


  Sql 语句;

  End;

  有参:

  Create procedure creatp( 参数名1 参数类型1 参数名2 参数类型2 )

  Begin

  Sql 语句;

  End;

  例如:

  无参:

DELIMITER $$   
DROP PROCEDURE IF EXISTS `test`.`createp` $$   
CREATE PROCEDURE `test`.`createp` ( idv )   
BEGIN   
  select * from `table_test` where id=idv;   
END $$   
DELIMITER ;  


  有参:

DELIMITER $$   
DROP PROCEDURE IF EXISTS `test`.`queryProV` $$   
CREATE DEFINER=`root`@`localhost` PROCEDURE `queryProV`(tid eger)   
BEGIN   
  select * from table_test where id=tid;   
END $$   
DELIMITER ;  


  b)     使用存储过程

  无参:Call 存储过程名;

  有参:Call 存储过程名( 参数值) ;

  例如:

  call createp(2);

  c)     删除存储过程

  Drop procedure 存储过程名;

  例如:

drop procedure createp;  

   3、Hibernate JDBC使用存储过程

package com.test.dao;   
import java.sql.CallableStatement;   
import java.sql.Connection;   
import java.sql.DriverManager;   
import java.sql.PreparedStatement;   
import java.sql.ResultSet;   
import java.sql.SQLException;   
import org.hibernate.Session;   
import com.test.hibernate.HibernateSessionFactory;   
/**   
  * MySQl 存储过程___   
  *   JDBC   
  * @author Administrator   
  *   
  */   
public  Test {   
     /**    
       * 获取数据库连接对象   
       * @    数据库连接对象   
       */   
     private  Connection getConnection{        
         final String MYSQL_DRIVER="com.mysql.jdbc.Driver";// 数据库连接驱动   
         final String MYSQL_USERNAME="root";// 数据库连接url   
         final String MYSQL_PASSWORD="123456";// 数据库连接密码   
         final String MYSQL_URL="jdbc:mysql://localhost:3306/test";// 数据库连接url           
         try{   
              Class.forName(MYSQL_DRIVER);   
               DriverManager.getConnection(MYSQL_URL, MYSQL_USERNAME, MYSQL_PASSWORD);   
          }catch(Exception e){   
              e.prStackTrace;   
         }   
         null;   
     }   
     /**   
     =   
DELIMITER $$   
DROP PROCEDURE IF EXISTS `test`.`queryPro` $$   
CREATE DEFINER=`root`@`localhost` PROCEDURE `queryPro`   
BEGIN   
  select * from table_test ;   
END $$   
DELIMITER ;   
       =   
       * 这是个无参存储过程jdbc 使用思路方法   
       * @throws SQLException   
       */   
     public void testQuery throws SQLException{   
         Connection conn=null;   
         CallableStatement cstmt=null;   
         ResultSet rs=null;   
         try{   
              conn=this.getConnection;   
              cstmt =conn.prepareCall("{call queryPro}");   
              rs=cstmt.executeQuery;   
              while(rs.next){   
                   .out.prln("id:"+rs.getInt(1)+"||name:"+rs.getString(2));   
              }   
         }catch(Exception e){e.prStackTrace;}   
         finally{   
              (rs!=null){   
                   rs.close;   
              }   
              (cstmt!=null){   
                   cstmt.close;   
              }   
              (conn!=null){   
                   conn.close;   
              }   
         }   
     }   
     /**   
       =   
DELIMITER $$   
DROP PROCEDURE IF EXISTS `test`.`queryProV` $$   
CREATE DEFINER=`root`@`localhost` PROCEDURE `queryProV`(tid eger)   
BEGIN   
  select * from table_test where id=tid;   
END $$   
DELIMITER ;   
       =   
       * 这是个有参存储过程jdbc 使用思路方法   
       * @throws SQLException   
       */   
     public void testQueryV throws SQLException{   
         Connection conn=null;   
         CallableStatement cstmt=null;   
          ResultSet rs=null;   
         try{   
              conn=this.getConnection;   
              cstmt =conn.prepareCall("{call queryProV(?)}");   
              cstmt.Int(1, 2);// 就是把上句中第个问号值设为2   
              rs=cstmt.executeQuery;   
              while(rs.next){   
                   .out.prln("id:"+rs.getInt(1)+"||name:"+rs.getString(2));   
              }   
         }catch(Exception e){e.prStackTrace;}   
         finally{   
              (rs!=null){   
                   rs.close;   
              }   
              (cstmt!=null){   
                   cstmt.close;   
              }   
              (conn!=null){   
                   conn.close;   
              }   
        }   
     }   
     /**   
      =   
DELIMITER $$   
DROP PROCEDURE IF EXISTS `test`.`delPro` $$   
CREATE DEFINER=`root`@`localhost` PROCEDURE `delPro`(tid nteger)   
BEGIN   
  delete from table_test where id=tid;   
END $$   
DELIMITER ;   
       =   
       * 这是个有参存储过程jdbc 使用思路方法   
       * @throws SQLException   
       */   
    public void testDel throws SQLException{   
         Connection conn=null;   
         CallableStatement cstmt=null;   
         try{   
              conn=this.getConnection;   
              cstmt =conn.prepareCall("{call delPro(?)}");   
              cstmt.Int(1, 2);// 就是把上句中第个问号值设为2   
              boolean tag=cstmt.execute;       
              .out.prln(" 删除成功");   
         }catch(Exception e){e.prStackTrace;}   
         finally{   
                 (cstmt!=null){   
                   cstmt.close;   
              }   
              (conn!=null){   
                   conn.close;   
              }   
        }   
     }   
     public  void (String  args) throws SQLException{   
     Test t = Test;   
          }   
}  


   4、Hibernate  JDBC中使用

  4.1 在数据库中创建存储过程;

  4.2 在hibernate 中配置存储过程以及返回对象

<?xml version="1.0" encoding="utf-8"?>   
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
<!--   
     Mapping file autogenerated by MyEclipse Persistence Tools   
-->   
<hibernate-mapping>   
     < name="com.test.hibernate.TableTest" table="table_test"   
         catalog="test">   
         <id name="id" type="java.lang.Integer">   
              <column name="id" />   
              <generator ="assigned" />   
         </id>   
         <property name="name" type="java.lang.String">   
             <column name="name" length="45" />   
         </property>   
         <property name="value" type="java.lang.String">   
              <column name="value" length="45" />   
         </property>   
    </>   
     <!-- 无参数: Hibernate 存储过程配置 -->   
    <!-- name: 查询语句在hibernate 中名字, 随便取 -->      
   <sql-query name="queryPro1" callable="true">   
    <!-- alias: 查询返回对象别名, 随便取           
     查询返回全路径否则会抱找不到类 -->   
    < alias="t1" ="com.test.hibernate.TableTest">   
         <!-- 查询中每个参数设置name 表示为别名 -->   
         <-property  name="c1" column="id" />   
        <-property  name="c2" column="name" />   
         <-property  name="c3" column="value" />   
    </>   
     <!-- mysql 中存储过程 -->   
    { call queryPro}   
    </sql-query>   
   <!-- 有参数: Hibernate 存储过程配置 -->   
   <!-- name: 查询语句在hibernate 中名字, 随便取 -->      
    <sql-query name="queryPro2" callable="true">   
    <!-- alias: 查询返回对象别名, 随便取           
     查询返回全路径否则会抱找不到类 -->   
    < alias="TableTest" ="com.test.hibernate.TableTest">   
         <!-- 查询中每个参数设置name 表示为别名 -->   
         <-property  name="id" column="id" />   
         <-property  name="name" column="name" />   
         <-property  name="value" column="value" />   
    </>   
    <!-- mysql 中存储过程 -->   
    {call queryProV(?)}   
   </sql-query>   
</hibernate-mapping>   
4.3. 使用   
package com.test.dao;   
import java.sql.CallableStatement;   
import java.sql.Connection;   
import java.sql.PreparedStatement;   
import java.sql.ResultSet;   
import java.sql.SQLException;   
import java.util.List;   
import org.hibernate.Query;   
import org.hibernate.Session;   
import com.test.hibernate.HibernateSessionFactory;   
import com.test.hibernate.TableTest;   
public  TestDao {   
     /**   
       * 无参数hibernate 存储过程查询   
       */   
     public void query{   
        Session session=null;   
        try{   
              session=HibernateSessionFactory.getSession;             
              Query qy=session.getNamedQuery("queryPro1");              
              List<TableTest> list=qy.list;   
              (list!=null){   
                   for( i=0;i<list.size;i){                      
                       TableTest test=list.get(i);   
                       .out.prln("id="+test.getId+"||name:"+test.getName);   
                   }   
             }      
         }catch(Exception e){e.prStackTrace;}   
         finally{   
              (session!=null){   
                   session.close;   
             }   
         }      
     }   
     /**   
       * 有参数hibernate 存储过程的查询   
       */   
     public void queryV{   
        Session session=null;   
         try{   
              session=HibernateSessionFactory.getSession;             
              Query qy=session.getNamedQuery("queryPro2");         
              qy.Integer(0, 3);// 设置指定位置参数注意参数从0 开始   
              List<TableTest> list=qy.list;   
              (list!=null){   
                   for( i=0;i<list.size;i){                      
                       TableTest test=list.get(i);   
                       .out.prln("id="+test.getId+"||name:"+test.getName);   
                   }   
              }      
         }catch(Exception e){e.prStackTrace;}   
         finally{   
              (session!=null){   
                   session.close;   
              }   
         }      
     }   
    /**   
      * 此种思路方法是jdbc 思路方法   
       * 优点:不用在在配置文件中进行配置   
      * 缺点:无法返回对象   
       * @throws SQLException   
       */   
     public void queryOther throws SQLException{   
         Session session=null;   
         Connection conn=null;   
         PreparedStatement pst=null;   
         ResultSet rs=null;   
         try{   
              session=HibernateSessionFactory.getSession;   
                conn=session.connection;   
                pst=conn.prepareCall("{call queryProV(?)}");   
              pst.Int(1, 3);   
                rs=pst.executeQuery;   
              while(rs.next){   
                   .out.prln("id="+rs.getInt(1)+"||name:"+rs.getString(2));   
              }   
                
        }catch(Exception e){e.prStackTrace;}   
         finally{   
              (rs!=null){   
                   rs.close;   
              }   
              (pst!=null){   
                  pst.close;   
              }   
              (conn!=null){   
                   conn.close;   
              }   
              (session!=null){   
                   session.close;   
              }   
         }      
    }   
     public  void (String  args) throws SQLException{   
         TestDao td= TestDao;   
         td.queryOther;   
     }   
}  


Tags:  hibernatemysql hibernate存储过程 hibernate与jdbc hibernatejdbc

延伸阅读

最新评论

发表评论