首先,我们要创建一个数据库,并生成一个表,来供hibernate访问。在这里我们采用MySQL作为DBMS。我们的数据库取名为hbtest,生成一个名叫EVENTS的表。生成数据库的操作比较简单,我就不介绍了,生成表的sql语句为:
- create table EVENTS (
- EVENT_ID int(6) AUTO_INCREMENT,
- EVENT_DATE date,
- TITLE varchar(20),
- primary key (EVENT_ID));
- package org.hibernate.tutorial.domain;
- //上面的这个包路径比较长,因为原例子中就是这样,我没有修改
- import java.util.Date;
- public class Event {
- private Long id;
- private String title;
- private Date date;
- public Event() {}
- public Long getId() {
- return id;
- }
- private void setId(Long id) {
- //这里要注意,这个set方法是private的,
- //目的是不让普通的程序员使用它,
- //但是hibernate框架还是可以通过反射机制访问私有属性id
- this.id = id;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.hibernate.tutorial.domain">
- <class name="Event" table="EVENTS">
- <id name="id" column="EVENT_ID">
- <generator class="native" />
- </id>
- <property name="date" type="timestamp" column="EVENT_DATE" />
- <property name="title" />
- </class>
- </hibernate-mapping>
接下来,我们在src文件夹中创建一个名叫hibernate.cfg.xml的文档,来对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>
- <!-- 数据库连接设置,根据具体情况来,特别是用户名和密码 -->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/hbtest</property>
- <property name="connection.username">root</property>
- <property name="connection.password">123456</property>
- <!-- JDBC连接池(内置的) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL语句的方言 -->
- <property name="dialect">org.hibernate.dialect.HSQLDialect</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">create</property>
- <mapping resource="mapping/Event.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
接下来我们写一个工具类。因为hibernate在执行的时候,总是要用到SessionFactory这个东西,而得到这个东西的方法稍微有点复杂,因此我们写一个工具类,唯一的功能就是得到SessionFactory,这样使用起来就很方便了,代码如下:
- package util;
- import org.hibernate.*;
- import org.hibernate.cfg.*;
- public class HibernateUtil {
- private static final SessionFactory sessionFactory;
- static {
- try {
- // Create the SessionFactory from hibernate.cfg.xml
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch (Throwable ex) {
- // Make sure you log the exception, as it might be swallowed
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- }
- package events;
- import org.hibernate.Session;
- import java.util.Date;
- import util.HibernateUtil;
- import org.hibernate.tutorial.domain.Event;
- public class EventManager {
- public static void main(String[] args) {
- EventManager mgr = new EventManager();
- mgr.createAndStoreEvent("My Event", new Date());
- HibernateUtil.getSessionFactory().close();
- }
- private void createAndStoreEvent(String title, Date theDate) {
- Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- //建立一个Event对象并进行赋值
- Event theEvent = new Event();
- theEvent.setTitle(title);
- theEvent.setDate(theDate);
- //将该对象写入数据库
- session.save(theEvent);
- session.getTransaction().commit();
- }
- }
当所有的工作都做完后,这个项目的布局是如下图所示:
至此,最简单的hibernate小例子就完成了,应该可以运行了