首先,可以写一个Hibernate自动在数据库中建表的工具类,当然也可以在配置文件hibernate.cfg.xml中配置,<property name="hibernate.hbm2ddl.auto">create</property>
不过在创建表后,一定要配置文件改回来,否则在你再执行程序时,它会把以前已经创建的表删除,重新建表。这样就会造成以前保存的数据丢失。
建表类:
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
/**
* 根据映射文件建表
* @para args
*/
public static void main(String[] args) {
Configuration cfg = new Configuration().configre;//读的是xml文件
SchemaExport se = new SchemaExport(cfg);
se.create(true,true);
}
}
再写一个Hibernate获取Session的工具类,因为SessionFactory是重量级的,最好只创建一次。
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HbnUtil {
private static SessionFactory factory = null;
static {
//读取配置文件,加载默认配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//初始化配置文件中信息,实例化SessionFactory
factory = cfg.buildSessionFactory();
}
public static Session getSession(){
return sf.openSession();//创建Session
}
public static void closeSession(Session s){
if (s != null && s.isOpen()){
s.close();
}
}
}
一个纯Java类(POJO)此类中只含有属性、构造方法、get/set方法
package entity;
import java.util.Date;
public class Classes {
private int oid;//代理主键,用于建表
private String className;//班级名
private int total;//总人数
private Date openDate;//开班日期
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public Date getOpenDate() {
return openDate;
}
public void setOpenDate(Date openDate) {
this.openDate = openDate;
}
}
配置的实体类与表的映射文件Classes.hbm.xml,类名-->表名 类的属性-->表的字段(column)
<?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="entity.Classes" table="t_classes">
<id name="oid" column="id">
<generator class="native"></generator>
</id>
<property name="className" column="class_name"
length="50" type="string"/>
<property name="total" type="integer"/>
<property name="openDate" column="open_date"
type="date"></property>
</class>
</hibernate-mapping>
Hibernate的配置文件hibernate.cfg.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<!--
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.23:1521:tarena</property>
<property name="connection.username">openlab</property>
<property name="connection.password">open123</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
-->
<mapping resource="entity/User.hbm.xml"/>
<mapping resource="entity/Classes.hbm.xml"/>
</session-factory>
</hibernate-configuration>
最后是一个简单测试类
package test;
import java.text.SimpleDateFormat;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HbnUtil;
import entity.Classes;
public class TestClasses {
/**
* @param args
*/
public static void main(String[] args) {
//创建持久对象的操作对象Session
Session session = HbnUtil.getSession();
try {
//开启事务
Transaction tx = session.beginTransaction();
Classes c = new Classes();
c.setClassName("SD0901");
c.setTotal(43);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
c.setOpenDate(sdf.parse("2009/01/01"));
//保存对象
session.save(c);
//修改班级名称
c.setClassName("ASD0901");
//删除对象
session.delete(c);
//根据对象id来查找对象
//get()/load()
Classes cl = (Classes)session.get(Classes.class, 1);
System.out.println("Id为1的班级名称:"+cl.getClassName());
//定义查询对象,HQL
Query query = session.createQuery("from Classes");
//执行查询
List<Classes> all = query.list();
for(Classes classes:all){
System.out.println("班级名称:"+classes.getClassName());
}
//提交事务
tx.commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally{
HbnUtil.closeSession(session);
}
}
}
以上是一个很简单的例子。O(∩_∩)O