Hibernate 框架的搭建

 

一、在开发环境中导入相关的hibernate包和连接池包和数据驱动包

 mysql-connector-java-5.1.6.jar

antlr-2.7.6.jar

c3p0-0.9.1.jar

commons-collections-3.1.jar

dom4j-1.6.1.jar

ejb3-persistence.jar

hibernate3.jar

hibernate-annotations.jar

hibernate-commons-annotations.jar

javassist-3.9.0.GA.jar

jta-1.1.jar

proxool-0.8.3.jar

slf4j-api-1.5.8.jar

slf4j-nop-1.7.5.jar

 

二、在根路径下配置基本配置文件hibernate.cfg.xml

 

<?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">

<!-- Generated by MyEclipse HibernateTools. -->

<hibernate-configuration>

 

         <session-factory>

                   <!--Oracle配置 -->

                   <propertyname="dialect">

                            org.hibernate.dialect.Oracle9iDialect

                   </property>

                   <propertyname="connection.url">

                            jdbc:oracle:thin:@192.168.7.***:1521:DatabaseName

                   </property>

                   <propertyname="connection.username">OracleName</property>

                   <propertyname="connection.password">OraclePassword</property>

                   <propertyname="connection.driver_class">

                            oracle.jdbc.driver.OracleDriver

                   </property>

                   <!--mysql 数据配置-->

                   <!--<propertyname="dialect">

                            org.hibernate.dialect.MySQLDialect

                   </property>

                   <propertyname="connection.url">

                            jdbc:mysql://127.0.0.1:3306/DatabaseName

                   </property>

                   <propertyname="connection.username">mysqlName</property>

                   <propertyname="connection.password">mysqlPassword</property>

                   <propertyname="connection.driver_class">

                            com.mysql.jdbc.Driver

                   </property>-->

                   <!--在后台打印sql语句 -->

                   <propertyname="show_sql">true</property>

                   <!--hibernate自动建表 -->

                   <propertyname="hibernate.hbm2ddl.auto">update</property>

                   <!--使用c3p0连接池 -->

                    <propertyname="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

                   <!--初始化时获取10个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3-->

                   <propertyname="c3p0.initialPoolSize">10</property>

                   <!--连接池中JDBC连接的最大数量。Hibernate默认为100 -->

                   <propertyname="hibernate.c3p0.max_size">100</property>

                   <!--连接池中JDBC连接的最小数量。Hibernate默认为1 -->

                   <propertyname="hibernate.c3p0.min_size">5</property>

                   <!--何时从连接池中移除一个空闲的连接(以秒为单位)时。Hibernate默认为0,永不过期 -->

                   <propertyname="hibernate.c3p0.timeout">300</property>

                   <!--被缓存的预编译语句数量。用来提高性能。Hibernate默认为0,缓存不可用-->

                   <propertyname="hibernate.c3p0.max_statements">1000</property>

                   <!--若数据库中连接不足时,一次向数据库服务器申请多少个连接-->

                   <propertyname="hibernate.c3p0.acquire_increment">5</property>

                   <!--hibernate将session 的线程配置 -->

                   <propertyname="current_session_context_class">thread</property>

                   <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->

       <propertyname="autoCommitOnClose">false</property>

                   <!--两种指定映射文件--> 

                   <!--注解方式-->

                   <!--<mappingclass="com.eakom.*"/> -->

                   <!--xxx.hbm.xml配置文件方式 -->

                   <!--<mappingresource="org/mxg/UserInfo.hbm.xml"> -->

         </session-factory>

 

</hibernate-configuration>

 

 

三、在跟项目的新建工具类

 

package com.eakom.utils;

import java.io.Serializable; 

 

import org.hibernate.Session; 

import org.hibernate.SessionFactory; 

import org.hibernate.Transaction; 

import org.hibernate.cfg.AnnotationConfiguration; 

 

public classHibernateUtils { 

   privatestaticSessionFactory sessionFactory

 

   privateHibernateUtils() { 

   } 

 

   static

        /**

         * configure()参数为空默认查找classes目录下hibernate.cfg.xml

         * configure("文件名")也有重载方法,参数名为配置文件名

         */ 

        sessionFactory = newAnnotationConfiguration().configure().buildSessionFactory(); 

   } 

 

   publicstaticSessionFactory getSessionFactory() { 

        return sessionFactory

   } 

 

   /**

     * 如果想使用sessionFactory.getCurrentSession()来获得Session时,需要在配置文件中添加一句:

     * <!-- 本地事务防止使用sessionFactory.getCurrentSession()时报错:"org.hibernate.HibernateException: NoCurrentSessionContext configured!"--> 

     * <propertyname="hibernate.current_session_context_class">thread</property>

     * @return

     */ 

   publicstaticSession getCurrentSession() { 

        return sessionFactory.getCurrentSession(); 

   } 

   publicstaticSession getNewSession() { 

        return sessionFactory.openSession(); 

   } 

   publicstaticvoidadd(Object entity) { 

        Session s = null

        Transaction tx = null

        try

            s = HibernateUtils.getNewSession(); 

            tx = s.beginTransaction(); 

            s.save(entity); 

            tx.commit(); 

        } finally

            if (s != null

                s.close(); 

        } 

   } 

 

   publicstaticvoidupdate(Object entity) { 

        Session s = null

        Transaction tx = null

        try

            s = HibernateUtils.getNewSession(); 

            tx = s.beginTransaction(); 

            s.update(entity); 

            tx.commit(); 

        } finally

            if (s != null

                s.close(); 

        } 

   } 

 

   publicstaticvoiddelete(Object entity) { 

        Session s = null

        Transaction tx = null

        try

            s = HibernateUtils.getNewSession(); 

            tx = s.beginTransaction(); 

            s.delete(entity); 

            tx.commit(); 

        } finally

            if (s != null

                s.close(); 

        } 

   } 

 

   publicstaticObject get(Class clazz, Serializable id) { 

        Session s = null

        try

            s = HibernateUtils.getNewSession(); 

            Object obj = s.get(clazz, id); 

            return obj; 

        } finally

           if (s != null

                s.close(); 

        } 

   } 

四、在项目中使用hibernate连接

 

Session session = HibernateUtils.getCurrentSession();

Transaction tx = session.beginTransaction();

String sql=”select * from tablename”;

SQLQuery query=session.createSQLQuery(sql);

Query.list();

tx.commit();

五、创建完毕

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值