MySQL存儲過程中的Hibernate JDBC
一、如何認(rèn)識Hibernate JDBC存儲過程
存儲過程是在數(shù)據(jù)庫中預(yù)編譯好的SQL語句,只需一次編譯即可,大大提高了sql 語句執(zhí)行的速度。
好處:提高了速度;
壞處:不便于移植。
二、存儲過程的語法:
a) 創(chuàng)建一個存儲過程
無參:
- Create procedure creatp()
- Begin
Sql 語句;
End;
有參:
Create procedure creatp( 參數(shù)名1 參數(shù)類型1 ,參數(shù)名2 參數(shù)類型2 )
Begin
Sql 語句;
End;
例如:
無參:
- DELIMITER $$
- DROP PROCEDURE IF EXISTS `test`.`createp` $$
- CREATE PROCEDURE `test`.`createp` ( idv int)
- BEGIN
- select * from `table_test` where id=idv;
- END $$
- DELIMITER ;
有參:
- DELIMITER $$
- DROP PROCEDURE IF EXISTS `test`.`queryProV` $$
- CREATE DEFINER=`root`@`localhost` PROCEDURE `queryProV`(tid integer)
- BEGIN
- select * from table_test where id=tid;
- END $$
- DELIMITER ;
b) 使用存儲過程
無參:Call 存儲過程名();
有參:Call 存儲過程名( 參數(shù)值) ;
例如:
call createp(2);
c) 刪除存儲過程
Drop procedure 存儲過程名;
例如:
- drop procedure createp;
三、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 class Test {
- /**
- * 獲取數(shù)據(jù)庫的連接對象
- * @return 數(shù)據(jù)庫連接對象
- */
- private Connection getConnection(){
- final String MYSQL_DRIVER="com.mysql.jdbc.Driver";// 數(shù)據(jù)庫連接的驅(qū)動
- final String MYSQL_USERNAME="root";// 數(shù)據(jù)庫連接的url
- final String MYSQL_PASSWORD="123456";// 數(shù)據(jù)庫連接的密碼
- final String MYSQL_URL="jdbc:mysql://localhost:3306/test";// 數(shù)據(jù)庫連接的url
- try{
- Class.forName(MYSQL_DRIVER);
- return DriverManager.getConnection(MYSQL_URL, MYSQL_USERNAME, MYSQL_PASSWORD);
- }catch(Exception e){
- e.printStackTrace();
- }
- return 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()){
- System.out.println("id:"+rs.getInt(1)+"||name:"+rs.getString(2));
- }
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(rs!=null){
- rs.close();
- }
- if(cstmt!=null){
- cstmt.close();
- }
- if(conn!=null){
- conn.close();
- }
- }
- }
- /**
- ===========================================
- DELIMITER $$
- DROP PROCEDURE IF EXISTS `test`.`queryProV` $$
- CREATE DEFINER=`root`@`localhost` PROCEDURE `queryProV`(tid integer)
- 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.setInt(1, 2);// 就是把上句中***個問號的值設(shè)為2
- rs=cstmt.executeQuery();
- while(rs.next()){
- System.out.println("id:"+rs.getInt(1)+"||name:"+rs.getString(2));
- }
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(rs!=null){
- rs.close();
- }
- if(cstmt!=null){
- cstmt.close();
- }
- if(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.setInt(1, 2);// 就是把上句中***個問號的值設(shè)為2
- boolean tag=cstmt.execute();
- System.out.println(" 刪除成功");
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(cstmt!=null){
- cstmt.close();
- }
- if(conn!=null){
- conn.close();
- }
- }
- }
- public static void main(String [] args) throws SQLException{
- Test tset =new Test();
- }
- }
四、Hibernate JDBC中使用
4.1 在數(shù)據(jù)庫中創(chuàng)建存儲過程;
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>
- <class name="com.test.hibernate.TableTest" table="table_test"
- catalog="test">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="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>
- </class>
- <!-- 無參數(shù): Hibernate 存儲過程配置 -->
- <!-- name: 查詢語句在hibernate 中的名字, 隨便取 -->
- <sql-query name="queryPro1" callable="true">
- <!-- alias: 查詢返回的對象的別名, 隨便取
- class 查詢返回的類的全路徑,否則會抱找不到類的錯誤 -->
- <return alias="t1" class="com.test.hibernate.TableTest">
- <!-- 查詢中每一個參數(shù)的設(shè)置,name 表示為別名 -->
- <return-property name="c1" column="id" />
- <return-property name="c2" column="name" />
- <return-property name="c3" column="value" />
- </return>
- <!-- mysql 中存儲過程 -->
- { call queryPro()}
- </sql-query>
- <!-- 有參數(shù): Hibernate 存儲過程配置 -->
- <!-- name: 查詢語句在hibernate 中的名字, 隨便取 -->
- <sql-query name="queryPro2" callable="true">
- <!-- alias: 查詢返回的對象的別名, 隨便取
- class 查詢返回的類的全路徑,否則會抱找不到類的錯誤 -->
- <return alias="TableTest" class="com.test.hibernate.TableTest">
- <!-- 查詢中每一個參數(shù)的設(shè)置,name 表示為別名 -->
- <return-property name="id" column="id" />
- <return-property name="name" column="name" />
- <return-property name="value" column="value" />
- </return>
- <!-- 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 class TestDao {
- /**
- * 無參數(shù)的hibernate 存儲過程查詢
- */
- public void query(){
- Session session=null;
- try{
- session=HibernateSessionFactory.getSession();
- Query qy=session.getNamedQuery("queryPro1");
- List<TableTest> list=qy.list();
- if(list!=null){
- for(int i=0;i<list.size();i++){
- TableTest test=list.get(i);
- System.out.println("id="+test.getId()+"||name:"+test.getName());
- }
- }
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(session!=null){
- session.close();
- }
- }
- }
- /**
- * 有參數(shù)的hibernate 的存儲過程之查詢
- */
- public void queryV(){
- Session session=null;
- try{
- session=HibernateSessionFactory.getSession();
- Query qy=session.getNamedQuery("queryPro2");
- qy.setInteger(0, 3);// 設(shè)置指定位置的參數(shù),注意參數(shù)從0 開始。
- List<TableTest> list=qy.list();
- if(list!=null){
- for(int i=0;i<list.size();i++){
- TableTest test=list.get(i);
- System.out.println("id="+test.getId()+"||name:"+test.getName());
- }
- }
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(session!=null){
- session.close();
- }
- }
- }
- /**
- * 此種方法是jdbc 的方法
- * 優(yōu)點:不用在在配置文件中進(jìn)行配置
- * 缺點:無法返回對象
- * @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.setInt(1, 3);
- rs=pst.executeQuery();
- while(rs.next()){
- System.out.println("id="+rs.getInt(1)+"||name:"+rs.getString(2));
- }
- }catch(Exception e){e.printStackTrace();}
- finally{
- if(rs!=null){
- rs.close();
- }
- if(pst!=null){
- pst.close();
- }
- if(conn!=null){
- conn.close();
- }
- if(session!=null){
- session.close();
- }
- }
- }
- public static void main(String [] args) throws SQLException{
- TestDao td=new TestDao();
- td.queryOther();
- }
- }
【編輯推薦】
- 在Weblogic中實現(xiàn)JDBC的功能
- 詳解JDBC與Hibernate區(qū)別
- JDBC連接MySQL數(shù)據(jù)庫關(guān)鍵四步
- 五步精通SQL Server 2000 JDBC驅(qū)動安裝與測試
- 詳解JDBC驅(qū)動的四種類型
- JDBC存儲過程在Oracle中的獲取結(jié)果集
【責(zé)任編輯:彭凡 TEL:(010)68476606】