1.hibernate的配置文件,一般放在classpath的根目录下,默认命名为hibernate.cfg.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.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">create-drop</property>
<property name="show_sql">true</property>
<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.hibernate的映射文件,User.hbm.xml,代码例子如下:
<?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 package="cn.itcast.hibernate.domain">
<class name="User">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
3.数据操作类,代码例子:
package cn.itcast.hibernate;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal session = new ThreadLocal();
private HibernateUtil() {
}
static {
Configuration cfg = new Configuration();
cfg.configure(); //如果配置文件路径或名字发生改变,那么就是cfg.configure("配置文件路径及名称");
sessionFactory = cfg.buildSessionFactory();
}
public static Session getThreadLocalSession() {
Session s = (Session) session.get();
if (s == null) {
s = getSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null) {
s.close();
session.set(null);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() {
return sessionFactory.openSession();
}
public static void add(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
}
public static void update(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
}
public static void delete(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
}
public static Object get(Class clazz, Serializable id) {
Session s = null;
try {
s = HibernateUtil.getSession();
Object obj = s.get(clazz, id);
return obj;
} finally {
if (s != null)
s.close();
}
}
}