SSH2的泛型DAO

1、首先定义泛型DAO的接口。
package com.ys.common.dao;

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

public interface IBaseDao<T, ID extends Serializable> {

/**
* 保存实体
*
* @param entity
* 实体类
*/
public void save(T entity);

/**
* 删除实体
*
* @param entity
* 实体类
*/
public void delete(T entity);

/**
* 根据实体id 删除实体
*
* @param entityClass
* 实体类
* @param id
* 实体id
*/
public void deleteById(Class<T> entityClass, ID id);

/**
* 更新实体
*
* @param entity
* 实体类
*/
public void update(T entity);

/**
* 根据实体id 查询单个实体
*
* @param entityClass
* 实体类
* @param id
* 实体id
* @return
*/
public T findById(Class<T> entityClass, ID id);

/**
* 列出所有实体集合
*
* @param entityClass
* 实体类
* @return 实体类List
*/
public List<T> findAll(Class<T> entityClass);

/**
* 根据实体参数,查询符合条件的实体类集合
*
* @param hql
* @param values
* 参数
* @return
*/
public List<Object> find(String hql, Object... values);

}


2、给出基于HibernateDaoSupport类的具体实现。
package com.ys.common.dao;

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

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseHibernateDao<T, ID extends Serializable> extends
HibernateDaoSupport implements IBaseDao<T, ID> {

public void delete(T entity) {
this.getHibernateTemplate().delete(entity);
}

public void deleteById(Class<T> entityClass, ID id) {
delete(this.findById(entityClass, id));
}

@SuppressWarnings("unchecked")
public T findById(Class<T> entityClass, ID id) {
return (T) this.getHibernateTemplate().get(entityClass, id);
}

@SuppressWarnings("unchecked")
public List<T> findAll(Class<T> entityClass) {
String name = entityClass.getName();
return this.getHibernateTemplate().find("from " + name);
}

public void save(T entity) {
this.getHibernateTemplate().save(entity);
}

public void update(T entity) {
this.getHibernateTemplate().update(entity);
}

@SuppressWarnings("unchecked")
public List<Object> find(String hql, Object... values) {
return this.getHibernateTemplate().find(hql, values);
}

}


3、在web.xml里注册Spring的OpenSessionInViewFilter。
	<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>


4、继承基础DAO类即可重用DAO类方法。
package com.ys.system.dept.dao;

import com.ys.common.dao.BaseHibernateDao;
import com.ys.system.dept.bean.SysDept;

public class DeptDao extends BaseHibernateDao<SysDept, Integer> {

}


具体的调用方式如下:
package com.ys.system.dept.service;

import java.util.List;

import com.ys.system.dept.bean.SysDept;
import com.ys.system.dept.dao.DeptDao;

public class DeptService implements IDeptService {

//Spring容器依赖注入
private DeptDao deptDao;

public DeptDao getDeptDao() {
return deptDao;
}

public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}

public void delete(SysDept dept) {
deptDao.delete(dept);
}

public SysDept findById(int deptId) {
return deptDao.findById(SysDept.class, deptId);
}

public void save(SysDept dept) {
deptDao.save(dept);
}

public void update(SysDept dept) {
deptDao.update(dept);
}

}


提供出DeptDao的Spring配置文件片段:
 <bean id="deptDao" class="com.ys.system.dept.dao.DeptDao" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="deptService" class="com.ys.system.dept.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值