hibernate工厂类以及dao书写规范
myeclipse自动创建HibernateSessionFactory,不过我们喜欢自己写,而且喜欢使用eclipse,下面是一个例子,大家可以改成自己喜欢的,只要保证单例模式就行
[java] public final class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory(){}
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static synchronized Session getSession(){
return sessionFactory.openSession();
}
}
public final class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory(){}
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static synchronized Session getSession(){
return sessionFactory.openSession();
}
}
在4.0以后cfg.buildSessionFactory();已经不推荐使用,现在使用buildSessionFactory(ServiceRegistry serviceRegistry)方法替代,代码如下
[java]public final class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory(){}
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static synchronized Session getSession(){
return sessionFactory.openSession();
}
}
public final class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory(){}
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static synchronized Session getSession(){
return sessionFactory.openSession();
}
}
下面做个例子来演示调用,我这里是用户保存
[java] //添加用户
public void addUser(User user){
Session session = null;
Transaction trans = null;
try{
session = HibernateSessionFactory.getSession();//获取和数据库的回话
trans = session.beginTransaction();//事务开始
session.save(user);//保存用户
trans.commit();//提交事务
}catch(HibernateException e){
if(null != trans)
trans.rollback();//回滚
e.printStackTrace();//异常可以抛出去给调用者处理
}finally{
if(null != session)
session.close();//关闭回话
}
}
//添加用户
public void addUser(User user){
Session session = null;
Transaction trans = null;
try{
session = HibernateSessionFactory.getSession();//获取和数据库的回话
trans = session.beginTransaction();//事务开始
session.save(user);//保存用户
trans.commit();//提交事务
}catch(HibernateException e){
if(null != trans)
trans.rollback();//回滚
e.printStackTrace();//异常可以抛出去给调用者处理
}finally{
if(null != session)
session.close();//关闭回话
}
}
这里是一段比较规范的代码,当然大家可以根据实际情况编写,最好统一,写好注释
摘自 mzlqh的专栏
补充:软件开发 , Java ,