Hibernate在MyEclipse中的部署,连接MySQL数据库

    如今,更多的人使用MyEclipse而不再使用eclipse,MyEclipse比eclipse的方便之处我就不说了,但MyEclipse不是免费的,我也是使用破解版的。
    在MyEclipse中如何搭建hibernate环境呢?今天学习一点点想与大家分享:

1. 在MyEclipse中创建web项目

新建web项目

这里写图片描述

输入项目名

这里写图片描述

下一步:

这里写图片描述

点击“finish”,创建web项目完成。

2.创建数据库资源管理器

点击“Open Perspective”

这里写图片描述

找到“MyEclipse Database Explorer”

这里写图片描述

新建数据库驱动连接

这里写图片描述

Driver template : 根据自己要使用的数据库选择;我这里用的是MySQL数据库,因此以MySQL为例 。
Driver name : 名字自己随便写。
Connection URL : 连接数据库的URL,不同数据库连接方式不同。
User name : 数据库用户名。
Password : 数据库密码。
Driver JARs : 添加连接数据库驱动jar包。
保存密码,点击“next”。

这里写图片描述

添加已创建要连接的数据库

这里写图片描述

在已建项目中安装hibernate的组件

这里写图片描述

根据自己需要选择hibernate的版本,我这里选择4.1版本

这里写图片描述

创建 Hibernate Configuration 文件与 SessionFactory 类

这里写图片描述

选择MySQL,点击“next”

这里写图片描述

点击“finish”完成hibernate环境搭建。

这里写图片描述

3.完成后,将自动生成 hibernate.cfg.xml 文件与 HibernateSessionFactory.java类

hibernate.cfg.xml 配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">MySQL</property>

    </session-factory>

</hibernate-configuration>

HibernateSessionFactory.java类

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * 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 HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static org.hibernate.SessionFactory sessionFactory;

    private static Configuration configuration = new Configuration();
    private static ServiceRegistry serviceRegistry; 

    static {
        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }
    private HibernateSessionFactory() {
    }

    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }

        return session;
    }

    /**
     *  Rebuild hibernate session factory
     *
     */
    public static void rebuildSessionFactory() {
        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }

    /**
     *  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();
        }
    }

    /**
     *  return session factory
     *
     */
    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    /**
     *  return hibernate configuration
     *
     */
    public static Configuration getConfiguration() {
        return configuration;
    }

}

注意:自动生成 hibernate.cfg.xml 文件 HibernateSessionFactory.java类同样可以自己手动写。自动生成的 hibernate.cfg.xml 配置文件不完整,这里我附上一段我自己写的 hibernate.cfg.xml 配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>

        <!-- 配置连接数据库的基本信息 -->
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>

        <!-- 配置hibernate的基本信息 -->
        <!-- hibernate 所使用的数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- 执行操作时是否在控制台打印SQL -->
        <property name="show_sql">true</property>

        <!-- 是否对 SQL 进行格式化  -->
        <property name="format_sql">true</property>

        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 指定关联 .hbm.xml -->
        <mapping resource="com/wang/hibernate/model/News.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

完成hibernate环境搭建就可以对hibernate进行开发学习。

学习经验与大家分享,有不足之处,望能原谅!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值