package com.king.stt.dao; import java.io.Serializable; import java.util.Collection; import java.util.List; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.HibernateTemplate; import com.king.util.hibernate.Page; public interface IDao { /** * <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 * * @return HibernateTemplate */ public HibernateTemplate getHibernateTemplate(); public <T> Serializable save(T entity) throws Exception; public <T> boolean update(T entity) throws Exception; public <T> void saveOrUpdate(T entity) throws Exception; public <T> boolean delete(T entity) throws Exception; public <T> boolean deleteAll(Collection<T> entities) throws Exception; public <T> T get(Class<T> c, Serializable id) throws Exception; public <T> T get(String hql, Object... args) throws Exception; public int executeByHql(String hql, Object... args) throws Exception; public int executeBySql(String sql) throws Exception; public <T> List<T> findByHql(String hql, Object... args) throws Exception; public <T> List<T> findBySql(String sql) throws Exception; public <T> List<T> findAll(Class<T> c) throws Exception; /** * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; list集合保存总记录调试和记录结果 * * @param queryHql * 查询记录hql语句 * @param queryCountHql * 查询记录条数hql语句 * @param firstResult * 当前查询页 * @param maxResult * 每页显示多少条 * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 * @throws Exception */ public <T> List<T> queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception; /** * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; * * @createDate 2010-8-3 上午11:16:59 * @author hoojo * @param queryHql * list集合结果查询 * @param queryCountHql * 总记录调试查询 * @param page * 分页对象 * @throws Exception */ public <T> void queryForPage(String queryHql, String queryCountHql, Page<T> page, Object... args) throws Exception; /** * <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 * * @param queryCountHql * hql查询count语句总条数 * @param cResult * DetachedCriteria 动态查询条件 * @param firstResult * 起始 * @param maxResult * 最大页数 * @return List<?> 查询集合 * @throws Exception */ public <T> List<T> queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception; /** * <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity * * @createDate 2010-8-3 上午11:14:30 * @author hoojo * @param queryCountHql * 查询count语句 * @param cResult * DetachedCriteria 动态查询条件类 * @param page * Page分页实体类 * @throws Exception */ public <T> void queryForPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception; /** * <b>function:</b> 传入查询条件DetachedCriteria进行查询 * * @createDate 2010-8-3 上午11:55:28 * @author hoojo * @param <T> * 类型 * @param dc * DetachedCriteria动态条件查询 * @return List * @throws Exception */ public <T> List<T> find(DetachedCriteria dc) throws Exception; }
DaoImpl
package com.king.stt.dao; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.king.util.hibernate.Page; @Repository(value = "dao") public class DaoImpl extends HibernateDaoSupport implements IDao { @Resource public void setSessionFacotry(SessionFactory sessionFacotry) { super.setSessionFactory(sessionFacotry); } @Override public <T> Serializable save(T entity) throws Exception { return this.getHibernateTemplate().save(entity); } @Override public <T> void saveOrUpdate(T entity) throws Exception { this.getHibernateTemplate().saveOrUpdate(entity); } @Override public int executeByHql(String hql, Object... args) throws Exception { return this.getHibernateTemplate().bulkUpdate(hql, args); } @Override public int executeBySql(String sql) throws Exception { return this.getSession().createSQLQuery(sql).executeUpdate(); } @SuppressWarnings("unchecked") @Override public <T> List<T> findBySql(String sql) throws Exception { List<T> list = null; try { list = (List<T>) this.getSession().createSQLQuery(sql).list(); } catch (Exception e) { throw new RuntimeException(e); } return list; } @Override public <T> boolean update(T entity) throws Exception { boolean bo = false; try { this.getHibernateTemplate().update(entity); bo = true; } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } @Override public <T> boolean delete(T entity) throws Exception { boolean bo = false; try { this.getHibernateTemplate().delete(entity); bo = true; } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } @Override public <T> T get(Class<T> c, Serializable id) throws Exception { T ety = null; try { ety = (T) this.getHibernateTemplate().get(c, id); } catch (Exception e) { throw new RuntimeException(e); } return ety; } @SuppressWarnings("unchecked") @Override public <T> T get(String hql, Object... args) throws Exception { T ety = null; try { Query query = this.getSession().createSQLQuery(hql); if (args != null) { for (int i = 0; i < args.length; i++) { query.setParameter(i, args[i]); } } ety = (T) query.setMaxResults(1).uniqueResult(); } catch (Exception e) { throw new RuntimeException(e); } return ety; } @SuppressWarnings("unchecked") @Override public <T> List<T> findByHql(String hql, Object... args) throws Exception { List<T> list = null; try { list = this.getHibernateTemplate().find(hql, args); } catch (Exception e) { throw new RuntimeException(e); } return list; } @SuppressWarnings("unchecked") @Override public <T> List<T> findAll(Class<T> c) throws Exception { List<T> list = null; try { list = (List<T>) this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c)); } catch (Exception e) { throw new RuntimeException(e); } return list; } @SuppressWarnings("unchecked") @Override public <T> List<T> queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception { List<T> list = new ArrayList<T>(); try { Session session = this.getSession(); Query query = session.createQuery(queryHql); for (int i = 0; i < args.length; i++) { query.setParameter(i, args[i]); } list.add((T) query.setFirstResult(firstResult).setMaxResults(maxResult).list()); Query count = session.createQuery(queryCountHql); for (int i = 0; i < args.length; i++) { count.setParameter(i, args[i]); } list.add((T) count.setMaxResults(1).uniqueResult()); } catch (Exception e) { throw new RuntimeException(e); } return list; } @SuppressWarnings("unchecked") @Override public <T> void queryForPage(String queryHql, String queryCountHql, Page<T> page, Object... args) throws Exception { try { Session session = this.getSession(); Query query = session.createQuery(queryHql); for (int i = 0; i < args.length; i++) { query.setParameter(i, args[i]); } page.setResult(query.setFirstResult(page.getCurrentPage()).setMaxResults(page.getPageSize()).list()); Query count = session.createQuery(queryCountHql); for (int i = 0; i < args.length; i++) { count.setParameter(i, args[i]); } page.setTotalsCount(Integer.parseInt(count.setMaxResults(1).uniqueResult().toString())); } catch (Exception e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") @Override public <T> List<T> queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception { List<T> list = new ArrayList<T>(); try { Session session = this.getSession(); list.add((T) this.getHibernateTemplate().findByCriteria(cResult, firstResult, maxResult)); list.add((T) session.createQuery(queryCountHql).setMaxResults(1).uniqueResult()); } catch (Exception e) { throw new RuntimeException(e); } return list; } @SuppressWarnings("unchecked") @Override public <T> List<T> find(DetachedCriteria dc) throws Exception { List<T> list = new ArrayList<T>(); try { list = (List<T>) this.getHibernateTemplate().findByCriteria(dc); } catch (Exception e) { throw new RuntimeException(e); } return list; } @SuppressWarnings("unchecked") @Override public <T> void queryForPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception { try { Session session = this.getSession(); page.setResult(this.getHibernateTemplate().findByCriteria(cResult, page.getCurrentPage(), page.getPageSize())); page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString())); } catch (Exception e) { throw new RuntimeException(e); } } @Override public <T> boolean deleteAll(Collection<T> entities) throws Exception { boolean bo = false; try { this.getHibernateTemplate().deleteAll(entities); bo = true; } catch (Exception e) { bo = false; throw new RuntimeException(); } return bo; } }
相关推荐
dao/hibernate/{自定义的存放包}目录:实现类 service/{自定义的存放包}目录:接口类 service/impl/{自定义的存放包}目录:实现类 action/{自定义的存放包}目录:struts2的类 conf目录下的 {自定义的存放包}...
模板项目 该项目是包含以下功能的模板: 登录页面。 执行授权。 ... 此功能的业务逻辑需自行实现。...域、dao、控制器、服务 - 实现基本登录功能的 bean 您可以将此项目用作应用程序的“起点”,以节省基本配置
天梯是一个用 Java 相关技术搭建的后台 CMS 解决方案,用户可以结合自身业务进行相应扩展,同时提供了针对 dao、service 等的代码生成工具。技术选型:Spring Data JPA、Hibernate、Shiro、 Spring MVC、Layer、...
HibernateTemplate的简单示例
3、实现一套简单的ORM(直接使用spring rowmapper,insert自己实现),可以基于对象进行crud和相对复杂(感觉比hibernate强大一点)的sql操作; 4、基于对象指定查询的字段,大部分时候可以忘掉表结构进行业务...
3.修改jdbc链接,自己导入数据库。 4.把服务器下install\config下的web.xml复制出来覆盖掉新建项目WEB-INF下的web.xml 5.classes下有四个文件,手动烤到myeclipse项目src根目录下中 6.将服务器上jeecms项目删掉,...
框架将各个零散的框架(struts,strust2,springmvc,hibernate,ibatis,spring_jdbc,flex)搭建好,并内置一个代码生成器,辅助项目开发,可以生成java的hibernat model,dao,manager,struts+struts2 action类,可以生成jsp...
(3)add some TemplateMethod in class:LogicNode, to reduce the amount of java code 2:an important new feature in this version is:******[[thread-safe]]****** (1)i'm not used ThreadLocal in this version...
3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于...
包含零件WAR打包,适合在任何Servlet 3.0容器上运行Google Guice v4的IoC Quartz计时器服务Hibernate管理数据库对象REST API,基于Jersey(服务器端)和Retrofit(客户端) Web UI示例:一个通过Vaadin v7
spring-hibernate3-2.0.8.jar spring-jdbc-3.1.1.RELEASE.jar spring-jdbc-4.1.4.RELEASE.jar spring-mock.jar spring-test-3.0.5.RELEASE.jar spring-tx-3.1.1.RELEASE.jar spring-web-3.0.5.RELEASE.jar spring-...
│ Dao.java │ GeneratorDemo.java │ hibernate.cfg.xml │ HibernateDaoImpl.java │ HibernateSessionFactory.java │ HibernateUtil.java │ JsonUtil.java │ list.txt │ log4j.properties │ ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................