- 1. 通用数据库访问层BaseDao
- 1.1 接口BaseDao
- 1.2 实现类BaseDaoImpl
- 2. 通用业务层BaseService
- 2.1 接口BaseService
- 2.2 实现类BaseServiceImpl
- 3. 分页信息类Page
- 4. 条件分页查询例子
- 4.1 BookDao接口继承BaseDao接口
- 4.2 BookDaoImpl实现BookDao接口并继承BaseDaoImpl
- 4.3 BookService接口继承BaseService接口
- 4.4 BookServiceImpl实现BookService接口并继承BaseServiceImpl
- 4.5 BookAction
1. 通用数据库访问层BaseDao
1.1 接口BaseDao
package com.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.SimpleExpression;
import com.domain.Page;
public interface BaseDao<T> {
void save(T t);
void update(T t);
void delete(T t);
T findById(Serializable id);
List<T> findAll();
List<T> findByCriteria(DetachedCriteria detachedCriteria);
int findCount(DetachedCriteria detachedCriteria);
int findCount(SimpleExpression... conditions);
List<T> findByPage(Page page,DetachedCriteria detachedCriteria);
List<T> findByPage(Page page,SimpleExpression... conditions);
int findCount(String hql,Object... paramValues);
List<T> findByPage(Page page,String hql,Object... paramValues);
}
1.2 实现类BaseDaoImpl
package com.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import com.dao.BaseDao;
import com.domain.Page;
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{
private Class clazz;
public BaseDaoImpl() {
Class clazz = this.getClass();
Type type = clazz.getGenericSuperclass();
ParameterizedType pType = (ParameterizedType) type;
Type[] types = pType.getActualTypeArguments();
this.clazz = (Class) types[0];
}
@Override
public void save(T t) {
getHibernateTemplate().save(t);
}
@Override
public void update(T t) {
getHibernateTemplate().update(t);
}
@Override
public void delete(T t) {
getHibernateTemplate().delete(t);
}
@Override
public T findById(Serializable id) {
return (T) getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll() {
return (List<T>) getHibernateTemplate().find("from "+clazz.getSimpleName());
}
public DetachedCriteria getConditionCriteria(SimpleExpression... conditions){
DetachedCriteria dc=DetachedCriteria.forClass(clazz);
for (SimpleExpression se : conditions) {
if(se.getValue()==null)continue;
String className=se.getValue().getClass().getSimpleName();
if(className.equals("String")||className.equals("Character")){
if(!se.getValue().toString().matches("%?\\s*%?")){
dc.add(se);
}
}else{
dc.add(se);
}
}
return dc;
}
@Override
public List<T> findByCriteria(DetachedCriteria detachedCriteria) {
return (List<T>) getHibernateTemplate().findByCriteria(detachedCriteria);
}
@Override
public int findCount(DetachedCriteria detachedCriteria) {
detachedCriteria.setProjection(Projections.rowCount());
List<Long> list= (List<Long>) getHibernateTemplate().findByCriteria(detachedCriteria);
detachedCriteria.setProjection(null);
return list.size()>0?list.get(0).intValue():0;
}
@Override
public int findCount(SimpleExpression... conditions) {
return findCount(getConditionCriteria(conditions));
}
@Override
public List<T> findByPage(Page page,DetachedCriteria detachedCriteria) {
return (List<T>) getHibernateTemplate().findByCriteria(detachedCriteria, page.getBeginIndex(), page.getPageSize());
}
@Override
public List<T> findByPage(Page page, SimpleExpression... conditions) {
return findByPage(page, getConditionCriteria(conditions));
}
@Override
public List<T> findByPage(final Page page,final String hql,final Object... paramValues) {
return getHibernateTemplate().execute(new HibernateCallback<List<T>>() {
@Override
public List<T> doInHibernate(Session session) throws HibernateException {
Query query =session.createQuery(hql);
query.setFirstResult(page.getBeginIndex());
query.setMaxResults(page.getPageSize());
for(int i=0;i<paramValues.length;i++){
query.setParameter(i, paramValues[i]);
}
return query.list();
}
});
}
@Override
public int findCount(String hql,Object... paramValues) {
List<Long> list=(List<Long>)getHibernateTemplate().find(hql, paramValues);
return list.size()>0?list.get(0).intValue():0;
}
}
2. 通用业务层BaseService
2.1 接口BaseService
package com.service;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.SimpleExpression;
import com.domain.Page;
public interface BaseService<T> {
void save(T t);
void update(T t);
void delete(T t);
T findById(Serializable id);
List<T> findAll();
List<T> findByCriteria(DetachedCriteria detachedCriteria);
int findCount(DetachedCriteria detachedCriteria);
int findCount(SimpleExpression... conditions);
List<T> findByPage(Page page,DetachedCriteria detachedCriteria);
List<T> findByPage(Page page,SimpleExpression... conditions);
int findCount(String hql,Object... paramValues);
List<T> findByPage(Page page,String hql,Object... paramValues);
}
2.2 实现类BaseServiceImpl
package com.service.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.SimpleExpression;
import com.dao.BaseDao;
import com.domain.Page;
import com.service.BaseService;
public class BaseServiceImpl<T> implements BaseService<T> {
private BaseDao<T> baseDao;
public void setBaseDao(BaseDao<T> baseDao) {
this.baseDao = baseDao;
}
@Override
public void save(T t) {
baseDao.save(t);
}
@Override
public void update(T t) {
baseDao.update(t);
}
@Override
public void delete(T t) {
baseDao.delete(t);
}
@Override
public T findById(Serializable id) {
return baseDao.findById(id);
}
@Override
public List findAll() {
return baseDao.findAll();
}
@Override
public List findByCriteria(DetachedCriteria detachedCriteria) {
return baseDao.findByCriteria(detachedCriteria);
}
@Override
public int findCount(DetachedCriteria detachedCriteria) {
return baseDao.findCount(detachedCriteria);
}
@Override
public int findCount(SimpleExpression... conditions) {
return baseDao.findCount(conditions);
}
@Override
public List findByPage(Page page, DetachedCriteria detachedCriteria) {
return baseDao.findByPage(page, detachedCriteria);
}
@Override
public List findByPage(Page page, SimpleExpression... conditions) {
return baseDao.findByPage(page, conditions);
}
@Override
public int findCount(String hql,Object... paramValues) {
return baseDao.findCount(hql,paramValues);
}
@Override
public List findByPage(Page page, String hql, Object... paramValues) {
return baseDao.findByPage(page, hql, paramValues);
}
}
3. 分页信息类Page
package com.domain;
public class Page {
private int currentPage;
private int pageSize;
private int totalCount;
private int totalPage;
private int beginIndex;
public final static int PAGESIZE_DEFAULT=10;
public final static int PAGESIZE_MIN=1;
public final static int PAGESIZE_MAX=100;
public int getCurrentPage() {
return currentPage<1?1:currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
if(pageSize<PAGESIZE_MIN||pageSize>PAGESIZE_MAX){
return PAGESIZE_DEFAULT;
}else{
return pageSize;
}
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage<1?0:(int)Math.ceil(totalCount/getPageSize());
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getBeginIndex() {
return (currentPage-1)*pageSize;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public Page() {
super();
}
public Page(int currentPage, int pageSize) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
}
}
4. 条件分页查询例子
4.1 BookDao接口继承BaseDao接口
package com.dao;
import com.domain.Book;
public interface BookDao extends BaseDao<Book>{
}
4.2 BookDaoImpl实现BookDao接口并继承BaseDaoImpl
package com.dao.impl;
import com.dao.BookDao;
import com.domain.Book;
public class BookDaoImpl extends BaseDaoImpl<Book> implements BookDao {
}
4.3 BookService接口继承BaseService接口
package com.service;
import com.domain.Book;
public interface BookService extends BaseService<Book> {
}
4.4 BookServiceImpl实现BookService接口并继承BaseServiceImpl
- 注意:业务子类注入Dao实例时也要把该Dao实例传递给业务父类
package com.service.impl;
import com.dao.BookDao;
import com.domain.Book;
import com.service.BookService;
public class BookServiceImpl extends BaseServiceImpl<Book> implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
super.setBaseDao(this.bookDao);;
}
}
4.5 BookAction
public String page4(){
Page page=new Page(1,2);
SimpleExpression[] conditions=new SimpleExpression[]{
Restrictions.eq("category.category_id", book.getCategory().getCategory_id()),
Restrictions.like("book_name", book.getBook_name().trim(),MatchMode.ANYWHERE)
};
page.setTotalCount(bookService.findCount(conditions));
List<Book> bookList=bookService.findByPage(page, conditions);
for (Book book : bookList) {
System.out.println(book);
}
return NONE;
}