精通Hibernate:第一個Hibernate應用
一、在Java應用中使用Hibernate的步驟
- 創建Hibernate的配置文件
- 創建持久化類
- 創建對象-關系映射文件
- 通過Hibernate API編寫訪問數據庫的代碼
二、Helloapp應用的結構
三、Hibernate的配置文件(hibernate.properties)
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- hibernate.connection.driver_class=com.mysql.jdbc.Driver
- hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB
- hibernate.connection.username=root
- hibernate.connection.password=1234
- hibernate.show_sql=true
四、創建持久化類Customer
- 持久化類符合JavaBean的規范,包含一些屬性,以及與之對應的getXXX()和setXXX()方法。
- 持久化類有一個id屬性,用來惟一標識Customer類的每個對象。在面向對象術語中,這個id屬性被稱為對象標識符(OID,Object Identifier),通常它都用整數表示
- Hibernate要求持久化類必須提供一個不帶參數的默認構造方法
- package mypack;
- import java.io.Serializable;
- import java.sql.Date;
- import java.sql.Timestamp;
- public class Customer implements Serializable {
- private Long id;
- private String name;
- private String email;
- private String password;
- private int phone;
- private String address;
- private char sex;
- private boolean married;
- private String description;
- private byte[] image;
- private Date birthday;
- private Timestamp registeredTime;
- public Customer(){}
- public Long getId(){
- return id;
- }
- private void setId(Long id){
- this.id = id;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
- public String getEmail(){
- return email;
- }
- public void setEmail(String email){
- this.email =email ;
- }
- public String getPassword(){
- return password;
- }
- public void setPassword(String password){
- this.password =password ;
- }
- public int getPhone(){
- return phone;
- }
- public void setPhone(int phone){
- this.phone =phone ;
- }
- public String getAddress(){
- return address;
- }
- public void setAddress(String address){
- this.address =address ;
- }
- public char getSex(){
- return sex;
- }
- public void setSex(char sex){
- this.sex =sex ;
- }
- public boolean isMarried(){
- return married;
- }
- public void setMarried(boolean married){
- this.married =married ;
- }
- public String getDescription(){
- return description;
- }
- public void setDescription(String description){
- this.description =description ;
- }
- public byte[] getImage() {
- return this.image;
- }
- public void setImage(byte[] image) {
- this.image = image;
- }
- public Date getBirthday() {
- return this.birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Timestamp getRegisteredTime() {
- return this.registeredTime;
- }
- public void setRegisteredTime(Timestamp registeredTime) {
- this.registeredTime = registeredTime;
- }
- }
注意:
- getXXX()和setXXX()方法可以采用任意的訪問級別,他的命名規則必須符合特定的命名規則,“get”和“set”后面緊跟屬性的名字,并且屬性名的首字母為大寫,如name屬性的get方法為getName()。
- 如果持久化類的屬性為boolean類型,那么它的get方法名可以用get做前綴也可以用is做前綴。
五、創建數據庫Schema
- drop database if exists SAMPLEDB;
- create database SAMPLEDB;
- use SAMPLEDB;
- create table CUSTOMERS (
- ID bigint not null primary key,
- NAME varchar(15) not null,
- EMAIL varchar(128) not null,
- PASSWORD varchar(8) not null,
- PHONE int ,
- ADDRESS varchar(255),
- SEX char(1) ,
- IS_MARRIED bit,
- DESCRIPTION text,
- IMAGE blob,
- BIRTHDAY date,
- REGISTERED_TIME timestamp
- );
#p#
六、創建對象-關系映射文件Customer.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>
- <class name="mypack.Customer" table="CUSTOMERS">
- <id name="id" column="ID" type="long">
- <generator class="increment"/>
- </id>
- <property name="name" column="NAME" type="string" not-null="true" />
- <property name="email" column="EMAIL" type="string" not-null="true" />
- <property name="password" column="PASSWORD" type="string" not-null="true"/>
- <property name="phone" column="PHONE" type="int" />
- <property name="address" column="ADDRESS" type="string" />
- <property name="sex" column="SEX" type="character"/>
- <property name="married" column="IS_MARRIED" type="boolean"/>
- <property name="description" column="DESCRIPTION" type="text"/>
- <property name="image" column="IMAGE" type="binary"/>
- <property name="birthday" column="BIRTHDAY" type="date"/>
- <property name="registeredTime" column="REGISTERED_TIME" type="timestamp"/>
- </class>
- </hibernate-mapping>
<id>元素映射OID
<generator>子元素用來設定標識符生成器。Hibernate提供了提供了多種內置的實現。
<property>元素映射值類型屬性
- name屬性:指定持久化類的屬性的名字。
- column屬性:指定與類的屬性映射的表的字段名。
- type屬性:指定Hibernate映射類型。Hibernate映射類型是Java類型與SQL類型的橋梁。
采用XML文件來配置對象-關系映射的優點:
- Hibernate既不會滲透到上層域模型中,也不會滲透到下層數據模型中。
- 軟件開發人員可以獨立設計域模型,不必強迫遵守任何規范。
- 數據庫設計人員可以獨立設計數據模型,不必強迫遵守任何規范。
- 對象-關系映射不依賴于任何程序代碼,如果需要修改對象-關系映射,只需修改XML文件,不需要修改任何程序,提高了軟件的靈活性,并且使維護更加方便。
#p#
七、創建BusinessService類
- package mypack;
- import javax.servlet.*;
- import org.hibernate.*;
- import org.hibernate.cfg.Configuration;
- import java.io.*;
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.util.*;
- public class BusinessService{
- public static SessionFactory sessionFactory;
- /** 初始化Hibernate,創建SessionFactory實例 */
- static{
- try{
- // 根據默認位置的Hibernate配置文件的配置信息,創建一個Configuration實例
- Configuration config = new Configuration();
- //加載Customer類的對象-關系映射文件
- config.addClass(Customer.class);
- // 創建SessionFactory實例 */
- sessionFactory = config.buildSessionFactory();
- }catch(RuntimeException e){e.printStackTrace();throw e;}
- }
- /** 查詢所有的Customer對象,然后調用printCustomer()方法打印Customer對象信息 */
- public void findAllCustomers(ServletContext context,PrintWriter out) throws Exception{
- Session session = sessionFactory.openSession(); //創建一個會話
- Transaction tx = null;
- try {
- tx = session.beginTransaction(); //開始一個事務
- Query query=session.createQuery("from Customer as c order by c.name asc");
- List customers=query.list();
- for (Iterator it = customers.iterator(); it.hasNext();) {
- printCustomer(context,out,(Customer) it.next());
- }
- tx.commit(); //提交事務
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- /** 持久化一個Customer對象 */
- public void saveCustomer(Customer customer){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.save(customer);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- /** 按照OID加載一個Customer對象,然后修改它的屬性 */
- public void loadAndUpdateCustomer(Long customer_id,String address){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Customer c=(Customer)session.get(Customer.class,customer_id);
- c.setAddress(address);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- /**刪除Customer對象 */
- public void deleteCustomer(Customer customer){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.delete(customer);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- /** 選擇向控制臺還是Web網頁輸出Customer對象的信息 */
- private void printCustomer(ServletContext context,PrintWriter out,Customer customer)throws Exception{
- if(context!=null)
- printCustomerInWeb(context,out,customer);
- else
- printCustomer( out,customer);
- }
- /** 把Customer對象的信息輸出到控制臺,如DOS 控制臺*/
- private void printCustomer(PrintWriter out,Customer customer)throws Exception{
- byte[] buffer=customer.getImage();
- FileOutputStream fout=new FileOutputStream("photo_copy.gif");
- fout.write(buffer);
- fout.close();
- out.println("------以下是"+customer.getName()+"的個人信息------");
- out.println("ID: "+customer.getId());
- out.println("口令: "+customer.getPassword());
- out.println("E-Mail: "+customer.getEmail());
- out.println("電話: "+customer.getPhone());
- out.println("地址: "+customer.getAddress());
- String sex=customer.getSex()=='M'? "男":"女";
- out.println("性別: "+sex);
- String marriedStatus=customer.isMarried()? "已婚":"未婚";
- out.println("婚姻狀況: "+marriedStatus);
- out.println("生日: "+customer.getBirthday());
- out.println("注冊時間: "+customer.getRegisteredTime());
- out.println("自我介紹: "+customer.getDescription());
- }
- /** 把Customer對象的信息輸出到動態網頁 */
- private void printCustomerInWeb(ServletContext context,PrintWriter out,Customer customer)throws Exception{
- //保存照片
- byte[] buffer=customer.getImage();
- String path=context.getRealPath("/");
- FileOutputStream fout=new FileOutputStream(path+"photo_copy.gif");
- fout.write(buffer);
- fout.close();
- out.println("------以下是"+customer.getName()+"的個人信息------"+"<br>");
- out.println("ID: "+customer.getId()+"<br>");
- out.println("口令: "+customer.getPassword()+"<br>");
- out.println("E-Mail: "+customer.getEmail()+"<br>");
- out.println("電話: "+customer.getPhone()+"<br>");
- out.println("地址: "+customer.getAddress()+"<br>");
- String sex=customer.getSex()=='M'? "男":"女";
- out.println("性別: "+sex+"<br>");
- String marriedStatus=customer.isMarried()? "已婚":"未婚";
- out.println("婚姻狀況: "+marriedStatus+"<br>");
- out.println("生日: "+customer.getBirthday()+"<br>");
- out.println("注冊時間: "+customer.getRegisteredTime()+"<br>");
- out.println("自我介紹: "+customer.getDescription()+"<br>");
- out.println("<img src='photo_copy.gif' border=0><p>");
- }
- public void test(ServletContext context,PrintWriter out) throws Exception{
- Customer customer=new Customer();
- customer.setName("Tom");
- customer.setEmail("tom@yahoo.com");
- customer.setPassword("1234");
- customer.setPhone(55556666);
- customer.setAddress("Shanghai");
- customer.setSex('M');
- customer.setDescription("I am very honest.");
- //設置Customer對象的image屬性,它是字節數組,存放photo.gif文件中的二進制數據
- //photo.gif文件和BusinessService.class文件位于同一個目錄下
- InputStream in=this.getClass().getResourceAsStream("photo.gif");
- byte[] buffer = new byte[in.available()];
- in.read(buffer);
- customer.setImage(buffer);
- //設置Customer對象的birthday屬性,它是java.sql.Date類型
- customer.setBirthday(Date.valueOf("1980-05-06"));
- saveCustomer(customer);
- findAllCustomers(context,out);
- loadAndUpdateCustomer(customer.getId(),"Beijing");
- findAllCustomers(context,out);
- deleteCustomer(customer);
- }
- public static void main(String args[]) throws Exception {
- new BusinessService().test(null,new PrintWriter(System.out,true));
- sessionFactory.close();
- }
- }
#p#
saveCustomer()方法
該方法調用Session的save()方法,把Customer對象持久化到數據庫中。
- tx = session.beginTransaction();
- session.save(customer);
- tx.commit();
當運行session.save()方法時,Hibernate執行以下SQL語句:
- insert into CUSTOMERS (ID, NAME, EMAIL, PASSWORD, PHONE, ADDRESS, SEX,
- IS_MARRIED,DESCRIPTION, IMAGE, BIRTHDAY, REGISTERED_TIME)
- values(1,'Tom','tom@yahoo.com','1234',55556666,'Shanghai','M',0,'I am very honest.', ☺,'1980-05-06',null)
在test()方法中并沒有設置Customer對象的id屬性,Hibernate會根據映射文件的配置,采用increment標識符生成器自動以遞增的方式為OID賦值。在Customer.hbm.xml文件中相關的映射代碼如下:
- <id name="id" column="ID" type="long">
- <generator class="increment"/>
- </id>
findAllCustomers()方法
該方法通過Query接口查詢所有的Customer對象。
- tx = session.beginTransaction(); //開始一個事務
- Query query=session.createQuery("from Customer as c order by c.name asc");
- List customers=query.list();
- for (Iterator it = customers.iterator(); it.hasNext();) {
- printCustomer(context,out,(Customer) it.next());
- }
- tx.commit(); //提交事務
Session的createQuery()方法的參數“from Customer as c order by c.name asc”使用的是Hibernate查詢語言。運行Query.list()方法時, Hibernate執行以下SQL語句:
- select * from CUSTOMERS order by NAME asc;
loadAndUpdateCustomer ()方法
該方法調用Session的get()方法,加載Customer對象,然后再修改Customer對象的屬性。
- tx = session.beginTransaction();
- Customer c=(Customer)session.get(Customer.class,customer_id);
- c.setAddress(address); //修改內存中Customer對象的address屬性
- tx.commit();
以上代碼先調用Session的get()方法,它按照參數指定的OID從數據庫中檢索出匹配的Customer對象,Hibernate會執行以下SQL語句:
- select * from CUSTOMERS where ID=1;
loadAndUpdateCustomer()方法接著修改Customer對象的address屬性。那么,Hibernate會不會同步更新數據庫中相應的CUSTOMERS表的記錄呢?答案是肯定的。Hibernate采用臟檢查機制,按照內存中的Customer對象的狀態的變化,來同步更新數據庫中相關的數據,Hibernate會執行以下SQL語句:
- update CUSTOMERS set NAME="Tom",EMAIL="Tom@yahoo.com"…ADDRESS="Beijing"…
- where ID=1;
盡管只有Customer對象的address屬性發生了變化,但是Hibernate執行的update語句中會包含所有的字段。
deleteCustomer()方法
該方法調用Session的delete()方法,刪除特定的Customer對象:
- tx = session.beginTransaction();
- session.delete(customer);
- tx.commit();
運行session.delete()方法時,Hibernate根據Customer對象的OID,執行以下SQL delete語句:
- delete from CUSTOMERS where ID=1;
八、效果圖
原文鏈接:http://blog.csdn.net/yu422560654/article/details/7028748
【編輯推薦】