1、创建一个简单JavaBean类Eventpackagecom.yanleilei.model;importjava.util.Date;publicclassEvent{privateintid;privateStringtitle;privateDatedate;publicintgetId(){returnid;}publicStringgetTitle(){returntitle;}publicDategetDate(){returndate;}publicvoidsetId(intid){this.id=id;}publicvoidsetTitle(Stringtitle){this.title=title;}publicvoidsetDate(Datedate){this.date=date;}}2、JavaBean的映射文件(命名为Event.hbm.xml)?xmlversion=1.0?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.yanleilei.modelclassname=Eventtable=eventidname=idcolumn=idgeneratorclass=increment//idpropertyname=title/propertyname=datetype=timestamp//class/hibernate-mapping3、Hibernate配置文件(命名为hibernate.cfg.xml)?xmlversion='1.0'encoding='utf-8'?!DOCTYPEhibernate-configurationPUBLIC-//Hibernate/HibernateConfigurationDTD3.0//EN!--Databaseconnectionsettings--propertyname=connection.driver_classcom.mysql.jdbc.Driver/propertypropertyname=connection.urljdbc:mysql://localhost:3306/hibernate/propertypropertyname=connection.usernameroot/propertypropertyname=connection.passwordroot/property!--JDBCconnectionpool(usethebuilt-in)--!--propertyname=connection.pool_size1/property--!--SQLdialect--propertyname=dialectorg.hibernate.dialect.MySQL5Dialect/property!--EnableHibernate'sautomaticsessioncontextmanagement--!--propertyname=current_session_context_classthread/property--!--Disablethesecond-levelcache--propertyname=cache.provider_classorg.hibernate.cache.internal.NoCacheProvider/property!--EchoallexecutedSQLtostdout--propertyname=show_sqltrue/property!--Dropandre-createthedatabaseschemaonstartup--!--propertyname=hbm2ddl.autoupdate/property--mappingresource=com/yanleilei/model/Event.hbm.xml//session-factory/hibernate-configuration4、创建一个SessionFactory的辅助类packagecom.yanleilei.util;importorg.hibernate.SessionFactory;importorg.hibernate.boot.registry.StandardServiceRegistryBuilder;importorg.hibernate.cfg.Configuration;importorg.hibernate.service.ServiceRegistry;publicclassHibernateUtil{privatestaticSessionFactorysessionFactory=buildSessionFactory();privatestaticSessionFactorybuildSessionFactory(){Configurationcfg=newConfiguration().configure();ServiceRegistryserviceRegistry=newStandardServiceRegistryBuilder().build();SessionFactorysessionFactory=cfg.buildSessionFactory(serviceRegistry);returnsessionFactory;}publicstaticSessionFactorygetSessionFactory(){returnsessionFactory;}}5、测试类packagecom.yanleilei.test;importjava.util.Date;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importcom.yanleilei.model.Event;importcom.yanleilei.util.HibernateUtil;publicclassTest{publicstaticvoidmain(String[]args){test();}publicstaticvoidtest(){SessionFactorysessionFactory=HibernateUtil.getSessionFactory();Sessionsession=sessionFactory.openSession();session.beginTransaction();Eventevent=newEvent();event.setTitle(TestCase);event.setDate(newDate());session.save(event);session.getTransaction().commit();}}6、创建数据库表eventcreatetableevent(idintprimarykeyauto_increment,#注意这里是auto_increment,不能写作autoincrementtitlevarchar(20),datetimestamp);附图1、执行结果附图2、本例程目录结构备注:本测试例程创建的是一个普通Java项目,有些信息读取不成功,所以另加一个hibernate.properties配置文件hibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://localhost:3306/hibernatehibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.connection.user=roothibernate.connection.password=roothibernate.show_sql=true