【SSH三大架構】Hibernate基礎第二篇:編寫HibernateUtil工具類最佳化效能

來源:互聯網
上載者:User

標籤:hibernate   架構   session   

相對於上一篇中的代碼編寫HibernateUtil類以提高程式的運行速度

首先,仍然要寫一個javabean(User.java):

package cn.itcast.hibernate.domain;import java.util.Date;public class User {private int id;private String name;private Date birthday;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}


然後,在當前包下添加一個對應檔: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="cn.itcast.hibernate.domain"><class name="User" table="tb_User"><id name="id"><generator class="native"/> </id><property name="name" /><property name="birthday" /></class></hibernate-mapping>

之後,我們要配置Hibernate.cfg.cml,此檔案直接放在src目錄下邊:

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/test</property><property name="connection.username">sa</property><property name="connection.password">123456</property><property name="hbm2ddl.auto">create</property><property name="show_sql">true</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" /></session-factory></hibernate-configuration>

接下來,我們就要編寫HibernateUtil.java類了,這個類主要是為了獲得Session。

因為,在我們每次在代碼中獲得Session是非常耗時的,我們希望它僅僅做一次,因此我們要做一個工具類去初始化Hibernate。而且因為工具類一般不希望能夠被繼承,使得使用者能夠改變工具類的東西,所以一般我們會採用final關鍵字。

package cn.itcast.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public final class HibernateUtil {private static SessionFactory sessionFactory;private HibernateUtil(){}static{Configuration cfg = new Configuration();cfg.configure();sessionFactory = cfg.buildSessionFactory();}public static SessionFactory getSessionFactory(){return sessionFactory;}public static Session getSession(){return sessionFactory.openSession();}}

然後,我們編寫一個簡單的測試類別,添加一條資料Add.java:

package cn.itcast.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.Transaction;import cn.itcast.hibernate.domain.User;public class Add {public static void main(String[] args){Session s = HibernateUtil.getSession(); //這裡直接調用HibernateUtil工具類中的getSession()方法獲得SessionTransaction tx = s.beginTransaction(); //開啟事務User user = new User();user.setName("zhangsan");user.setBirthday(new Date());s.save(user); //儲存對象tx.commit();//提交事務s.close();//關閉Session}}
如此,一個簡單的擷取Session來提高程式的效能的HibernateUtil類就編寫完畢了。

【SSH三大架構】Hibernate基礎第二篇:編寫HibernateUtil工具類最佳化效能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.