下载hibernate包,如hibernate-3.2.0.ga.zip。解压后其目录为下面用HIBERNATE_HOME代替。
HIBERNATE_HOME/hibernate3.jar
HIBERNATE_HOME/lib/*.jar
MySql jdbc驱动
可以创建一个User Library,以后加入时直接加入User Library
从HIBERNATE_HOME/etc中复制hibernate.cfg.xml和log4j.properties到src目录下。
hibernate.cfg.xml为hibernate配置文件。
log4j.properties为log4j配置文件。
为了便于调试最好加入log4j配置文件。
以上每种数据库配制对应的key和value都可以在HIBERNATE_HOME/etc/hibernate.properties中找到。hibernate.properties是hibernate最早用的配制方式,后来改为用xml文件方式了。
<!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="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">xxx</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration>
package com.iteedu.hibernate; import java.util.Date; public class User { private String id; private String name; private Date createDate; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
定义User类的映射文件User.hbm.xml,名字可以随意起。
根为<hibernate-mapping>,每一个实体类对应一个class子标签。
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.iteedu.hibernate.User"> <id name="id"> <generator class="uuid"></generator> </id> <property name="name"></property> <property name="createDate"></property> </class> </hibernate-mapping>
将User.hbml.xml文件加入到hibernate.cfg.xml文件中。
在<session-factory>标签中加入mapping,resource为文件全路径,用“/”分割。
<mapping resource="com/iteedu/hibernate/User.hbm.xml"/>
用hibernate自带的工具类可以根据映射文件自动创建数据库中的表。但数据库还是要自己手工创建。
package com.iteedu.hibernate; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { public static void main(String[] args) { //读取配制文件 Configuration cfg=new Configuration().configure(); SchemaExport se=new SchemaExport(cfg); se.create(true, true); } }
package com.iteedu.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { //读取配制文件 Configuration cfg=new Configuration().configure(); //得到工厂 SessionFactory sf=cfg.buildSessionFactory(); Session session=null; try { //用工厂打开会话 session=sf.openSession(); //开启事务 session.beginTransaction(); User user=new User(); user.setCreateDate(new Date()); user.setName("sdfa"); session.save(user); //提交事务 session.getTransaction().commit(); } catch(Exception e) { e.printStackTrace(); //事务回滚 session.getTransaction().rollback(); } finally { //关闭会话 if(session!=null) { if(session.isOpen()) { session.close(); } } } } }
为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入如下配制:
<property name="hibernate.show_sql">true</property>
<!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="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">dzh</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com.iteedu.hibernate.User.hbm.xml"/> </session-factory> </hibernate-configuration>