Hibernate3.x調用預存程序大揭密

來源:互聯網
上載者:User

三.編碼與測試

在準備工作完成後,進入編碼與測試階段,本例示範了在hibernate3.0中調用mysql的預存程序的方法。

1、hibernate的設定檔

在hibernate的設定檔中包含資料庫的串連資訊,以及加入OR mapping的xml格式的對應檔,該檔案如下(部分內容略):

……
  <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
  <property name="connection.username">root</property>
  <property name="connection.password">root</property>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  <mapping resource="com/amigo/proc/model/User.hbm.xml"/>
……

2、OR Mapping檔案

產生的OR Mapping檔案有User.java以及其對應的hibernate對應檔User.hbm.xml。其中User.java的內容如下:

public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/** 使用者id*/
private String userid;
/** 使用者姓名*/
private String name;
/** 使用者blog*/
private String blog;
//省略get/set方法
}

User.hbm.xml檔案的內容如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.amigo.proc.model">
   <class name="User" table="tbl_user">
     <id name="userid" column="userid">
       <generator class="assigned"/>
     </id>
     <property name="name" column="name" type="string" />
     <property name="blog" column="blog" type="string" />
   </class>
   <sql-query name="getUserList" callable="true">
  <return alias="user" class="User">
  <return-property name="userid" column="userid"/>
  <return-property name="name" column="name"/>
  <return-property name="blog" column="blog" />
  </return>
  {call getUserList()}
</sql-query>
</hibernate-mapping>

在該檔案中需注意<sql-query…></sql-query>中的這段代碼,調用的預存程序在其中定義,並定義了調用預存程序後將記錄組裝成User對象,同時對記錄的欄位與對象的屬性進行相關映射。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.