关于hibernate操作数据库的DAO类

wanggaina666 2008-10-16 05:28:34
关于hibernate操作数据库的DAO类,抽象出了一个 BaseHibernateImpl类,他继承自自定义接口:BaseHibernateI。
BaseHibernateImpl我一直觉得不是很完善,大家有没有更好的DAO类?拿出来分享一下...等待高手的类...
下面是BaseHibernateI接口和BaseHibernateImpl类:
...
public interface BaseHibernateI {


public Object get(Class clz,java.io.Serializable id);

public void add(Object o);

public void delete(Class clz,java.io.Serializable id);

public List search(Class clz,Object o);

public void update(Object o);
}
------------------------------------------
....
public abstract class BaseHibernateImpl implements BaseHibernateI {

private Session session = null;

public void add(Object o) {
// TODO 自动生成方法存根
Transaction tx = null;
session = getSession();
try {

tx = session.beginTransaction();
session.save(o);
tx.commit();

} catch (HibernateException e) {
// TODO Auto-generated catch block
throw e;
} finally {
this.CloseSession();
}
}

public void delete(Class clz, Serializable id) {
// TODO 自动生成方法存根
Transaction tx = null;
session = this.getSession();
try {
tx = session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage()+".............."+e);
throw e;
} finally {
this.CloseSession();
}

}

public Object get(Class clz, Serializable id) {
// TODO 自动生成方法存根
Object ret = null;
session = getSession();
try {
ret = session.get(clz, id);
} catch (HibernateException e) {
// TODO Auto-generated catch block
throw e;
} finally {
this.CloseSession();
}

return ret;
}

public List search(Class clz, Object o) {
// TODO 自动生成方法存根
session = getSession();
List result;
try {
result = session.createCriteria(clz).add(Example.create(o)).list();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
throw e;
}
return result;
}

public void update(Object o) {
// TODO 自动生成方法存根
Transaction tx = null;
session = this.getSession();
try {
tx = session.beginTransaction();
session.update(o);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
throw e;
} finally {
this.CloseSession();
}
}

public Session getSession() {
this.session = HibernateSessionFactory.getSession();
return this.session;
}

public void setSession(Session session) {
this.session = session;
}

public void CloseSession() {
this.session.close();
}


}

大家各显奇能吧!
...全文
326 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
aixiaorenzhe123 2008-10-20
  • 打赏
  • 举报
回复
在这里感觉每个方法让程序给你生成一个Session比较浪费,
我们最好是利用ThreadLocal维护Session这样一个线程维护一个
Session也符合一些业务需要!
xiaojiit 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 masterkingman 的回复:]
引用:
hibernate对数据库已经有了封装,各种数据库方法已经够简洁,如果再搞封装,那何时是头!

现在都在追求简洁呢,不要再封啊封的了
------------------------------------------------------
试想,如果一个项目有20个dao,每个dao有4~5个方法,就是80~100个方法,这时候spring模板的优势就体现了
[/Quote]
支持!
老冯laofeng 2008-10-18
  • 打赏
  • 举报
回复
我建了一个名叫java新时代的技术交流,群号是60694902
期待你们的加入哦!


本群要求成员必须是java专业
提供技术交流,问题解答,讨论Java程序员创业之路等。
老冯laofeng 2008-10-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 masterkingman 的回复:]
引用:
hibernate对数据库已经有了封装,各种数据库方法已经够简洁,如果再搞封装,那何时是头!

现在都在追求简洁呢,不要再封啊封的了
------------------------------------------------------
试想,如果一个项目有20个dao,每个dao有4~5个方法,就是80~100个方法,这时候spring模板的优势就体现了
[/Quote]

顶一下
java__king 2008-10-18
  • 打赏
  • 举报
回复
masterkingman 2008-10-17
  • 打赏
  • 举报
回复
引用:
hibernate对数据库已经有了封装,各种数据库方法已经够简洁,如果再搞封装,那何时是头!

现在都在追求简洁呢,不要再封啊封的了
------------------------------------------------------
试想,如果一个项目有20个dao,每个dao有4~5个方法,就是80~100个方法,这时候spring模板的优势就体现了
老冯laofeng 2008-10-17
  • 打赏
  • 举报
回复
楼主可以利用java的反射机制,在抽象类中定义下方法:
protected abstract Class getReferenceClass();
子类只需要实现该方法,
那么抽象类中所有的查询就可以通用了
  • 打赏
  • 举报
回复
我前几天刚用struts+DAO+hibernate开发的一个留言版把DAO帖出来吧!

//PersonDAO.java类
package com.jercy.struts.note.dao;

import com.jercy.struts.note.vo.Person;

public interface PersonDAO {
// 做登陆验证
public boolean login(Person person) throws Exception;
};


//NoteDAO.java类
package com.jercy.struts.note.dao;

import java.util.List;

import com.jercy.struts.note.vo.Note;

public interface NoteDAO {
// 增加操作
public void insert(Note note) throws Exception;

// 修改操作
public void update(Note note) throws Exception;

// 删除操作
public void delete(int id) throws Exception;

// 按ID查询,主要为更新使用
public Note queryById(int id) throws Exception;

// 查询全部
public List queryAll() throws Exception;

// 模糊查询
public List queryByLike(String cond) throws Exception;
};

whetu 2008-10-17
  • 打赏
  • 举报
回复
用hibernate3,spring2吧,让dao继承HibernateDaoSupport。然后使用getHibernateTemplate(),就不用手动管理session和事务提交了。使用很方便。
yangfuchao418 2008-10-17
  • 打赏
  • 举报
回复
稍微共享下吧,觉得可以就行
package com.comm;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.SessionFactory.HibernateSessionFactory;
//T:表示某一种类型,这个类使用了泛型类,并且获取该泛型的CLass对象
//这样我们就不需要在一些方法中间传入Class对象

public class BaseDao3<T> {
// 该语句得到当前对象的父类的第一个泛型类型对应的Class对象
// this.getClass()得到当前对象对应的Class
// getClass().getGenericSuperclass())得到父类对应的class
Class<T> entityClass = (Class<T>) ((ParameterizedType) this.getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];

public boolean add(T ob){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
session.save(ob);
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return false;
}
public boolean delete(T ob){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
session.delete(ob);
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return false;
}
public boolean update(T ob){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
session.saveOrUpdate(ob);
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return false;
}
public boolean deleteById(Serializable id){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
session.delete(session.get(entityClass,id));
tx.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return false;
}
//主键,具体的类型
public T getObById(Serializable id){
Session session=HibernateSessionFactory.getSession();
return (T)session.get(entityClass,id);
}
public List<T> getAllObject(){
Session session=HibernateSessionFactory.getSession();
Criteria cri=session.createCriteria(entityClass);
return cri.list();
}
}




Landor2004 2008-10-16
  • 打赏
  • 举报
回复
hibernate对数据库已经有了封装,各种数据库方法已经够简洁,如果再搞封装,那何时是头!

现在都在追求简洁呢,不要再封啊封的了
masterkingman 2008-10-16
  • 打赏
  • 举报
回复
spring里面有个支持hibernate的Dao,用他的模板就可以省去很多关于session的重复代码了,什么try啊catch啊finally啊统统都可以省去,还有声明式事务也可以加上

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