关键字: spring, hibernate, 通用dao
Hibernate+spring通用的dao.
首先定义一个SimpleDao接口,里面的方法如下:
- /*
- *
- * @author zyong
- *
- * @date 2009-9-19 下 午11:02:34
- *
- * @action 一个简单公共的hibernate通用dao实现类
- */
- package com.wind.dao;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.LockMode;
- import org.hibernate.criterion.DetachedCriteria;
- /**
- *
- * @author zyong
- *
- * @action 一 个通用的Hibernate的dao接口<br>
- * 数据访问层里面的每一个接口都应该继承这个接口<br>
- * 而不用重写里面的方法
- */
- public interface SimpleDao<E extends Serializable, PK extends Serializable> {
- /**
- *
- * 该枚举类型用于,getStackValue方法中的Stack枚举
- *
- */
- enum Stack {
- MAX, MIN, AVG, SUM;
- }
- /**
- *
- * @param id
- * 根据主键查询一个实体
- * @return 一个实体对象
- */
- E get(PK id);
- /**
- *
- * @param id
- * 根据主键查询一个实体
- * @param lock
- * 加锁实体
- * @return 一个实体对象
- */
- E get(PK id, LockMode lock);
- /**
- * 使 用数据库函数
- *
- * @param criteria
- * 一个DetacherCriteria对象
- * @param propertyName
- * 实体类属性名
- * @param stackName
- * Stack枚举类型中的任意一个
- * @return 一行一列数据库
- */
- Object getStackValue(DetachedCriteria criteria, String propertyName,
- String stackName);
- /**
- * 查 询数据库对应的记录数
- *
- * @param criteria
- * 一个DetachedCriteria对象
- * @return 记录数
- */
- Integer getRowCount(DetachedCriteria criteria);
- /**
- *
- * @param id
- * 根据主键加裁一个实体对象
- * @return 一个实体对象
- */
- E load(PK id);
- /**
- *
- * @param id
- * 根据主键加裁实体
- * @param lock
- * 加锁实体
- * @return 一个实体对象
- */
- E load(PK id, LockMode lock);
- /**
- *
- * @return 加 裁所有对象
- */
- List<E> loadAll();
- /**
- *
- * @param entity
- * 保存一个实体
- * @throws HibernateException
- * 抛出Exception异常
- */
- void save(E entity) throws HibernateException;
- /**
- *
- * @param entity
- * 删除一个实体
- * @throws HibernateException
- * 抛出异常
- */
- void delete(E entity) throws HibernateException;
- /**
- *
- * @param entity
- * 删除一个实体
- * @param lock
- * 加锁实体
- * @throws HibernateException
- * 抛出异常
- */
- void delete(E entity, LockMode lock) throws HibernateException;
- /**
- *
- * @param entitys
- * 删除多个实体
- * @throws HibernateException
- * 抛出异常
- */
- void delete(Collection<E> entitys) throws HibernateException;
- /**
- *
- * @param entity
- * 修改一个实体
- * @throws HibernateException
- * 抛出异常
- */
- void update(E entity) throws HibernateException;
- /**
- *
- * @param entity
- * 修改一个实体
- * @param lock
- * 加锁实体
- * @throws HibernateException
- * 抛出异常
- */
- void update(E entity, LockMode lock) throws HibernateException;
- /**
- *
- * @param entity
- * 当实体在数据库不在在与之对应记录时,则保存实体,在在实体,则更新实体
- * @throws HibernateException
- * 抛出异常
- */
- void saveOrUpdate(E entity) throws HibernateException;
- /**
- *
- * @param entitys
- * 保存多个实体
- * @throws HibernateException
- * 抛出异常
- */
- void saveOrUpdate(Collection<E> entitys) throws HibernateException;
- /*---------------------------利用hql,sql对数据库进行操作 --------------------------------*/
- /**
- *
- * @param hql
- * 使用hql语句进行数据库增删改操作
- * @return 受影响行的记录数
- */
- Integer bulkUpdate(String hql);
- /**
- *
- * @param hql
- * 使用hql语句进行数据库增删改操作
- * @param params
- * hql语句参数
- * @return 受影响行的记录数
- */
- Integer bulkUpdate(String hql, Object... values);
- /**
- *
- * @param hql
- * 使用hql语句,检索数据
- * @return 一个list集合
- */
- List<E> find(String hql);
- /**
- *
- * @param hql
- * 使用hql语句,检索数据
- * @param params
- * hql语句参数
- * @return 一个list集合
- */
- List<E> find(String hql, Object... values);
- /**
- *
- * @param queryName
- * 使用命名的hql语句进行查询
- * @return 一个list集合
- */
- List<E> findByNamedQuery(String queryName);
- /**
- *
- * @param queryName
- * 使用带参数的命名hql语句进行查询
- * @param values
- * 参数集合
- * @return 一个list集合
- */
- List<E> findByNamedQuery(String queryName, Object... values);
- /**
- *
- * @param queryName
- * 使用带参数的命名hql语句进行查询
- * @param params
- * 参数集合<br>
- * Map的键为参数名称即paramName<br>
- * Map的值则为values
- * @return 一个list集合
- */
- List<E> findByNamedParam(String queryName, Map<String, Object> params);
- /**
- *
- * @param queryName
- * 使用带参数的命名hql语句进行查询
- * @param params
- * 参数集合<br>
- * Map的键为参数名称即paramName<br>
- * Map的值则为values
- * @return 一个list集合
- */
- List<E> findByNamedQueryAndNamedParam(String queryName,
- Map<String, Object> params);
- /**
- *
- * @param criteria
- * 使 用指定的检索标准来检索数
- * @return 一个list集合
- */
- List<E> findByCriteria(DetachedCriteria criteria);
- /**
- *
- * @param criteria
- * 使用指定的检索标准来分页检索数据
- * @param firstResult
- * 开始条数
- * @param maxResults
- * 返回记录数
- * @return 一个list集合
- */
- List<E> findByCriteria(DetachedCriteria criteria, Integer firstResult,
- Integer maxResults);
- /**
- * 加 锁指定的实体
- *
- * @param entity
- * 实体对象
- *
- * @param lock
- * 加锁
- */
- void lock(E entity, LockMode lock) throws HibernateException;
- /**
- * 强 制立即更新到数据库,否则需要事务提交后,才会提交到数据库
- */
- void flush() throws HibernateException;
- /**
- *
- * @return 根据SimpleDao泛型类型,创建一个与会话无关的检索对象
- */
- DetachedCriteria createDetachedCriteria();
- /**
- *
- * @param c
- * 为一个实体类型
- * @return 根据指定的类型创建一个与会话无关的检索对象
- */
- DetachedCriteria createDetachedCriteria(Class<? extends Serializable> c);
- /**
- *
- * @return 创建与会话绑定的检索标准对象
- */
- Criteria createCriteria();
- }
/*
*
* @author zyong
*
* @date 2009-9-19 下午11:02:34
*
* @action 一个简单公共的hibernate通用dao实现类
*/
package com.wind.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
/**
*
* @author zyong
*
* @action 一个通用的Hibernate的dao接口<br>
* 数据访问层里面的每一个接口都应该继承这个接口<br>
* 而不用重写里面的方法
*/
public interface SimpleDao<E extends Serializable, PK extends Serializable> {
/**
*
* 该枚举类型用于,getStackValue方法中的Stack枚举
*
*/
enum Stack {
MAX, MIN, AVG, SUM;
}
/**
*
* @param id
* 根据主键查询一个实体
* @return 一个实体对象
*/
E get(PK id);
/**
*
* @param id
* 根据主键查询一个实体
* @param lock
* 加锁实体
* @return 一个实体对象
*/
E get(PK id, LockMode lock);
/**
* 使用数据库函数
*
* @param criteria
* 一个DetacherCriteria对象
* @param propertyName
* 实体类属性名
* @param stackName
* Stack枚举类型中的任意一个
* @return 一行一列数据库
*/
Object getStackValue(DetachedCriteria criteria, String propertyName,
String stackName);
/**
* 查询数据库对应的记录数
*
* @param criteria
* 一个DetachedCriteria对象
* @return 记录数
*/
Integer getRowCount(DetachedCriteria criteria);
/**
*
* @param id
* 根据主键加裁一个实体对象
* @return 一个实体对象
*/
E load(PK id);
/**
*
* @param id
* 根据主键加裁实体
* @param lock
* 加锁实体
* @return 一个实体对象
*/
E load(PK id, LockMode lock);
/**
*
* @return 加裁所有对象
*/
List<E> loadAll();
/**
*
* @param entity
* 保存一个实体
* @throws HibernateException
* 抛出Exception异常
*/
void save(E entity) throws HibernateException;
/**
*
* @param entity
* 删除一个实体
* @throws HibernateException
* 抛出异常
*/
void delete(E entity) throws HibernateException;
/**
*
* @param entity
* 删除一个实体
* @param lock
* 加锁实体
* @throws HibernateException
* 抛出异常
*/
void delete(E entity, LockMode lock) throws HibernateException;
/**
*
* @param entitys
* 删除多个实体
* @throws HibernateException
* 抛出异常
*/
void delete(Collection<E> entitys) throws HibernateException;
/**
*
* @param entity
* 修改一个实体
* @throws HibernateException
* 抛出异常
*/
void update(E entity) throws HibernateException;
/**
*
* @param entity
* 修改一个实体
* @param lock
* 加锁实体
* @throws HibernateException
* 抛出异常
*/
void update(E entity, LockMode lock) throws HibernateException;
/**
*
* @param entity
* 当实体在数据库不在在与之对应记录时,则保存实体,在在实体,则更新实体
* @throws HibernateException
* 抛出异常
*/
void saveOrUpdate(E entity) throws HibernateException;
/**
*
* @param entitys
* 保存多个实体
* @throws HibernateException
* 抛出异常
*/
void saveOrUpdate(Collection<E> entitys) throws HibernateException;
/*---------------------------利用hql,sql对数据库进行操作--------------------------------*/
/**
*
* @param hql
* 使用hql语句进行数据库增删改操作
* @return 受影响行的记录数
*/
Integer bulkUpdate(String hql);
/**
*
* @param hql
* 使用hql语句进行数据库增删改操作
* @param params
* hql语句参数
* @return 受影响行的记录数
*/
Integer bulkUpdate(String hql, Object... values);
/**
*
* @param hql
* 使用hql语句,检索数据
* @return 一个list集合
*/
List<E> find(String hql);
/**
*
* @param hql
* 使用hql语句,检索数据
* @param params
* hql语句参数
* @return 一个list集合
*/
List<E> find(String hql, Object... values);
/**
*
* @param queryName
* 使用命名的hql语句进行查询
* @return 一个list集合
*/
List<E> findByNamedQuery(String queryName);
/**
*
* @param queryName
* 使用带参数的命名hql语句进行查询
* @param values
* 参数集合
* @return 一个list集合
*/
List<E> findByNamedQuery(String queryName, Object... values);
/**
*
* @param queryName
* 使用带参数的命名hql语句进行查询
* @param params
* 参数集合<br>
* Map的键为参数名称即paramName<br>
* Map的值则为values
* @return 一个list集合
*/
List<E> findByNamedParam(String queryName, Map<String, Object> params);
/**
*
* @param queryName
* 使用带参数的命名hql语句进行查询
* @param params
* 参数集合<br>
* Map的键为参数名称即paramName<br>
* Map的值则为values
* @return 一个list集合
*/
List<E> findByNamedQueryAndNamedParam(String queryName,
Map<String, Object> params);
/**
*
* @param criteria
* 使用指定的检索标准来检索数
* @return 一个list集合
*/
List<E> findByCriteria(DetachedCriteria criteria);
/**
*
* @param criteria
* 使用指定的检索标准来分页检索数据
* @param firstResult
* 开始条数
* @param maxResults
* 返回记录数
* @return 一个list集合
*/
List<E> findByCriteria(DetachedCriteria criteria, Integer firstResult,
Integer maxResults);
/**
* 加锁指定的实体
*
* @param entity
* 实体对象
*
* @param lock
* 加锁
*/
void lock(E entity, LockMode lock) throws HibernateException;
/**
* 强制立即更新到数据库,否则需要事务提交后,才会提交到数据库
*/
void flush() throws HibernateException;
/**
*
* @return 根据SimpleDao泛型类型,创建一个与会话无关的检索对象
*/
DetachedCriteria createDetachedCriteria();
/**
*
* @param c
* 为一个实体类型
* @return 根据指定的类型创建一个与会话无关的检索对象
*/
DetachedCriteria createDetachedCriteria(Class<? extends Serializable> c);
/**
*
* @return 创建与会话绑定的检索标准对象
*/
Criteria createCriteria();
}
SimpleDaoImpl为一个SimpleDao的实现类.代码如下:
- /*
- *
- * @author zyong
- *
- * @date 2009-9-19 下 午11:02:34
- *
- * @action 一个简单公共的hibernate通用dao实现类
- */
- package com.wind.dao.impl;
- import java.io.Serializable;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.LockMode;
- import org.hibernate.criterion.DetachedCriteria;
- import org.hibernate.criterion.Projections;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.wind.dao.SimpleDao;
- /**
- *
- * @author zyong
- *
- * @action 一 个公共的Hibernate通用dao实现类<br>
- * 数据库访问层,每一个实现类都应该来继承该类<br>
- * 不应该重写里面的方法,需要相应的方法,直接到数据访问层每个类对应的接口中添加
- */
- public abstract class SimpleDaoImpl<E extends Serializable, PK extends Serializable>
- extends HibernateDaoSupport implements SimpleDao<E, PK> {
- /**
- * 为 E对应的实例类型
- */
- private Class<?> entityClass;
- /**
- * 获 取E实例类的类型
- */
- public SimpleDaoImpl() {
- Class<?> c = this .getClass();
- Type t = c.getGenericSuperclass();
- if (t instanceof ParameterizedType) {
- this .entityClass = (Class<?>) ((ParameterizedType) t)
- .getActualTypeArguments()[0 ];
- }
- }
- @SuppressWarnings ( "unchecked" )
- public E get(PK id) {
- return (E) this .getHibernateTemplate().get( this .entityClass, id);
- }
- @SuppressWarnings ( "unchecked" )
- public E get(PK id, LockMode lock) {
- E entity = (E) this .getHibernateTemplate().get( this .entityClass, id,
- lock);
- if (entity != null ) {
- this .flush(); // 如 果实体不为null,立即刷新,否则锁不会生效
- }
- return entity;
- }
- public Object getStackValue(DetachedCriteria criteria, String propertyName,
- Stack value) {
- switch (value) {
- case MAX:
- criteria.setProjection(Projections.max(propertyName));
- break ;
- case MIN:
- criteria.setProjection(Projections.min(propertyName));
- break ;
- case AVG:
- criteria.setProjection(Projections.avg(propertyName));
- break ;
- default :
- criteria.setProjection(Projections.sum(propertyName));
- break ;
- }
- return this .findByCriteria(criteria, 0 , 1 ).get( 0 );
- }
- public Integer getRowCount(DetachedCriteria criteria) {
- criteria.setProjection(Projections.rowCount());
- return (Integer) this .findByCriteria(criteria, 0 , 1 ).get( 0 );
- }
- @SuppressWarnings ( "unchecked" )
- public E load(PK id) {
- return (E) this .getHibernateTemplate().load( this .entityClass, id);
- }
- @SuppressWarnings ( "unchecked" )
- public E load(PK id, LockMode lock) {
- E entity = (E) this .getHibernateTemplate().load( this .entityClass, id,
- lock);
- if (entity != null ) {
- this .flush(); // 如 果实体不为null,立即刷新,否则锁不会生效
- }
- return entity;
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> loadAll() {
- return this .getHibernateTemplate().loadAll(entityClass);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> find(String hql) {
- return this .getHibernateTemplate().find(hql);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> find(String hql, Object... values) {
- return this .getHibernateTemplate().find(hql, values);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByNamedQuery(String queryName, Object... values) {
- return this .getHibernateTemplate().findByNamedQuery(queryName, values);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByNamedQuery(String queryName) {
- return this .getHibernateTemplate().findByNamedQuery(queryName);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByNamedQueryAndNamedParam(String queryName,
- Map<String, Object> params) {
- return this .getHibernateTemplate().findByNamedQueryAndNamedParam(
- queryName, (String[]) params.keySet().toArray(),
- params.values().toArray());
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByNamedParam(String queryName, Map<String, Object> params) {
- return this .getHibernateTemplate()
- .findByNamedParam(queryName,
- (String[]) params.keySet().toArray(),
- params.values().toArray());
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByCriteria(DetachedCriteria criteria) {
- return this .getHibernateTemplate().findByCriteria(criteria);
- }
- @SuppressWarnings ( "unchecked" )
- public List<E> findByCriteria(DetachedCriteria criteria,
- Integer firstResult, Integer maxResults) {
- return this .getHibernateTemplate().findByCriteria(criteria,
- firstResult, maxResults);
- }
- public void save(E entity) throws HibernateException {
- this .getHibernateTemplate().save(entity);
- }
- public void saveOrUpdate(E entity) throws HibernateException {
- this .getHibernateTemplate().saveOrUpdate(entity);
- }
- public void saveOrUpdate(Collection<E> entitys) throws HibernateException {
- this .getHibernateTemplate().saveOrUpdateAll(entitys);
- }
- public void delete(E entity) throws HibernateException {
- this .getHibernateTemplate().delete(entity);
- }
- public void delete(E entity, LockMode lock) throws HibernateException {
- this .getHibernateTemplate().delete(entity, lock);
- this .flush(); // 如 果实体不为null,立即刷新,否则锁不会生效
- }
- public void delete(Collection<E> entitys) throws HibernateException {
- this .getHibernateTemplate().deleteAll(entitys);
- }
- public void update(E entity) throws HibernateException {
- this .getHibernateTemplate().update(entity);
- }
- public void update(E entity, LockMode lock) throws HibernateException {
- this .getHibernateTemplate().update(entity, lock);
- this .flush(); // 如 果实体不为null,立即刷新,否则锁不会生效
- }
- public Integer bulkUpdate(String hql) {
- return this .getHibernateTemplate().bulkUpdate(hql);
- }
- public Integer bulkUpdate(String hql, Object... values) {
- return this .getHibernateTemplate().bulkUpdate(hql, values);
- }
- public void flush() throws HibernateException {
- this .getHibernateTemplate().flush();
- }
- public void lock(E entity, LockMode lock) throws HibernateException {
- this .getHibernateTemplate().lock(entity, lock);
- }
- public DetachedCriteria createDetachedCriteria() {
- return DetachedCriteria.forClass( this .entityClass);
- }
- public DetachedCriteria createDetachedCriteria(
- Class<? extends Serializable> c) {
- return DetachedCriteria.forClass(c);
- }
- public Criteria createCriteria() {
- return this .createDetachedCriteria().getExecutableCriteria(
- this .getSession());
- }
- }
/*
*
* @author zyong
*
* @date 2009-9-19 下午11:02:34
*
* @action 一个简单公共的hibernate通用dao实现类
*/
package com.wind.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wind.dao.SimpleDao;
/**
*
* @author zyong
*
* @action 一个公共的Hibernate通用dao实现类<br>
* 数据库访问层,每一个实现类都应该来继承该类<br>
* 不应该重写里面的方法,需要相应的方法,直接到数据访问层每个类对应的接口中添加
*/
public abstract class SimpleDaoImpl<E extends Serializable, PK extends Serializable>
extends HibernateDaoSupport implements SimpleDao<E, PK> {
/**
* 为E对应的实例类型
*/
private Class<?> entityClass;
/**
* 获取E实例类的类型
*/
public SimpleDaoImpl() {
Class<?> c = this.getClass();
Type t = c.getGenericSuperclass();
if (t instanceof ParameterizedType) {
this.entityClass = (Class<?>) ((ParameterizedType) t)
.getActualTypeArguments()[0];
}
}
@SuppressWarnings("unchecked")
public E get(PK id) {
return (E) this.getHibernateTemplate().get(this.entityClass, id);
}
@SuppressWarnings("unchecked")
public E get(PK id, LockMode lock) {
E entity = (E) this.getHibernateTemplate().get(this.entityClass, id,
lock);
if (entity != null) {
this.flush();// 如果实体不为null,立即刷新,否则锁不会生效
}
return entity;
}
public Object getStackValue(DetachedCriteria criteria, String propertyName,
Stack value) {
switch (value) {
case MAX:
criteria.setProjection(Projections.max(propertyName));
break;
case MIN:
criteria.setProjection(Projections.min(propertyName));
break;
case AVG:
criteria.setProjection(Projections.avg(propertyName));
break;
default:
criteria.setProjection(Projections.sum(propertyName));
break;
}
return this.findByCriteria(criteria, 0, 1).get(0);
}
public Integer getRowCount(DetachedCriteria criteria) {
criteria.setProjection(Projections.rowCount());
return (Integer) this.findByCriteria(criteria, 0, 1).get(0);
}
@SuppressWarnings("unchecked")
public E load(PK id) {
return (E) this.getHibernateTemplate().load(this.entityClass, id);
}
@SuppressWarnings("unchecked")
public E load(PK id, LockMode lock) {
E entity = (E) this.getHibernateTemplate().load(this.entityClass, id,
lock);
if (entity != null) {
this.flush();// 如果实体不为null,立即刷新,否则锁不会生效
}
return entity;
}
@SuppressWarnings("unchecked")
public List<E> loadAll() {
return this.getHibernateTemplate().loadAll(entityClass);
}
@SuppressWarnings("unchecked")
public List<E> find(String hql) {
return this.getHibernateTemplate().find(hql);
}
@SuppressWarnings("unchecked")
public List<E> find(String hql, Object... values) {
return this.getHibernateTemplate().find(hql, values);
}
@SuppressWarnings("unchecked")
public List<E> findByNamedQuery(String queryName, Object... values) {
return this.getHibernateTemplate().findByNamedQuery(queryName, values);
}
@SuppressWarnings("unchecked")
public List<E> findByNamedQuery(String queryName) {
return this.getHibernateTemplate().findByNamedQuery(queryName);
}
@SuppressWarnings("unchecked")
public List<E> findByNamedQueryAndNamedParam(String queryName,
Map<String, Object> params) {
return this.getHibernateTemplate().findByNamedQueryAndNamedParam(
queryName, (String[]) params.keySet().toArray(),
params.values().toArray());
}
@SuppressWarnings("unchecked")
public List<E> findByNamedParam(String queryName, Map<String, Object> params) {
return this.getHibernateTemplate()
.findByNamedParam(queryName,
(String[]) params.keySet().toArray(),
params.values().toArray());
}
@SuppressWarnings("unchecked")
public List<E> findByCriteria(DetachedCriteria criteria) {
return this.getHibernateTemplate().findByCriteria(criteria);
}
@SuppressWarnings("unchecked")
public List<E> findByCriteria(DetachedCriteria criteria,
Integer firstResult, Integer maxResults) {
return this.getHibernateTemplate().findByCriteria(criteria,
firstResult, maxResults);
}
public void save(E entity) throws HibernateException {
this.getHibernateTemplate().save(entity);
}
public void saveOrUpdate(E entity) throws HibernateException {
this.getHibernateTemplate().saveOrUpdate(entity);
}
public void saveOrUpdate(Collection<E> entitys) throws HibernateException {
this.getHibernateTemplate().saveOrUpdateAll(entitys);
}
public void delete(E entity) throws HibernateException {
this.getHibernateTemplate().delete(entity);
}
public void delete(E entity, LockMode lock) throws HibernateException {
this.getHibernateTemplate().delete(entity, lock);
this.flush();// 如果实体不为null,立即刷新,否则锁不会生效
}
public void delete(Collection<E> entitys) throws HibernateException {
this.getHibernateTemplate().deleteAll(entitys);
}
public void update(E entity) throws HibernateException {
this.getHibernateTemplate().update(entity);
}
public void update(E entity, LockMode lock) throws HibernateException {
this.getHibernateTemplate().update(entity, lock);
this.flush();// 如果实体不为null,立即刷新,否则锁不会生效
}
public Integer bulkUpdate(String hql) {
return this.getHibernateTemplate().bulkUpdate(hql);
}
public Integer bulkUpdate(String hql, Object... values) {
return this.getHibernateTemplate().bulkUpdate(hql, values);
}
public void flush() throws HibernateException {
this.getHibernateTemplate().flush();
}
public void lock(E entity, LockMode lock) throws HibernateException {
this.getHibernateTemplate().lock(entity, lock);
}
public DetachedCriteria createDetachedCriteria() {
return DetachedCriteria.forClass(this.entityClass);
}
public DetachedCriteria createDetachedCriteria(
Class<? extends Serializable> c) {
return DetachedCriteria.forClass(c);
}
public Criteria createCriteria() {
return this.createDetachedCriteria().getExecutableCriteria(
this.getSession());
}
}
数据访问层里面的每一个接口都继承SimpleDao接口,每一个接口的实现类都继承SimpleDaoImpl类.