Java_Web三大框架之Hibernate配置文件(二)


下面介紹一下編寫Hibernate的配置文件,使用Hibernate操作數據庫。

開始部署:下載需要的jar包
              下載Hibernate
          Hibernate 的官方主頁是www.hibernate.org
          推薦下載hibernate-distribution-3.3.2.GA-dist.zip
        Hibernate包目錄結構
 
            部署jar包
              hibernate3.jar
             required 目錄下的jar 包

              Oracle 數據庫驅動jar包

第一步:創建實體類和實體映射文件

public class User {
    
    private int id;
    private String username;
    private String password;
}
省略get和set方法
配置映射文件(*.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.msit.hibernate.entity">
<!--User實體,t_user數據庫表面名-->
    <class name="User" table="t_user">
                 <!--自增id-->
        <id name="id">
            <generator class="increment"/>
        </id>
        <property name="username" />
        <property name="password" />
    </class>

</hibernate-mapping>
第二步:向hibernate.cfg.xml文件中配置映射文件
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory name="foo">
        <!-- 數據庫方言 -->
        <property name="dialect">
            org.hibernate.dialect.OracleDialect
        </property>
        <!-- 連接數據庫Url -->
        <property name="hibernate.connection.url">
            jdbc:oracle:thin:@localhost:1521:orcl
        </property>
        <!-- 連接驅動 -->
        <property name="hibernate.connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <!-- 用戶名 -->
        <property name="hibernate.connection.username">epet</property>
        <!-- 密碼 -->
        <property name="hibernate.connection.password">123456</property>
        
        <!-- 自動創建數據庫表格 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 配置映射信息 -->
        <mapping resource="com/msit/hibernate/entity/User.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>

注:
<session-factory>
    <!--省略其他配置-->
    <!--注意配置文件名必須包含其相對於classpath 的全路徑-->
    <mapping resource="cn/jbit/houserent/entity/User.hbm.xml" />
</session-factory>

第三步:抽出HibernateUtil接口

package com.msit.hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    
    private HibernateUtil(){
        
    };
    
    public static SessionFactory SessionFactory = null;
    
    static{
        //hibernate初始化
        Configuration cf = new Configuration();
        cf.configure();
        SessionFactory = cf.buildSessionFactory();//DriverManager.getconnection()
        //Session session = SessionFactory.openSession();//相當於得到Connection對象
    }
    
    public static Session getSession(){
        
        return SessionFactory.openSession();
    }
    
    public static void closeSession(Session session){
        if(session!=null){
            session.clear();
        }
    }

}

第四步:編寫測試類:

package com.msit.hibernate.test;

import java.sql.DriverManager;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.msit.hibernate.HibernateUtil.HibernateUtil;
import com.msit.hibernate.entity.User;

public class HibernateTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        
        
        //創建用戶
        User user = new User();
        user.setId(1);
        user.setUsername("master123456");
        user.setPassword("123");
        
        Session session = HibernateUtil.getSession();
        
        //進行事務處理
        Transaction Transaction = session.beginTransaction();
        
        try {
            
            //對數據做保存至數據庫
            session.update(user);
            
            //提交事務
            Transaction.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //如果出現異常則進行事務回滾
            Transaction.rollback();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM