1.基本DAO泛型接口
2.基本DAO泛型接口的实现
@SuppressWarnings("unchecked")
public T findById(ID id) {
return (T) hibernateTemplate.get(type, id);
}
public void modify(T entity) {
hibernateTemplate.update(entity);
}
public void remove(T entity) {
hibernateTemplate.delete(entity);
}
public void removeAll(Collection<T> entities) {
hibernateTemplate.deleteAll(entities);
}
@SuppressWarnings("unchecked")
public ID save(T entity) {
return (ID) hibernateTemplate.save(entity);
}
public int getTotalRows() {
String actualHql = "select count(*) "
+ hql.substring(hql.indexOf("from"));
return ((Long) this.hibernateTemplate.find(actualHql).get(0))
.intValue();
}
public int getPageSize(int size) {
// 最大页数
int pageSize;
// 实际每页数据条数
int actualSize;
// 总记录数
int totalRows = this.getTotalRows();
// 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数
actualSize = (size > totalRows) ? totalRows : size;
if (totalRows > 0) {
pageSize = (totalRows % size == 0) ? (totalRows / actualSize)
: (totalRows / actualSize + 1);
} else {
pageSize = 0;
}
return pageSize;
}
@SuppressWarnings("unchecked")
public List<T> findByPage(final int page, final int size) {
final int pageSize = this.getPageSize(size);
final int totalRows = this.getTotalRows();
return hibernateTemplate.executeFind(new HibernateCallback() {
public List<T> doInHibernate(Session session)
throws HibernateException, SQLException {
// 实际页码
int actualPage = (page > pageSize) ? pageSize : page;
// 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数
int actualSize = (size > totalRows) ? totalRows : size;
// 计算请求页码的第一条记录的索引值,如果
int startRow = (actualPage > 0) ? (actualPage - 1) * actualSize
: 0;
Query query = session.createQuery(hql);
// 设置第一条记录
query.setFirstResult(startRow);
query.setMaxResults(actualSize);
return (List<T>) query.list();
}
});
}
}
3.具体的DAO接口
4.具体DAO接口的实现
5.基本泛型服务接口
/**
* <p>
* 删除实体集合
* </p>
*
* @param entities :
* 实体
*/
public void removeAll(Collection<T> entities);
/**
* <p>
* 修改实体
* </p>
*
* @param entity :
* 实体
*/
public void modify(T entity);
/**
* <p>
* 通过名字查找
* </p>
*
* @param id :
* id
* @return 找到的实体
*/
public T findById(ID id);
/**
* <p>
* 查找全部实体
* <p>
*
* @return 所有实体的列表
*/
public List<T> findAll();
/**
* <p>
* 根据给定的hql语句进行分页查找
* <p>
*
* @param page :
* 要查询的页码
* @param size :
* 每页记录条数
* @return 匹配的实体列表
*/
public List<T> findByPage(final int page, final int size);
/**
* <p>
* 计算匹配查询条件的记录总数,如果没有注入或者设置hql语句,将使用默认的查询语句返回数据库中所有记录
* </p>
*
* @return 记录总数
*/
public int getTotalRows();
/**
* <p>
* 根据每页记录的数量,计算出总的分页数
* </p>
*
* @param size
* 每页记录的数量
* @return 分页总数
*/
public int getPageSize(int size);
}
6.基本泛型服务接口的实现
public List<T> findAll() {
return genericDao.findAll();
}
public T findById(ID id) {
return genericDao.findById(id);
}
public List<T> findByPage(int page, int size) {
return genericDao.findByPage(page, size);
}
public int getPageSize(int size) {
return genericDao.getPageSize(size);
}
public int getTotalRows() {
return genericDao.getTotalRows();
}
public void modify(T entity) {
genericDao.modify(entity);
}
public void remove(T entity) {
genericDao.remove(entity);
}
public void removeAll(Collection<T> entities) {
genericDao.removeAll(entities);
}
public ID save(T entity) {
return genericDao.save(entity);
}
public void setGenericDao(GenericDao<T, ID> genericDao) {
this.genericDao = genericDao;
}
}
7.具体的服务接口
public void removeByUids(long [] uids);
}
8.具体的服务接口的实现
return user;
}
public void removeByUids(long[] uids) {
StringBuffer hqlSb = new StringBuffer();
hqlSb.append(" from User u where u.uid in (");
for (int i = 0; i < uids.length; i++) {
if (i != uids.length - 1) {
hqlSb.append(uids[i]).append(",");
} else {
hqlSb.append(uids[i]);
}
}
hqlSb.append(")");
userDao.setHql(hqlSb.toString());
Collection<User> entities = userDao.findAll();
if (entities != null) {
userDao.removeAll(entities);
}
}
}
9.分页数据信息类
10.分页工具类,用来生成客户端的分页代码
11.分页效果图