一、BaseDao模板
public interface BaseDao<T> {
public Serializable save(T entity);
public void delete(T entity);
public void update(T entity);
public List<T> find(T condition);
public T findById(Serializable id);
// 参数1:hqlString = from Users u where u.name like ? and u.telephone=?
// 参数2:new Object[]{name,telephone,...}
public List<T> getListByHQL(String hqlString, Object... values);
public List<T> queryListObjectAllForPage(int pageSize, int page,
String hqlString, Object... values);
//单值查询 select count()
public Object uniqueResult(String sqlString, Object... values);
public Object uniqueResultForPages(String hqlString,int pageSize,int page, Object... values);
}
二、BaseDaoImpl模板
@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {
//@Resource //官方的注解
@Autowired
private SessionFactory sessionFactory;
protected Class<T> entityClass;
public Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
protected Class getEntityClass() {
if (entityClass == null) {
entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
return entityClass;
}
public Serializable save(T entity) {
return this.getCurrentSession().save(entity);
}
public void delete(T entity) {
this.getCurrentSession().delete(entity);
}
public void update(T entity) {
this.getCurrentSession().update(entity);
}
public List<T> find(T condition) {
Criteria criteria = this.getCurrentSession().createCriteria(getEntityClass());
criteria.add(Example.create(condition));
return criteria.list();
}
public T findById(Serializable id) {
T load = (T) this.getCurrentSession().get(getEntityClass(), id);
return load;
}
/**
* <根据HQL语句,得到对应的list>
*
* @param hqlString
* HQL语句
* @param values
* 不定参数的Object数组
* @return 查询多个实体的List集合
* @see com.itv.launcher.util.IBaseDao#getListByHQL(java.lang.String,
* java.lang.Object[])
*/
public List<T> getListByHQL(String hqlString, Object... values) {
Query query = this.getCurrentSession().createQuery(hqlString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]); //设置占位符的参数
}
}
return query.list();
}
public List<T> queryListObjectAllForPage(int pageSize, int page, String hqlString, Object... values) {
Query query = this.getCurrentSession().createQuery(hqlString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
query.setFirstResult((page - 1) * pageSize);
query.setMaxResults(pageSize);
return query.list();
}
public Object uniqueResult(String hqlString, Object... values) {
Query query = this.getCurrentSession().createQuery(hqlString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query.uniqueResult();
}
public Object uniqueResultForPages(String hqlString, int pageSize,
int page, Object... values) {
Query query = this.getCurrentSession().createQuery(hqlString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
query.setFirstResult((page - 1) * pageSize);
query.setMaxResults(pageSize);
return query.uniqueResult();
}
}
三、继承
public interface UserDao extends BaseDao<User> {}
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {}