hibernate4.2.1_HelloWorld学习例子
2013-05-12 23:47
302 查看
这次学习hibernate用的版本是 hibernate-release-4.2.1.Final。由于刚开始学,新旧版本间的差异比自己想像中的多,导致弄了整整两天才实现第一个CLASS例子,过程滋味不言而喻。曾经也从网上找过好多例子,但多是问题帖子,完整的例子少得可怜,对新版本的例子更是难以找出,现在把我这两天学到的第一个例子完整贴出来分享给大家,希望对新学的有点帮助。
工程目录完整结构截图:
只要把hibernate-release-4.2.1.Final下载解压,然后复制lib\required目录下全部JAR包就可以用了,不需要额外下载别的JAR包了,当然了,为了连接数据库MYSQL,还需要一个数据库驱动JAR包。
1、建立一个普通的JAVA工程,名字随便取一个,下面我会建立两个实体类和对应的两张表,分别演示用传统映射配置文件和用注解实现映射的实现过程。
2、建立一个学生和老师类,并分别建立对应的两张表,建表这里就不说了,自己可以完成,需要注意的是建主键。
Student类
3、建立两个类的对应的测试类;
StudentTest.java
TeacherTest.java
4、hibernate.cfg.xml和Student.hbm.xml
Student.hbm.xml一般放在对应的实体类所在目录下,hibernate.cfg.xml一般放在工程根目录下
hibernate.cfg.xml
Student.hbm.xml
好了,直接运行上面的应该是没有问题的了,我用的是myeclipse10.5的开发平台。建议大家先了解一些基本的知识再来看上面这些代码,每行代码或配置的具体意思我在这就不解释了,需要大家去查找一下网上别的教程,这里提供的是我自己测试成功的例子。另外,大家在弄配置文件的时候一定要多小心,本人就因某些配置格式,浪费了一天多时间。
工程目录完整结构截图:
只要把hibernate-release-4.2.1.Final下载解压,然后复制lib\required目录下全部JAR包就可以用了,不需要额外下载别的JAR包了,当然了,为了连接数据库MYSQL,还需要一个数据库驱动JAR包。
1、建立一个普通的JAVA工程,名字随便取一个,下面我会建立两个实体类和对应的两张表,分别演示用传统映射配置文件和用注解实现映射的实现过程。
2、建立一个学生和老师类,并分别建立对应的两张表,建表这里就不说了,自己可以完成,需要注意的是建主键。
Student类
package com.luhy.hibernate.model; public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }Ttudent类
package com.luhy.hibernate.model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Teacher { private int id; private String name; private String title; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
3、建立两个类的对应的测试类;
StudentTest.java
package com.luhy.hibernate.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.luhy.hibernate.model.Student; public class StudentTest { public static void main(String[] args) { Student st = new Student(); st.setId(6); st.setName("好"); st.setAge(20); Configuration cfg = new Configuration().configure(); ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sf = cfg.buildSessionFactory(serviceRegistry); Session session = sf.openSession(); session.beginTransaction(); //事务开启 session.save(st); //保存对象 session.getTransaction().commit(); session.close(); sf.close(); } }
TeacherTest.java
package com.luhy.hibernate.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.luhy.hibernate.model.Teacher; public class TeacherTest { public static void main(String[] args) { Teacher t = new Teacher(); t.setId(2); t.setName("kk"); t.setTitle("oolk"); Configuration cfg = new Configuration().configure(); ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sf = cfg.buildSessionFactory(serviceRegistry); Session session = sf.openSession(); session.beginTransaction(); //事务开启 session.save(t); //保存对象 session.getTransaction().commit(); session.close(); sf.close(); } }
4、hibernate.cfg.xml和Student.hbm.xml
Student.hbm.xml一般放在对应的实体类所在目录下,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"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">lu5896848</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</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.internal.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/luhy/hibernate/model/Student.hbm.xml" /> <mapping class="com.luhy.hibernate.model.Teacher"/> </session-factory> </hibernate-configuration>
Student.hbm.xml
<?xml version="1.0" encoding='utf-8'?> <!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.luhy.hibernate.model"> <class name="Student" table="student"> <id name="id" column="id" /> <property name="name" column="name" /> <property name="age" column="age" /> </class> </hibernate-mapping>
好了,直接运行上面的应该是没有问题的了,我用的是myeclipse10.5的开发平台。建议大家先了解一些基本的知识再来看上面这些代码,每行代码或配置的具体意思我在这就不解释了,需要大家去查找一下网上别的教程,这里提供的是我自己测试成功的例子。另外,大家在弄配置文件的时候一定要多小心,本人就因某些配置格式,浪费了一天多时间。
相关文章推荐
- 关于ant的build的学习 hibernate中的一个例子 HelloWorld的
- hibernate学习:HelloWorld_Annotation
- 学习Hibernate——简单的一个例子
- Hibernate 4.1.1的第一个例子HelloWorld
- 小汪Hibernate 学习之一 Hibernate 小例子
- 菜鸟学习Hibernate——简单的一个例子
- Hibernate 4.1.1的第一个例子HelloWorld
- hibernate_初步学习,完整例子
- 分享下我学习Thrift的入门例子helloworld,客户端用php,服务端用python:
- 菜鸟学习Hibernate——简单的一个例子
- 基于annotation注解的Spring3 MVC之一 HelloWorld 例子学习笔记
- Hibernate学习5 事务管理简单例子购买股票
- portlet初学习及HelloWorld例子
- Hibernate学习(一)-HelloWorld
- 菜鸟学习Hibernate——简单的一个例子
- Hibernate的一个多表例子.仅供今后学习复习参考.还请指教.
- Hibernate3 - HelloWorld(hibernate-mapping和Annotation) 【从零开始学习Hibernate_01】
- Hibernate的学习一__简单的小例子
- 分享下我学习Thrift的入门例子helloworld,客户端用php,服务端用python
- Hibernate学习--Hibernate的第一个小例子