在做项目的时候每次都为每个POJO实体写一个对应的DAO很麻烦,虽然可以由IDE或者一些快速生成工具来生成,但是也难免需要更改。处于这个原因,个人写了一个通用的泛型DAO来封装一些最基本的操作,包括对实体的增删改查。基于Spring的hibernate模板实现,初学,如有什么不好的地方请大家多多拍砖! Java代码?
/**??
* DAO操作基类 本DAO层实现了通用的数据操作??
*???
* @author 黄磊??
*???
* @param <T>??
*??????????? POJO实体对象??
* @param <ID>??
*??????????? ID??
*/??
public class BaseHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport {???
??
??? private static final Logger logger = Logger.getLogger(BaseHibernateDAO.class);???
??
??? /**??
???? * 保存指定实体类??
???? *???
???? * @param entityobj??
???? *??????????? 实体类??
???? */??
??? public void save(T entity) {???????????
??????? try {???
??????????? getHibernateTemplate().save(entity);???
??????? } catch (RuntimeException e) {???
??????????? logger.error("保存实体异常", e);???
??????????? throw e;???
??????? }???
??? }???
??
??? /**??
???? * 删除指定实体??
???? *???
???? * @param entityobj??
???? *??????????? 实体类??
???? */??
??? public void delete(T entity) {???
??????? try {???
??????????? getHibernateTemplate().delete(entity);???
??????? } catch (RuntimeException e) {???
??????????? logger.error("删除实体异常", e);???
??????????? throw e;???
??????? }???
??? }???
???????
??? /**??
???? * 更新或保存指定实体??
???? *???
???? * @param entity 实体类??
???? */??
??? public void saveOrUpdate(T entity) {???
??????? try {???
??????????? getHibernateTemplate().saveOrUpdate(entity);???
??????? } catch (RuntimeException e) {???
??????????? logger.error("更新或保存实体异常", e);???
??????????? throw e;???
??????? }???
??? }???
??
??? /**??
???? * 查找指定ID实体类对象??
???? *???
???? * @param entityClass??
???? *??????????? 实体Class??
???? * @param id??
???? *??????????? 实体ID??
???? * @return 实体对象??
???? */??
??? @SuppressWarnings("unchecked")???
??? public T findById(Class<T> entityClass, ID id) {???
??????? try {???
??????????? return (T) getHibernateTemplate().get(entityClass, id);???
??????? } catch (RuntimeException e) {???
??????????? logger.error("查找指定ID实体异常,ID:" + id, e);???
??????????? throw e;???
??????? }???
??? }???
??
??? /**??
???? * 查找指定属性的实体集合??
???? *???
???? * @param entityClass??
???? *??????????? 实体??
???? * @param propertyName??
???? *??????????? 属性名??
???? * @param value??
???? *??????????? 条件??
???? * @return 实体集合??
???? */??
??? @SuppressWarnings("unchecked")???
??? public List<T> findByProperty(Class<T> entityClass, String propertyName,???
??????????? Object value) {???
??????? try {???
??????????? String queryStr = "from " + entityClass.getName()???
??????????????????? + " as model where model." + propertyName + "=?";??????????????
??????????? return getHibernateTemplate().find(queryStr, value);???
??????? } catch (RuntimeException e) {???
??????????? logger.error("查找指定条件实体集合异常,条件:" + propertyName, e);???
??????????? throw e;???
??????? }???
??? }???
??
??? /**??
???? * 查询指定HQL语句的分页数据集合??
???? *???
???? * @param hsql??
???? *??????????? HQL语句??
???? * @param firstRow??
???? *??????????? 开始记录号??
???? * @param maxRow??
???? *??????????? 最大记录号??
???? * @return 分页数据集合??
???? * @throws Exception??
???? *???????????? 抛出异常??
???? */??
??? @SuppressWarnings("unchecked")???
??? public List<T> findByPage(final String hsql, final int firstRow,???
??????????? final int maxRow) {????????
??????? try {???
??????????? return getHibernateTemplate().executeFind(new HibernateCallback() {???
??????????????? public Object doInHibernate(Session s)???
??????????????????????? throws HibernateException, SQLException {???
??????????????????? Query query = s.createQuery(hsql);???
??????????????????? query.setFirstResult(firstRow);???
??????????????????? query.setMaxResults(maxRow);???
??????????????????? List list = query.list();???
??????????????????? return list;???
??????????????? }???
??????????? });???
??????? } catch (RuntimeException e) {???
??????????? logger.error("分页查询异常,HQL:" + hsql, e);???
??????????? throw e;???
??????? }???
??? }??????
???????
??? /**??
???? * 从Spring上下文中获取本类对象??
???? * 此方法可能存在线程并发问题(待测)??
???? *???
???? * @param context Spring上下文??
???? * @return 本类对象???
???? */??
??? @SuppressWarnings("unchecked")???
??? public static BaseHibernateDAO getFromApplicationContext(WebApplicationContext context) {???
??????? return (BaseHibernateDAO)context.getBean("BaseHibernateDAO");???
??? }
}??
详细解决方案
j2ee学习札记——初试ssh之泛型通用dao(转)
热度:508 发布时间:2016-04-22 01:12:07.0
相关解决方案
- j2ee 中怎样实现上传头像后按一定比例截取解决方案
- 向左走(J2EE)向右走(Andorid)如果是您(前辈!)你选哪一个?解决方法
- 南京JAVA /j2ee 软件工程师社区 技术交流/薪资.(限已工作人员)
- 向左走(J2EE)向右走(Andorid)如果是你(前辈!)你选哪一个
- 寻SSH(J2EE)技术转J2ME一些建议解决方案
- j2ee 小女子不才,选用ssh框架做毕业设计,遇到有关问题颇多,望有贤之士help me,呀,快要答辩了
- JEECG(J2EE Code Generation) 基于代码生成器J2EE智能开发框架 公布新版本通知
- use interceptor to log informations (J2EE 六 利用interceptor 打Log)
- J2EE 惯用JAR包介绍
- 【转】eclipse ide for java ee developers 开发环境筹建(j2ee)
- J2EE、J2SE、J2ME的容易区别
- J2SE J2EE J2ME的差异 (转)
- 学厌了就得回头见见这几个概念J2SE J2EE J2ME JDK JRE SDK
- 有点什么技术架构?J2EE?NET?求大侠解决
- 万佛归宗——从 VB.NET 到 C# 的语言转换工具!从 Java/J2EE 到 C# 的自动转换工具!解决方案
- 买本《Expert One-on-One J2EE Design and Development》,该怎么解决
- 买本《Expert One-on-One J2EE Design and Development》,该如何解决
- J2EE WEB-Tomcat5.5.9中文问题解决方案
- J2EE & J2SE
- [求助]j2ee 各种技术学习
- J2EE
- 值得思考的 J2EE 架构的6个最佳实践
- J2EE J2SE 都混了
- Brief introduction of J2EE
- J2EE/WEB开发研讨群
- 电子书Building J2EE Applications with the Rational Unified Process
- J2EE 对通信工作好么?
- J2EE SDK在那里现在?
- Hisoft招聘Java/J2EE Software Engineer(Location:上海)
- J2EE 初学,请指点