方法
通过Hibernate可以简化对数据库的操作,本节首先创建一个HibernateUtil类,用于管理session,然后介绍如何通过Hibernate实现数据库的查询、插入、删除和更新操作。 SessionFactory用来创建Session实例,通过Configuration实例构建SessionFactory。Configuration实例根据当前的配置信息,构造SessionFactory实例并返回。一旦SessionFactory构造完毕,即被赋予特定的配置信息。 Session是持久层操作的基础,相当于JDBC的Connection。通过SessionFactory实例构建。Session实例提供的saveOrUpdate、delete和createQuery方法分别实现了数据库的插入更新、删除和查询操作,简化了数据库的基本操作。
创建单例模式
package Dao;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//SessionFactory单态模式
public class HibernateUtil {
private static Configuration cfg = null;
private static SessionFactory sf = null;
//静态代码块
static { //在类加载的时候只能执行一次
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获得session工厂
public static SessionFactory getSessionFactory() {
return sf;
}
//关闭工厂
public static void closeSessionFactory() {
sf.close();
}
}
}
}
创建session
Session
保证在一个线程中仅存在一个Session实例
1.getCurrentSession (Hibernate 3+);getCurrentSession方法,保证了线程中Session的唯一性;并且可以自动关闭session实例
调用代码:Session session = sessionFactory.getCurrentSession();
使用getCurrentSession需要在hibernate.cfg.xml配置文件中加入
thread
Transaction
更改ManageUser
Transaction tx = session.beginTransaction();
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/firstorm?useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="cn/hrbust/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
保存
public void testSavaUser() {
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
User u = new User();
u.setName("李自轩");
u.setGender("男");
u.setAge(21);
u.setBirthday(Date.valueOf("1999-2-21"));
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
session.save(u);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(ts != null) {
ts.rollback();
}
}
}
查询
public void testQueryUser() {
SessionFactory sf=null;
Session session =null;
Transaction ts=null;
try {
sf=HibernateUtil.getSessionFactory();//SessionFactory单态模式
session =sf.getCurrentSession(); //保证每个读写线程有唯一的session实例
ts=session.beginTransaction();
Query query=session.createQuery("from User");//类名
List<User> users =query.list();
for(User u:users) {
System.out.println(u.getName()+" "+u.getGender()+" "+u.getAge()+" "+u.getBirthday());
}
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(ts!=null) {
ts.rollback();
}
}
}