1, 在web 项目的lib 中导入最基本 jar包:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-3.1.13-bin.jar
slf4j-api-1.6.1.jar
slf4j-nop-1.6.2.jar
2. 在src目录下建立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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">liuhl</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/hibernate/model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.在数据库中建立student表格,然后在com.hibernate.model包下建立对应的Student类;
接着在com.hibernate.model包下建立Student.hbm.xml文件;
Student.hbm.xml文件的基本配置如:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
<class name="Student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
4,最后写一个测试类,如:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Student;
public class StudentTest
{
public static void main(String[] args)
{
Student s = new Student();
s.setId(1);
s.setName("张三");
s.setAge(21);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
这就是hibernate的helloworld
分享到:
相关推荐
springMVC+hibernate搭建的一个demo帮助不会配环境的新人:数据库用的是mysql
简单struts+spring+hibernate搭建,配置,适合初学者
hibernate搭建用到的jar包hibernate搭建用到的jar包hibernate搭建用到的jar包hibernate搭建用到的jar包
String+Spring+Hibernate搭建的框架,可供初学者学习。
hibernate搭建基础环境,配置hibernate操作数据库,并配对与之对应的SQL语句。
extjs4 + struct+ spring + hibernate 搭建后台。
免费的Spring++JPA+Hibernate搭建,手把手教你如何搭建SpringMVC+Hibernate框架
我自己总结的超详细hibernate项目环境搭建文档
cxf spring Hibernate 搭建webservice,在myEclipse中成功运行。数据库为mysql,sql文件在项目里。所需jar包在图片中,根据图片自己添加相应jar包。
Struts2+Spring+Hibernate搭建全解 Struts2+Spring+Hibernate是J2EE的最新流行框架。本篇是我搭建这个框架的经验总结,有很多人搭建这个框架总会遇到
Struts2+Spring+Hibernate搭建登录实例完整步骤
mina+spring+hibernate搭建所需jar包
跟博客上视频配套的源码,hibernate 4.19
Java web 项目 我使用eclipse写的 导出为一个文件夹 使用了Spring Hibernate 并没有Struts 带分页的公共方法和标签 带需要的Jar包 初始写项目搭建框架的人员可以用来参考或使用
Struts1+Strutst2+Srping+Hibernate 搭建框架的jar包
本系统针对教育工作的具体需求,用struts + spring + hibernate搭建的框架为设计平台,以B/S(Browser/Server)模式开发与设计题库及试卷管理模块
一个完整的Spring+Hibernate+FreeMarker环境的搭建过程。 包括了国际化配置、缓存配置、事物配置等功能
JSF+Hibernate开发WEB应用比用SSH开发WEB,更简单,更易上手。本源码通过用户登录的实例,展示了采用JSF和HIBERNATE技术,搭建WEB应用系统的方法。数据库采用SQL2008,实例包含了创建user的SQL脚本。
myeclipse搭建hibernate文档