hibernate底层dao封装

最近公司有任务,用的是ssh框架,我看了下公司的dao的封装确实牛逼,所有我也参考他的dao进行了封装。废话少说先上代码。

package cn.com.wanhao.dao.impl.base;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.com.wanhao.util.Page;

public interface BaseDao
{
	/**
	 * <b>function:</b>增加一个entity对象,返回是否添加成功
	 * @createDate 2016-11-25 下午02:44:34
	 * @author ending
	 * @param T 对象类型
	 * @param entity 实体类
	 * @return boolean true/false
	 * @throws Exception
	 * */
	public <T> boolean add(T entity) throws Exception;
	/**
	 * <b>function:</b>增加一个entity对象,返回String主键
	 * @createDate 2016-11-25 下午02:45:15
	 * @author ending
	 * @param T 对象类型
	 * @param entity 实体类
	 * @return String
	 * @throws Exception
	 * */
	public <T> String addStringKey(T entity) throws Exception;
	/**
	 * <b>function:</b>增加一个entity对象,返回Integer主键
	 * @createDate 2016-11-25 下午02:45:15
	 * @author ending
	 * @param T 对象类型
	 * @param entity 实体类
	 * @return Integer
	 * @throws Exception
	 * */
	public <T> Integer addNumKey(T entity) throws Exception;
	/**
	 * <b>function:</b>传入一个hql语句,返回影响多少条数据
	 * @createDate 2016-11-25 下午02:45:15
	 * @author ending
	 * @param hql
	 * @return int
	 * @throws Exception
	 * */
	public int executeData(String hql) throws Exception;
	/**
	 * <b>function:</b>传入一个hql语句,返回list集合
	 * @createDate 2016-11-25 下午02:53:14
	 * @author ending
	 * @param hql
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> findByHql(String hql) throws Exception;
	/**
	 * <b>function:</b>执行sql语句,实行增,删,改
	 * @createDate 2016-11-25 下午02:56:03
	 * @author ending
	 * @param sql
	 * @return Integer
	 * @throws Exception
	 * */
	public int executeBySql(String sql) throws Exception;
	/**	
	 * <b>function:</b>执行sql查询语句,获取list集合
	 * @createDate 2016-11-25 下午02:56:03
	 * @author ending
	 * @param sql
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> findBySql(String sql) throws Exception;
	/**	
	 * <b>function:</b>修改实体的数据,返回boolean结果
	 * @createDate 2016-11-25 下午03:01:08
	 * @author ending
	 * @param entity
	 * @return boolean
	 * @throws Exception
	 * */
	public <T> boolean editEntity(T entity) throws Exception;
	/**	
	 * <b>function:</b>执行hql语句,返回boolean结果
	 * @createDate 2016-11-25 下午03:01:08
	 * @author ending
	 * @param hql
	 * @return boolean
	 * @throws Exception
	 * */
	public boolean editHql(String hql) throws Exception;
	/**	
	 * <b>function:</b>执行hql语句,返回执行结果影响的行数
	 * @createDate 2016-11-25 下午03:04:10
	 * @author ending
	 * @param hql
	 * @return Integer
	 * @throws Exception
	 * */
	public int editNumHql(String hql) throws Exception;
	/**	
	 * <b>function:</b>传入要删除的实体,返回boolean结果
	 * @createDate 2016-11-25 下午03:08:33
	 * @author ending
	 * @param entity
	 * @return boolean
	 * @throws Exception
	 * */
	public <T> boolean removeEntity(T entity) throws Exception;
	/**	
	 * <b>function:</b>传入要实体类的class和int主键,返回具体实体
	 * @createDate 2016-11-25 下午03:12:07
	 * @author ending
	 * @param clazz
	 * @param id
	 * @return T
	 * @throws Exception
	 * */
	public <T> T getById(Class<T> clazz,int id) throws Exception;
	/**	
	 * <b>function:</b>传入要实体类的class和String主键,返回具体实体
	 * @createDate 2016-11-25 下午03:12:07
	 * @author ending
	 * @param clazz
	 * @param id
	 * @return T
	 * @throws Exception
	 * */
	public <T> T getById(Class<T> clazz,String id) throws Exception;
	/**	
	 * <b>function:</b>传入要实体类的class和Serializable主键,返回具体实体
	 * @createDate 2016-11-25 下午03:12:07
	 * @author ending
	 * @param clazz
	 * @param id
	 * @return T
	 * @throws Exception
	 * */
	public <T> T getById(Class<T> clazz,Serializable id) throws Exception;
	/**	
	 * <b>function:</b>传入hql语句,返回实体
	 * @createDate 2016-11-25 下午03:16:21
	 * @author ending
	 * @param hql
	 * @return T
	 * @throws Exception
	 * */
	public <T> T getByHql(String hql) throws Exception;
	/**	
	 * <b>function:</b>传入hql语句,返回实体集合
	 * @createDate 2016-11-25 下午03:17:26
	 * @author ending
	 * @param hql
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> getList(String hql) throws Exception;
	/**	
	 * <b>function:</b>传入hql语句删除数据,返回执行结果
	 * @createDate 2016-11-25 下午03:18:44
	 * @author ending
	 * @param hql
	 * @return boolean
	 * @throws Exception
	 * */
	public boolean remove(String hql) throws Exception;
	/**	
	 * <b>function:</b>动态查询
	 * @createDate 2016-11-25 下午03:18:44
	 * @author ending
	 * @param clazz
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> getList(Class<T> clazz) throws Exception;
	/**	
	 * <b>function:</b>传入hql查询语句和object数组的参数,list返回结果
	 * @createDate 2016-11-25 下午03:18:44
	 * @author ending
	 * @param hql
	 * @param obj
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> getList(String hql,Object[] obj) throws Exception;
	/**	
	 * <b>function:</b>传入查询语句和查询总条数的hql,当前页数,一页显示多少数据,用list集合返回
	 * @createDate 2016-11-25 下午03:26:39
	 * @author ending
	 * @param queryHql
	 * @param queryCountHql
	 * @param firstResult
	 * @param maxResult
	 * @return List
	 * @throws Exception
	 * */
	public List<?> showPage(String queryHql,String queryCountHql,int firstResult,int maxResult) throws Exception;
	/**	
	 * <b>function:</b>传入查询语句和查询总条数的hql,page分页对象,用list集合返回
	 * @createDate 2016-11-25 下午03:26:39
	 * @author ending
	 * @param queryHql
	 * @param queryCountHql
	 * @param page
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> showPage(String queryHql,String queryCountHql,Page<T> page) throws Exception;
	/**	
	 * <b>function:</b>传入查询语句和查询总条数的hql,DetachedCriteria动态查询条件进行分页,用list集合返回
	 * @createDate 2016-11-25 下午03:26:39
	 * @author ending
	 * @param queryHql
	 * @param criteria
	 * @param firstResult
	 * @param maxResult
	 * @return List
	 * @throws Exception
	 * */
	@SuppressWarnings("rawtypes")
	public List showPage(String queryHql,DetachedCriteria criteria,int firstResult,int maxResult) throws Exception;
	/**	
	 * <b>function:</b>传入查询语句和查询总条数的hql,DetachedCriteria动态查询条件进行分页,分页对象page,用list集合返回
	 * @createDate 2016-11-25 下午03:26:39
	 * @author ending
	 * @param queryHql
	 * @param criteria
	 * @param page
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> showPage(String queryHql,DetachedCriteria criteria,Page<T> page) throws Exception;
	/**	
	 * <b>function:</b>传入DetachedCriteria动态查询条件,用list集合返回
	 * @createDate 2016-11-25 下午03:38:01
	 * @author ending
	 * @param criteria
	 * @return List
	 * @throws Exception
	 * */
	public <T> List<T> find(DetachedCriteria criteria) throws Exception;
	/**	
	 * <b>function:</b>提供session使用
	 * @createDate 2016-11-25 下午03:39:22
	 * @author ending
	 * @return Session
	 * */
	public Session session();
	/**	
	 * <b>function:</b>提供HibernateTemplate使用
	 * @createDate 2016-11-25 下午03:39:22
	 * @author ending
	 * @return HibernateTemplate
	 * */
	public HibernateTemplate getTemplate();
}

