Hibernate在MyEclipse中的部署,连接MySQL数据库
来源:互联网 发布:部落冲突咏王升级数据 编辑:程序博客网 时间:2024/06/05 08:16
如今,更多的人使用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进行开发学习。
学习经验与大家分享,有不足之处,望能原谅!
- Hibernate在MyEclipse中的部署,连接MySQL数据库
- 在myeclipse中连接到mysql中的数据库
- 在MyEclipse中连接MySQL数据库
- MyEclipse连接数据库MySQL
- Myeclipse连接MySQL数据库
- MyEclipse 连接MySQL数据库
- Myeclipse连接mysql数据库
- myeclipse连接mysql数据库
- Myeclipse连接Mysql数据库
- myeclipse连接mysql数据库
- MyEclipse连接MySQL数据库
- Myeclipse连接mysql数据库
- Myeclipse连接 数据库MySql
- myeclipse连接mysql数据库
- MyEclipse连接MySQL数据库
- Hibernate连接mysql数据库
- Hibernate连接mysql数据库
- hibernate连接mysql数据库
- 禁用浏览中的Tab键
- 获得本地IP地址
- 设计模式--单例模式
- Android中什么是ANR?产生的原因,解决办法
- 因为锁等待导致的慢查询不会改变状态变量slow_queries值
- Hibernate在MyEclipse中的部署,连接MySQL数据库
- 在Firefox中通过JavaScript复制到剪贴板(Copy to Clipboard)
- 福州大学第十三届程序设计竞赛_重现总结
- 关于线程的停止、挂起、退出(修改)
- Spring.NET企业架构实践之 Nhibernate + WCF + ASP.NET MVC + NVelocity 对PetShop4.0重构(三)——持久层
- 剑指offer:二叉搜索树的第k个结点
- 有实力的人才能谈梦想
- Android基础 | 控件基础 | Listview之BaseAdapter②
- IF标志位与中断的关系