前言
使用面向对象的语言和关系型的数据库,开发起来很繁琐,费时。由于现在流行的数据库都不面向对象。Hibernate 是一个Java的ORM(Object/Relational Mapping)解决方案。
Hibernte不仅关心把Java对象对应到数据库的表中,而且提供了请求和检索的方法。简化了手工进行JDBC操作的流程。
如何使用Hibernate:
增加Hibernate的数据库配置文件:
首先,我们要增加:
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">org.h2.Driver</property> <property name="connection.url">jdbc:h2:db/h2manager</property> <property name="connection.username">tntxia</property> <property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.H2Dialect</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">create</property> <mapping resource="com/tntxia/test/hibernate/Entity/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
然后,我们再在com/tntxia/test/hibernate/Entity里面增加一个Event.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.tntxia.test.hibernate.Entity"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping>
Java的测试代码如下:
package com.tntxia.test.hibernate; import java.util.Date; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.tntxia.test.hibernate.Entity.Event; public class Test { /** * @param args */ public static void main(String[] args) { // A SessionFactory is set up once for an application SessionFactory sessionFactory = new Configuration() .configure() // configures settings from hibernate.cfg.xml .buildSessionFactory(); // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Event( "Our very first event!", new Date() ) ); session.save( new Event( "A follow up event", new Date() ) ); session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List<Event> result = session.createQuery( "from Event" ).list(); for ( Event event : (List<Event>) result ) { System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); } session.getTransaction().commit(); session.close(); } }
这个例子中,我们插入了两条记录。然后又把这两条记录查询出来。
当我们再写一个查询的语句时,
package com.tntxia.test.hibernate; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.tntxia.test.hibernate.Entity.Event; public class Test2 { /** * @param args */ public static void main(String[] args) { // A SessionFactory is set up once for an application SessionFactory sessionFactory = new Configuration() .configure() // configures settings from hibernate.cfg.xml .buildSessionFactory(); // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); List<Event> result = session.createQuery( "from Event" ).list(); for ( Event event : (List<Event>) result ) { System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); } session.getTransaction().commit(); session.close(); } }
发现刚刚插入的数据,无法查询出来。因为hbm2ddl.auto的属性为true,每次运行,数据库都会重建,所以应该把这个属性删掉。
相关推荐
Hibernate入门到精通,分享Hibernate的干货,带你入门,走向精通。
解压后包含hibernate入门所需jar (hibernate常用jar,mysql驱动,Junit4jar)
Hibernate入门案例,提供MyEclipse和Eclipse的项目源文件以及war文件
hibernate入门
Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门...
总共分5部分:1和2为hibernate入门,3为hibernate实体映射,4为hibernate复合主键,5为hibernate实体层设计.
hibernate入门小例子,采用mysql(sql-front)数据库
Hibernate入门教程.pdf
hibernate 入门详细操作步骤 包括hibernate包,实例实现文档····
这是一个关于hibernate入门到精通的书,希望可以帮到你
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持。然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西,看了这个希望能给你点启示!
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例
hibernate hibernate入门 hibernate学习 hibernate基础
hibernate入门教程 介绍如何引入Hibernate和搭建
Hibernate入门 - 基础配置,Hibernate入门 - 基础配置
NULL 博文链接:https://moshow.iteye.com/blog/1587008