目录结构
实现步骤
1. 安装MySQL数据库并添加数据库hibernate4;
2. 新建工程org.rabbitx.hibernate4;
3. 在此工程下添加目录lib并添加需要的 jar 包;
hibernate包(hibernate-release-4.2.4.Final\lib\required目录下所有包):
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
MySQL驱动包:
mysql-connector-java-5.1.30-bin.jar
日志包(可选):
log4j-1.2.17.jar
4. 手动或使用hibernate tool插件添加hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置连接数据库的基本信息 --> <!-- 1. hibernate.connection.* 此处可以简写为connection.*,但与spring集成之后不能简写; 2. jdbc:mysql//localhost:3306/hibernate4可以简写为jdbc:mysql///hibernate4,因为默认值是localhost:3306; --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.1.105:3306/hibernate4</property> <!-- 配置hibernate基本信息 --> <!-- 配置数据库方言 --> <!-- 数据库方言值可以在hibernate-release-4.2.4.Final\project\etc\hibernate.properties文件找查询 --> <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="org/rabbitx/hibernate4/helloworld/News.hbm.xml"/> </session-factory> </hibernate-configuration>
5. 添加持久化类
package org.rabbitx.hibernate4.helloworld; import java.util.Date; public class News { private Integer id; private String message; private Date date; public News() { super(); } public News(String message,Date date) { this.message = message; this.date = date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "News [id=" + id + ", message=" + message + ", date=" + date + "]"; } }
6. 手动或使用hibernate tool插件添加对象-关系映射文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-8-19 23:17:56 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="org.rabbitx.hibernate4.helloworld.News" table="TBL_NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="message" type="java.lang.String"> <column name="MESSAGE" /> </property> <property name="date" type="java.util.Date"> <column name="DATE" /> </property> </class> </hibernate-mapping>
7. 通过Hibernate API 访问数据库
package org.rabbitx.hibernate4.helloworld; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class Main { public static void main(String[] args) { //1. 创建 Configuration对象:对应 hibernate的基本配置信息和 对象关系映射信息 Configuration cfg = new Configuration().configure(); //2. 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象.hibernate 的任何配置和服务都需要在该对象中注册后才能有效. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); //3. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); //4. 创建一个 Session 对象 Session session = sessionFactory.openSession(); //5. 开启事务 Transaction transaction = session.beginTransaction(); //6. 执行保存操作 News news = new News("Tianjin explosion",new Date()); session.save(news); //7. 提交事务 transaction.commit(); //8. 关闭Session session.close(); //9. 关闭SessionFactory sessionFactory.close(); } }
8. 运行后查看数据库
相关推荐
Hibernate 4——Hello World
hibernate的helloworld版。下载下来可以直接运行。
Hibernate教程01_Hibernate的HelloWorld及基本配置 http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
一个简单的hibernate helloworld 下载下来就能用
Hibernate 4.0版helloworld,项目需要Maven管理库依赖,项目包含Oracle建表语句
用Spring MVC+hibernate 写的一个HelloWorld小实例,加入了一个简单验证。
hibernate-helloworld-native
NULL 博文链接:https://huangminwen.iteye.com/blog/1312068
版本差异太难搞了,找半天才找到错误,主要是包冲突 NoSuchMethodError: org.jboss....NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory org.hibernate.cache.spi.RegionFactory 记录下来,以备不时
如果你没学过hibernate,但是又想看一下它的最简单形式。这个应该适合你。
Hibernate01-简介和HelloWorld 例子代码,实现了简单新增,测试类需要引进junit4
hibernate 使用及与oracle连接的方式
这是hibernate 的超级简单的例子,只有一个持久化对象和一个辅助类,还有一个测试类,对于初学者很有参考价值
主要介绍了Hibernate的Annotation版Hello world实现方法,详细分析了Annotation的具体使用步骤与Hello world实现方法,需要的朋友可以参考下
Hibernate_HelloWorld 使用Netbeans和SQL Server的HelloWorld Hibernate
ssh_helloworld(初学参考),初学者学习的例子,供学习参考
NULL 博文链接:https://chentian114.iteye.com/blog/2295338
本例子通過學習“002_尚学堂马士兵_Java视频教程_Hibernate3.3.2_HelloWorld.avi”教程實踐的小例子。 整理出来的精简JAR包,共八个。分别: antlr-2.7.6.jar、commons-collections-3.1.jar、dom4j-1.6.1.jar、...
spring+hibernate的一个helloworld,可能还有些小地方需要修改才可以使用
JPA的HelloWorld实现,使用实现JPA规范的Hibernate持久化框架