package com.yys.dao.impl; java
import java.lang.reflect.ParameterizedType;
import java.util.List; session
import javax.annotation.Resource; ide
import org.hibernate.Query;
import org.hibernate.SessionFactory; this
import com.yys.dao.BaseDao;
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {
private Class<T> clazz;
@Resource
private SessionFactory sessionFactory;
protected BaseDaoImpl() {
ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
clazz=(Class<T>) type.getActualTypeArguments()[0];
} .net
@Override
public void saveEntity(T t) throws Exception {
this.sessionFactory.getCurrentSession().save(t);
} hibernate
@Override
public void deleteEntity(T t) throws Exception {
this.sessionFactory.getCurrentSession().delete(t);
} get
@Override
public void updateEntity(T t) throws Exception {
this.sessionFactory.getCurrentSession().update(t);
} it
@Override
public void saveOrUpdate(T t) throws Exception {
this.sessionFactory.getCurrentSession().saveOrUpdate(t);
} io
@Override
public void batchEntityByHql(String Hql, Object... objects)
throws Exception {
Query query=this.sessionFactory.getCurrentSession().createQuery(Hql);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
query.executeUpdate();
} class
@Override
public T getEntity(Integer id) {
return (T) this.sessionFactory.getCurrentSession().get(clazz,id);
}
@Override
public T loadEntity(Integer id) {
return (T) this.sessionFactory.getCurrentSession().load(clazz, id);
}
@Override
public List<T> findEntityByHQL(String Hql, Object... objects) {
Query query=this.sessionFactory.getCurrentSession().createQuery(Hql);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
return query.list();
}
}