1. BaseDao


       

import java.util.List;


public interface BaseDao<T> {

public void save(T t);


public void update(T t);


public void delete(Integer id);


public T findById(Integer id);


public List<T> findByHQL(String hql, Object... params);


}

2.BaseDaoImpl

    

import java.lang.reflect.ParameterizedType;

import java.util.List;


import javax.annotation.Resource;


import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;


import com.medicinemanage.dao.BaseDao;


public class BaseDaoImpl<T> implements BaseDao<T> {

private Class<T> clazz;


@SuppressWarnings("unchecked")

public BaseDaoImpl() {

ParameterizedType type = (ParameterizedType) this.getClass()

.getGenericSuperclass();

clazz = (Class<T>) type.getActualTypeArguments()[0];

System.out.println("DAO的真实实现类是:" + this.clazz.getName());

}


@Resource

private SessionFactory sessionFactory;


// 获取当前session

protected Session getSession() {

return this.sessionFactory.getCurrentSession();

}


@Override

public void save(T t) {

getSession().save(t);

}


@Override

public void update(T t) {

getSession().update(t);

}


@Override

public void delete(Integer id) {

getSession().delete(this.findById(id));

}


@SuppressWarnings("unchecked")

@Override

public T findById(Integer id) {

return (T) getSession().get(this.clazz, id);

}


@SuppressWarnings("unchecked")

@Override

public List<T> findByHQL(String hql, Object... params) {

Query query = getSession().createQuery(hql);

for (int i = 0; params != null && i < params.length; i++) {

query.setParameter(i, params[i]);

}

return query.list();

}


}

3.BaseService

    


import java.util.List;


public interface BaseService<T> {

public void save(T entity);


public void update(T entity);


public void delete(Integer id);


public T getById(Integer id);


public List<T> getByHQL(String hql, Object... params);

}

    


4.BaseServiceImpl

    

import java.util.List;


import javax.annotation.Resource;


import org.springframework.transaction.annotation.Transactional;


import com.medicinemanage.dao.BaseDao;

import com.medicinemanage.service.BaseService;


@Transactional

public class BaseServiceImpl<T> implements BaseService<T> {

// 注入BaseDao

private BaseDao<T> dao;


@Resource

public void setDao(BaseDao<T> dao) {

this.dao = dao;

}


@Override

public void save(T entity) {

dao.save(entity);

}


@Override

public void update(T entity) {

dao.update(entity);

}


@Override

public void delete(Integer id) {

dao.delete(id);

}


@Override

public T getById(Integer id) {

return dao.findById(id);

}


@Override

public List<T> getByHQL(String hql, Object... params) {

return dao.findByHQL(hql, params);

}


}

5.具体以Breakage对象来实例化

    a.BreakageDao

         

    import com.medicinemanage.model.Breakage;

    

    public interface BreakageDao  extends BaseDao<Breakage> {

    

    }

   

    

    

    b.BreakageDaoImpl

    

    

     import org.springframework.stereotype.Repository;

    

    import com.medicinemanage.dao.BreakageDao;

    import com.medicinemanage.model.Breakage;

    @Repository("breakageDao")

    public class BreakageDaoImpl extends BaseDaoImpl<Breakage> implements BreakageDao{

    

    }

    c.BreakageService

    

    import com.medicinemanage.model.Breakage;

    

    public interface BreakageService extends BaseService<Breakage> {

    

    }

    d.breakageServiceImpl

    

    import javax.annotation.Resource;

    

    import org.springframework.stereotype.Repository;

    import org.springframework.transaction.annotation.Transactional;

    

    import com.medicinemanage.dao.BaseDao;

    import com.medicinemanage.model.Breakage;

    import com.medicinemanage.service.BreakageService;

    @Repository("breakageService")

    @Transactional

    public class BreakageServiceImpl extends BaseServiceImpl<Breakage> implements BreakageService {

     //注入dao

     @Resource(name="breakageDao")

     public void setDao(BaseDao<Breakage> breakageDao) {

     super.setDao(breakageDao);

    

     }

    }