package cn.com.wanhao.dao.impl.base.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.wanhao.dao.impl.base.BaseDao;
import cn.com.wanhao.util.Page;

@SuppressWarnings("unchecked")
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao
{

	@Override
	public <T> boolean add(T entity) throws Exception
	{
		boolean bool = false;
		try
		{
			Serializable ser = this.getHibernateTemplate().save(entity);
			if (ser != null)
			{
				bool = true;
			}
		}
		catch (Exception e)
		{
			bool = false;
			throw new RuntimeException(e);
		}
		return bool;
	}

	@Override
	public <T> String addStringKey(T entity) throws Exception
	{
		String id = null;
		try
		{
			id = (String) this.getHibernateTemplate().save(entity);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return id;
	}

	@Override
	public <T> Integer addNumKey(T entity) throws Exception
	{
		Integer id = null;
		try
		{
			id = (Integer) this.getHibernateTemplate().save(entity);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return id;
	}

	@Override
	public int executeData(String hql) throws Exception
	{
		try
		{
			return this.getHibernateTemplate().bulkUpdate(hql);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	@Override
	public <T> List<T> findByHql(String hql) throws Exception
	{
		List<T> list = null;
		try
		{
			list = this.getHibernateTemplate().find(hql);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public int executeBySql(String sql) throws Exception
	{
		try
		{
			return this.getSession().createSQLQuery(sql).executeUpdate();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	@Override
	public <T> List<T> findBySql(String sql) throws Exception
	{
		try
		{
			return this.getSession().createSQLQuery(sql).list();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	@Override
	public <T> boolean editEntity(T entity) throws Exception
	{
		boolean bool = false;
		try
		{
			this.getHibernateTemplate().update(entity);
			bool = true;
		}
		catch (Exception e)
		{
			bool = false;
			throw new RuntimeException(e);
		}
		return bool;
	}

	@Override
	public boolean editHql(String hql) throws Exception
	{
		boolean bool = false;
		try
		{
			int num = this.getHibernateTemplate().bulkUpdate(hql);
			bool = num > 0 ? true : false;
		}
		catch (Exception e)
		{
			bool = false;
			throw new RuntimeException(e);
		}
		return bool;
	}

	@Override
	public int editNumHql(String hql) throws Exception
	{
		int count = 0;
		try
		{
			count = this.getHibernateTemplate().bulkUpdate(hql);
		}
		catch (Exception e)
		{
			count = 0;
			throw new RuntimeException(e);
		}
		return count;
	}

	@Override
	public <T> boolean removeEntity(T entity) throws Exception
	{
		boolean bool = false;
		try
		{
			this.getHibernateTemplate().delete(entity);
			bool = true;
		}
		catch (Exception e)
		{
			bool = false;
			throw new RuntimeException(e);
		}
		return bool;
	}

	@Override
	public <T> T getById(Class<T> clazz, int id) throws Exception
	{
		T t = null;
		try
		{
			t = (T) this.getHibernateTemplate().get(clazz, id);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return t;
	}

	@Override
	public <T> T getById(Class<T> clazz, String id) throws Exception
	{
		T t = null;
		try
		{
			t = (T) this.getHibernateTemplate().get(clazz, id);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return t;
	}

	@Override
	public <T> T getById(Class<T> clazz, Serializable id) throws Exception
	{
		T t = null;
		try
		{
			t = (T) this.getHibernateTemplate().get(clazz, id);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return t;
	}

	@Override
	public <T> T getByHql(String hql) throws Exception
	{
		T t = null;
		try
		{
			t = (T) this.getSession().createQuery(hql).setMaxResults(1).uniqueResult();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return t;
	}

	@Override
	public <T> List<T> getList(String hql) throws Exception
	{
		List<T> list = null;
		try
		{
			list = this.getHibernateTemplate().find(hql);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public boolean remove(String hql) throws Exception
	{
		boolean bool = false;
		try
		{
			bool = this.executeBySql(hql) > 0 ? true : false;
		}
		catch (Exception e)
		{
			bool = false;
			throw new RuntimeException(e);
		}
		return bool;
	}

	@Override
	public <T> List<T> getList(Class<T> clazz) throws Exception
	{
		List<T> list = null;
		try
		{
			list = this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(clazz));
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public <T> List<T> getList(String hql, Object[] obj) throws Exception
	{
		List<T> list = null;
		try
		{
			list = this.getHibernateTemplate().find(hql, obj);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception
	{
		List<Object> list = new ArrayList<Object>();
		try
		{
			Session session = this.getSession();
			list.add(session.createQuery(queryHql).setFirstResult(firstResult).setMaxResults(maxResult).list());
			list.add(session.createQuery(queryCountHql).setFirstResult(firstResult).uniqueResult());
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public <T> List<T> showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception
	{
		try
		{
			Session session = this.getSession();
			page.setList(session.createQuery(queryHql).setFirstResult(page.getCunrrentPage())
					.setMaxResults(page.getPageSize()).list());
			page.setTotalCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult()
					.toString()));
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return page.getList();
	}

	@Override
	public List<?> showPage(String queryHql, DetachedCriteria criteria, int firstResult, int maxResult)
			throws Exception
	{
		List<Object> list = new ArrayList<Object>();
		try
		{
			Session session = this.getSession();
			list.add(this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResult));
			list.add(session.createQuery(queryHql).setMaxResults(1).uniqueResult());
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public <T> List<T> showPage(String queryHql, DetachedCriteria criteria, Page<T> page) throws Exception
	{
		try
		{
			Session session = this.getSession();
			page.setList(this.getHibernateTemplate().findByCriteria(criteria, page.getCunrrentPage(),
					page.getPageSize()));
			page.setTotalCount(Integer.parseInt(session.createQuery(queryHql).setMaxResults(1).uniqueResult()
					.toString()));
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return page.getList();
	}

	@Override
	public <T> List<T> find(DetachedCriteria criteria) throws Exception
	{
		List<T> list = null;
		try
		{
			list=this.getHibernateTemplate().findByCriteria(criteria);
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public Session session()
	{
		return this.getSession();
	}

	@Override
	public HibernateTemplate getTemplate()
	{
		return this.getHibernateTemplate();
	}

}
以上基本就是公用的dao了,可以给任务dao层调用,这次写了下次就不用写了。下面就是你本身项目dao到用上面的了,想用就用。

package cn.com.wanhao.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.wanhao.dao.LoginToDao;
import cn.com.wanhao.dao.impl.base.BaseDao;
import cn.com.wanhao.entity.Perssion;
import cn.com.wanhao.entity.RoleTable;

@Component
public class LoginToDaoImpl implements LoginToDao{
	@Autowired
	private BaseDao baseDao;


	public BaseDao getBaseDao()
	{
		return baseDao;
	}


	public void setBaseDao(BaseDao baseDao)
	{
		this.baseDao = baseDao;
	}


	@Override
	public Perssion find(int id) throws Exception {
		return baseDao.getById(Perssion.class, id);
	}


	@Override
	public void save(Perssion perssion) throws Exception
	{
		baseDao.add(perssion);
	}


	@Override
	public Integer addNumKey(RoleTable entity) throws Exception
	{
		return baseDao.addNumKey(entity);
	}

}

package cn.com.wanhao.util;

import java.util.ArrayList;
import java.util.List;

public class Page<T>
{
	//当前页码
	private int cunrrentPage;
	//全部页码
	private int totalPage;
	//全部数据
	private int totalCount;
	//每页多少数据
	private int pageSize;
	//查询返回结果
	private List<T> list=new ArrayList<T>();
	//分页链接
	private String url;
	
	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public int getCunrrentPage()
	{
		return cunrrentPage;
	}

	public void setCunrrentPage(int cunrrentPage)
	{
		if(cunrrentPage<0){
			cunrrentPage=0;
		}
		this.cunrrentPage = cunrrentPage;
	}

	public int getTotalPage()
	{
		if(totalCount%pageSize==0){
			totalPage=totalCount/pageSize;
		}else{
			totalPage=totalCount/pageSize+1;
		}
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		if(totalPage<0){
			totalPage=0;
		}
		this.totalPage = totalPage;
	}

	public int getTotalCount()
	{
		return totalCount;
	}

	public void setTotalCount(int totalCount)
	{
		if(totalCount<0){
			totalCount=0;
		}
		this.totalCount = totalCount;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		if(pageSize<20){
			pageSize=20;
		}
		this.pageSize = pageSize;
	}

	public List<T> getList()
	{
		return list;
	}

	public void setList(List<T> list)
	{
		this.list = list;
	}
}


记住还要在spring配置文件中

    <bean id="loginToDao" class="cn.com.wanhao.dao.impl.base.impl.BaseDaoImpl">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean> 
这个一定要注入公用dao中


  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
第24次课-1 Spring与Hibernate的整合 本节主要内容 24.1 概述 24.2 管理SessionFactory 24.3 Spring对Hibernate的简化 24.1 概述 24.1.1 概述 Spring提供了很多IoC特性的支持,方便处理大部分典型的Hibernate整合问题。 如:SessionFactory的注入、HibernateTemplate的简化操作、DAO的支持等。 为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系和事务管理方法。 24.1 概述 24.1.1 概述 如果Spring与Hibernate进行了整合,则Hibernate便处于被Spring管理的状态下,Hibernate所需的基础资源,都由Spring以注入的方式提供。 由Spring接管的内容包括: Hibernate创建SessionFactory时需要的DataSource 执行持久化必需的Session 持久层访问必需的事务控制 24.1 概述 24.1.1 概述 Spring的管理方式: 将DataSource、SessionFactory、Transaction等作为Spring的Bean,通过配置文件的方式来管理。 24.1 概述 24.1.1 概述 Spring提供了DAO支持,可以简化DAO组件的开发,特别是IoC容器的使用,提供了DAO组件与业务逻辑组件之间的松耦合组合方式。 所有的DAO组件,都由容器负责注入到业务逻辑组件中,使用业务逻辑组件无需关心DAO组件的实现。 24.1 概述 24.1.2 两者结合的优势 通用的资源管理:Spring的ApplicationContext能够管理SessionFactory,通过配置文件可以方便改写相关的配置。 有效的Session管理:Spring提供了有效、简单、安全的Hibernate Session处理。 IoC容器降低了DAO组件与业务逻辑层之间的耦合性。 DAO模式的使用,降低了系统重构的代价。 方便的事务管理:Spring提供的声明式事务处理可以全面有效地处理事务。 异常包装:Spring能够包装Hibernate的异常,使开发者可以选择恰当的层来处理异常。 24.2 管理SessionFactory Hibernate的SessionFactory,是单个数据库映射关系编译后的内存镜像,是Hibernate执行持久化访问的基础。 Spring通过ApplicationContext管理SessionFactory,可以不使用Hibernate应用必需的hibernate.cfg.xml。 Spring配置管理SessionFactory与数据库的连接,在实际的应用中,数据源会采用依赖注入的方式,传递给SessionFactory。 见beans-config_sh.xml 24.3 Spring对Hibernate的简化 24.3.1 概述 Hibernate的持久层访问步骤: 创建Configuration实例 创建SessionFactory实例 创建Session实例 打开事务 开始持久化访问 提交事务 如遇异常,回滚事务 关闭Session 24.3 Spring对Hibernate的简化 24.3.1 概述 Spring提供的持久层访问的方式,无须显式地打开和关闭Session,也无须在代码中执行任何的事务操作语句。 Spring提供了HibernateTemplate,用于持久层访问。它只要获得SessionFactory的引用,就可以智能地打开Session,并在持久化访问结束后关闭Session,程序开发只需完成持久层逻辑,通用的操作则由HibernateTemplate完成。 24.3 Spring对Hibernate的简化 24.3.2 简化的具体表现 Spring对Hibernate的简化包括: 基于依赖注入的SessionFactory管理机制。由依赖注入完成,无需手动创建,它的创建和维护均由BeanFactory负责管理。 更优秀的Session管理机制。Spring对Session的管理是透明的,无须在代码中操作。 统一的事务管理。无论是编程式事务还是声明式事务,Spring都提供一致的编程模型。 24.3 Spring对Hibernate的简化 24.3.2 简化的具体表现 Spring对Hibernate的简化包括: 统一的异常处理机制。不再强制开发者在持久层捕捉异常,持久层异常被包装成DataAccessException,底层数据库异常包装成业务异常。开发者可以自己决定在合适的层处理。 HibernateTemplate支持类。可以完成大量Hibernate持久层的操作。 24.3 Spring对Hibernate的简化 24.3.3 HibernateTemplate概述 Spring提供了org.springframework.orm.hibernate3.HibernateTemplate类和org.springframework.orm.hibernate3.HibernateCallback接口来方便和Hibernate整合。 HibernateTemplate封装Hibernate的主要类,它提供了很多方便的操作数据的方法。 24.3 Spring对Hibernate的简化 24.3.3 HibernateTemplate概述 HibernateTemplate可将Hibernate的持久层访问模板化。创建HibernateTemplate后,注入一个SessionFactory的引用,就可以执行相关操作了。 HibernateTemplate提供了3个构造函数 HibernateTemplate(SessionFactory sf) HibernateTemplate(SessionFactory sf, boolean allowCreate) 24.3 Spring对Hibernate的简化 24.3.3 HibernateTemplate的常用方法 HibernateTemplate提供了很多常用的数据访问方法(CRUD)。 另外,从Spring 2.0开始增加了对命名SQL查询的支持,也增加了对分页的支持。 24.3 Spring对Hibernate的简化 24.3.3 HibernateTemplate的常用方法 void delete(Object entity):删除指定的持久化实例 void deleteAll(Collection entities):删除集合内全部持久化类的实例 List find(String queryString):根据HQL查询字符串来返回实例集合 List findByNamedQuery(String queryName):根据命名查询返回实例集合 Object get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例 24.3 Spring对Hibernate的简化 24.3.3 HibernateTemplate的常用方法 Serializable save(Object entity):保存新的实例 void saveOrUpdate(Object entity):根据实例状态,选择保存或更新 void update(Object entity):更新实例的状态 void setMaxResults(int maxResults):设置分页的大小 24.3 Spring对Hibernate的简化 24.3.4 HibernateTemplate的复杂用法 HibernateTemplate还提供了一种更灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式。 HibernateTemplate的灵活访问方式是通过如下两个方法完成的: Object execute(HibernateCallback action) List execute(HibernateCallback action) 开发者通过HibernateCallback,可以完全使用Hibernate灵活的方式来访问数据库,解决了Spring封装Hibernate后灵活性不足的缺陷。 24.3 Spring对Hibernate的简化 24.3.4 HibernateTemplate的复杂用法 HibernateCallback是一个接口,位于org.springframework.orm.hibernate3中。 该接口中只有一个方法doInHibernate(Session session)。 通常,程序中采用实现HibernateCallback的匿名内部类来获取HibernateCallback的实例,方法doInHibernate()就是Spring执行的持久化操作。 24.3 Spring对Hibernate的简化 24.3.5 HibernateDaoSupport Spring为与Hibernate进行整合,提供了一个工具类HibernateDaoSupport HibernateDaoSupport提供了基于AOP事务的自动处理,程序员完全可以不用理会事务的开始与提交,它会自动完成SessionFactory的注入和事务的注入。 24.3 Spring对Hibernate的简化 24.3.5 HibernateDaoSupport HibernateDaoSupport类提供的主要方法: public final HibernateTemplate getHibernateTemplate() public final void setSessionFactory(SessionFactory sf) 思考题 1. Spring中是怎么对Hibernate进行支持的? 2. 如何进行Spring与Hibernate的整合?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值