Java中连接数据的Hibernate配置

Java中连接数据的Hibernate 配置


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">
<hibernate-configuration>
  <session-factory>


    <!-- properties -->
    <!--
      <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
      <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
      <property name="connection.url">jdbc:microsoft:sqlserver://192.168.0.118;databasename=saadmin</property>
      <property name="connection.username">sa</property>
      <property name="connection.password">saadmin</property>
    -->


    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://192.168.0.117:3306/mysqldb?useUnicode=true&amp;characterEncoding=gb2312</property>
    <property name="connection.username">root</property>
    <property name="connection.password">adminroot</property>
    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.idle_test_period">0</property>
    <property name="hibernate.c3p0.min_size">1</property>
    <property name="hibernate.c3p0.max_size">15</property>
    <property name="hibernate.c3p0.timeout">0</property>
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>


    <!-- mapping files -->
    <mapping resource="ormmapping/CrsProjectMember.hbm.xml"/>
    <mapping resource="ormmapping/Organization.hbm.xml"/>
    <mapping resource="ormmapping/CrsProjectVersion.hbm.xml"/>
    <mapping resource="ormmapping/CrsCrMember.hbm.xml"/>
    <mapping resource="ormmapping/CrsCcbMeeting.hbm.xml"/>
    <mapping resource="ormmapping/CrsProjectComponent.hbm.xml"/>


  </session-factory>
</hibernate-configuration>

 

 

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*

 

 

SessionFactory.java


 

package com.mitac.yin.hibernate;

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

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html}.
 */
public class SessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * NOTICE: Location should be on the classpath as Hibernate uses
     * #resourceAsStream style lookup for its configuration file. That
     * is place the config file in a Java package - the default location
     * is the default Java package.<br><br>
     * Examples: <br>
     * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
     * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
     */


    private static String CONFIG_FILE_LOCATION = "/MyBBS/WebRoot/WEB-INF/applicationContext.xml";

    /** Holds a single instance of Session */
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    /** The single instance of hibernate configuration */
    private static final Configuration cfg = new Configuration();

    /** The single instance of hibernate SessionFactory */
    private static org.hibernate.SessionFactory sessionFactory;

    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */


    public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                try {
                    cfg.configure(CONFIG_FILE_LOCATION);
                    sessionFactory = cfg.buildSessionFactory();
                } catch (Exception e) {
                    System.err
                            .println("%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
                }
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }

        return session;
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

    /**
     * Default constructor.
     */
    private SessionFactory() {
    }

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值