您的位置:首页 > 编程语言 > Java开发

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

2016-05-05 15:56 211 查看
如今,更多的人使用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
4000
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进行开发学习。

学习经验与大家分享,有不足之处,望能原谅!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate myeclipse