Hibernate 最简单例子
郭 强 guoqiang-vip@hotmail.com
一 开发环境
Windows XP Pack 2
MyEclipse 5.5.1 GA
jdk1.5.0_07
Tomcat 5.0
MySQL Server 5.0
MySQL Tools for 5.0
二 开发代码
1 创建数据库,sql语句如下:
DROP TABLE IF EXISTS `test`.`user`;
CREATE TABLE `test`.`user` (
`id` int(10) unsigned NOT NULL auto_increment,
`username` varchar(64) NOT NULL,
`password` varchar(64) NOT NULL,
`first_name` varchar(128) NOT NULL,
`last_name` varchar(128) NOT NULL,
`date_created` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2 程序代码:
(1)hibernate.cfg.xml
package com.guoqiang.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
*ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe
*currentthreadofexecution. FollowstheThreadLocalSession
*pattern,see{@link }.
*/
publicclass HibernateSessionFactory {
/**
*Locationofhibernate.cfg.xmlfile.
*LocationshouldbeontheclasspathasHibernateuses
*#resourceAsStreamstylelookupforitsconfigurationfile.
*Thedefaultclasspathlocationofthehibernateconfigfileis