关于BaseDAO要注意的问题

、引用茶叶写的一个baseDAO

package com.juno.dao.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

/**
* 所有Dao类的父类
*
* @author macrotea 2011-2-31
* @param <T>
*/

@Component("baseDao")
public class BaseDao<T, ID> extends HibernateDaoSupport {
private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);

/**
* 设置Dao将操作的实体
*/
private Class<T> entityClazz;

public Class<T> getEntityClazz() {
return entityClazz;
}

public void setEntityClazz(Class<T> entityClazz) {
this.entityClazz = entityClazz;
}

public BaseDao() {
super();
}

public BaseDao(Class<T> entityClazz) {
this.entityClazz = entityClazz;
}

/**
* 设置hibernateTemplate所需的SessionFactory
*/
@Resource
public void setLocalSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

/**
* 获得所有实体记录
*/
@SuppressWarnings("unchecked")
public List<T> findAll() throws Exception {
return getHibernateTemplate().find("FROM " + entityClazz.getSimpleName());
}

/**
* 保存一个实体
*/
@SuppressWarnings("unchecked")
public ID save(T obj) throws Exception {
return (ID) getHibernateTemplate().save(obj);
}

/**
* 保存/更新一个实体
*/
public void saveOrUpdate(T obj) throws Exception {
getHibernateTemplate().saveOrUpdate(obj);
}

/**
* 根据id获得一个实体
*/
@SuppressWarnings("unchecked")
public T findById(int id) throws Exception {
T retVal = (T) getHibernateTemplate().get(entityClazz, id);
if (retVal == null) {
logger.debug("get:该ID:" + id + " 的实体不存在!");
}
return retVal;
}

/**
* 根据id加载一个实体
*/
@SuppressWarnings("unchecked")
public T loadById(int id) throws Exception {
T retVal = (T) getHibernateTemplate().load(entityClazz, id);
return retVal;
}

/**
* 根据id删除一个实体
*/
public void deleteById(int id) throws Exception {
T entity = loadById(id);
if (entity == null) {
String msg = "delete:该ID:" + id + " 的实体不存在!";
throw new Exception(msg);
}
getHibernateTemplate().delete(entity);
}

/**
* 获得所有实体记录总数
*/
@SuppressWarnings("unchecked")
public Long countAll() throws Exception {
List retVal = getHibernateTemplate().find("SELECT count(*) FROM " + entityClazz.getSimpleName());
return (Long) retVal.get(0);
}

/**
* 根据属性和属性值查找
*/
@SuppressWarnings("unchecked")
public List findByProperty(String propertyName, Object value) throws Exception {
System.out.println("findByProperty value:" + value);
List retVal = getHibernateTemplate().find("from " + entityClazz.getSimpleName() + " as model where model." + propertyName + "= ?", value);
return retVal;
}

/**
* 根据多个id查找
*/
public List<T> findByIdArray(Integer[] idArray) throws Exception {
List<T> retVal = new ArrayList<T>();
T model = null;
for (int i = 0; i < idArray.length; i++) {
model = findById(idArray[i]);
retVal.add(model);
}
return retVal;
}
}


然后自己写一个dao类来继承basedao
package com.juno.dao.service.impl;

import org.springframework.stereotype.Component;

import com.juno.bean.Hello;
import com.juno.dao.service.BaseDao;
import com.juno.dao.service.HelloDAO;

/**
* @author Juno
*
*/
@Component("helloDao")
public class HelloDAOImpl extends BaseDao<Hello, Integer> implements HelloDAO {
// property constants
public static final String USERNAME = "username";
public static final String AGE = "age";

/*
* 如下方式调用父类方法:super.XXX()
*/
public HelloDAOImpl() {
super(Hello.class);
}

@Override
public void update(Hello Hello) throws Exception {

}
}
这时候、一定要记住加上
	public HelloDAOImpl() {
super(Hello.class);
}
这个构造方法!要不无效!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值