大致思路是这样的,前端用一个Page类来进行封装,包括基本的数据,当前页,查询参数等信息。
public class Page<T> {
private int everyPage=10;
private int totalCount;
private int totalPage;
private int currentPage=1;
private int beginIndex;
private boolean hasNextPage;
private boolean hasPrePage;
private String order;
private Map<String, String> params=new HashMap<String, String>();
private List<T> result;
public Page(int everyPage, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage) {
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public Map<String, String> getParams() {
return params;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
public List<T> getResult() {
return result;
}
public void setResult(List<T> result) {
this.result = result;
}
public Page() {
super();
}
public int getEveryPage() {
return everyPage;
}
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPrePage() {
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
public void initPage(int everyPage,int totalCount,int currentPage){
setTotalCount(totalCount);
setEveryPage(everyPage);
setCurrentPage(currentPage==0?1:currentPage);
int totalPage=0;
if(totalCount%everyPage==0){
totalPage=totalCount/everyPage;
}else {
totalPage=totalCount/everyPage+1;
}
setTotalPage(totalPage);
setBeginIndex((getCurrentPage()-1)*everyPage);
setHasPrePage(currentPage==1?false:true);
setHasNextPage(currentPage==getTotalPage()|| totalPage==0?false:true);
}
}
然后在action的类中是这样写的
private Page<KhFormGenerated> pageZpNew=new Page<KhFormGenerated>();
private Page<KhFormState> pageKhNew=new Page<KhFormState>();
...对应的get,set
public String dxFormState(){
dxList=khDxService.findKhDxs();
/*formGList=khFormService.findKhFormGAll(zpfPage,10,zpdxId,zpfYear);*/
/*pageZp=khFormService.findKhFormGAll(zpfPage,10,zpdxId,zpfYear);
pageKh=khFormService.findAllFormState(khfPage,10,dxId,khfYear);*/
String zphqlString=ProjectUtils.getHql("select khbg from KhFormGenerated khbg ", pageZpNew.getParams());
String khhqlString=ProjectUtils.getHql("select khbs from KhFormState khbs ", pageKhNew.getParams());
pageZpNew=khFormGService.page(zphqlString,pageZpNew);
pageKhNew=khFormStateService.page(khhqlString, pageKhNew);
return Action.SUCCESS;
}
ProjectUtils.getHql是根据Page对象里的查询参数来生成hql语句
public static String getHql(String head,Map<String, String> params){
int countNotNull=0;
for (String param : params.keySet()) {
if (!params.get(param).equals("")&¶ms.get(param)!=null&&Integer.parseInt(params.get(param)+"")!=0) {
countNotNull++;
}
if(params.get(param)==null||params.get(param).equals("")){
params.put(param, "0");
}
}
int count=0;
for (String param : params.keySet()) {
if (Integer.parseInt(params.get(param))!=0) {
if(count==0)
head+="where ";
if(countNotNull>=1 &&count!=countNotNull-1){
head+=param+"=? and ";
}else{
head+=param+"=? ";
}
count++;
}
}
return head;
}
jsp页面,用struts标签,然后将对应的参数写到Page对象的params里面去,下面是怎么写的例子
<form action="dxFormState.action" method="post" >
对象: <select name="pageZpNew.params['khbg.khDx.khDxId']"
style="height:30px;width:120px;margin-right:20px;">
<option value="0">请选择</option>
<s:iterator value="dxList" id="dx" status="stu">
<option value=<s:property value="#dx.khDxId" /> <s:if test="pageZpNew.params['khbg.k hDx.khDxId']==#dx.khDxId">selected="selected" </s:if> ><s:property value="#dx.kh DxName" />
</option>
</s:iterator>
</select>
年度: <select name="pageZpNew.params['khbg.khForm.year']" style="height:30px;width:80px;margin-right:20px;"><option value="0">请选择</option>
<option value="2014"
<s:if test="pageZpNew.params['khbg.khForm.year']==2014">selected="selected" </s:if>>2014</option><option value="2015"<s:if test="pageZpNew.params['khbg.khForm.year']==2015">selected="selected" </s:if>>2015</option><option value="2016"<s:if test="pageZpNew.params['khbg.khForm.year']==2016">selected="selected" </s:if>>2016</option><option value="2017"<s:if test="pageZpNew.params['khbg.khForm.year']==2017">selected="selected" </s:if>>2017</option><option value="2018"<s:if test="pageZpNew.params['khbg.khForm.year']==2018">selected="selected" </s:if>>2018</option><option value="2019"s:if test="pageZpNew.params['khbg.khForm.year']==2019">selected="selected" </s:if>>2019</option><option value="2020"<s:if test="pageZpNew.params['khbg.khForm.year']==2020">selected="selected" </s:if>>2020</option>
</select>
核表状态: <select name="pageZpNew.params['khbg.zpState']"style="height:30px;width:100px;margin-right:20px;"><option value="0" <s:if test="pageZpNew.params['khbg.zpState']==0">selected="selected" </s:if> >请选择</option><option value="1" <s:if test="pageZpNew.params['khbg.zpState']==1">selected="selected" </s:if>>未填写</option><option value="2" <s:if test="pageZpNew.params['khbg.zpState']==2">selected="selected" </s:if>>暂时保存</option><option value="3" <s:if test="pageZpNew.params['khbg.zpState']==3">selected="selected" </s:if>>提交</option>
</select>
<button type="submit" class="btn btn-primary mr_20">查询</button>
共<s:property value="pageZpNew.totalCount" />条记录,共<s:property value="pageZpNew.totalPage"/>页
<button type="button" <s:if test='pageZpNew.hasPrePage==false'> disabled="disabled"</s:if> class="pagePre btn btn-default ml_20 mr_10 ">上一页</button>
第<s:property value="pageZpNew.currentPage"/>页 <input name="pageZpNew.currentPage" class="hidden currentPage" value=<s:property value="pageZpNew.currentPage"/> >
<button type="button" <s:if test='pageZpNew.hasNextPage==false'> disabled="disabled"</s:if> class="pageNext btn btn-default ml_10">下一页</button>
</form>
把参数都写到form表单,并封装到pageZpNew对象中去
这样从前端,到action控制层就都有了,接下来是dao层和service层,采用泛型实现basedao和baseservice,然后将对应的dao和service集成basedao和baseservice,然后用具体的daoimpl和serviceimpl来实现对应的dao和service 并分别继承basedaoimpl和baseserviceimpl
下面是basedao
public interface BaseDao<T> {
public abstract void delete(int id);
public abstract void delete(T entity);
public abstract T getById(int id);
public abstract List<T> findAll();
public abstract List<T> findByIds(String[] ids);
public abstract int save(T entity);
public abstract void update(T entity);
public abstract List<T> find(String hql);
public abstract List<T> find(String hql,Object... values);
public abstract List<T> query(String hql);
public abstract List<T> query(String hql,int page,int pageSize);
public abstract List<T> query(String hql,Object...objects);
public abstract void saveOrUpdate(T entity);
public abstract void saveOrUpdateAll(List<T> list);
public abstract int update(String hql,Object...objects);
public abstract int update(String hql);
public abstract int excudeSqlUpdate(String sql,Object...objects);
public abstract int excudeSql(String sql);
public abstract List<T> pageQuery(String hql,int page,int pageSize,Object... objects);
public abstract Page<T> page(String hql,Page<T> page);
public abstract List<T> createCriteria(int page,int pageSize,Map<String, Object> map);
public abstract List<T> createCriteria(Map<String, Object> map);
}
basedaoimpl
@Transactional
@Repository
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
protected Log logger =LogFactory.getLog(BaseDaoImpl.class);
@SuppressWarnings("unchecked")
protected Class<T> clazz=GenericsUtils.getSuperClassGenricType(this.getClass()); ;
@Override
public void delete(int id) {
Object object=(Object) getCSession().get(clazz, id);
getCSession().delete(object);
}
@SuppressWarnings("unchecked")
@Override
public T getById(int id) {
return (T)getCSession().get(clazz, id);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findAll() {
return getCSession().createQuery("from "+clazz.getSimpleName()).list();
}
@Override
public List<T> findByIds(String[] ids) {
return null;
}
@Override
public int save(T entity) {
int id=(Integer) getCSession().save(entity);
return id;
}
@Override
public void update(T entity) {
getCSession().update(entity);
}
public Session getCSession(){
return getHibernateTemplate().getSessionFactory().getCurrentSession();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql) {
return getHibernateTemplate().find(hql);
}
@Override
public List<T> find(String hql, Object... values) {
@SuppressWarnings("unchecked")
List<T> lists=(List<T>)this.getHibernateTemplate().find(hql, values);
return lists;
}
@SuppressWarnings("unchecked")
@Override
public List<T> query(String hql) {
return createQuery(hql).list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> query(String hql, int page, int pageSize) {
return createQuery(hql, page,pageSize).list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> query(String hql, Object... objects) {
return createQuery(hql, objects).list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> pageQuery(String hql, int page, int pageSize, Object... objects) {
return createQuery(hql, page, pageSize, objects).list();
}
public Query createQuery(String hql){
Query query=getCSession().createQuery(hql);
return query;
}
public Query createQuery(String hql,Object...objects){
Query query=getCSession().createQuery(hql);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
return query;
}
public Query createQuery(String hql,int page,int PageSize){
Query query=getCSession().createQuery(hql);
query.setFirstResult((page-1)*PageSize);
query.setMaxResults(PageSize);
return query;
}
public Query createQuery(String hql,int page,int PageSize,Object... objects){
Query query=getCSession().createQuery(hql);
query.setFirstResult((page-1)*PageSize);
query.setMaxResults(PageSize);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
return query;
}
@Override
public List<T> createCriteria(int page, int pageSize,
Map<String, Object> map) {
@SuppressWarnings("unchecked")
List<T> list=getCSession().createCriteria(getClass())
.add( Restrictions.allEq(map))
.setFirstResult((page-1)*pageSize)
.setMaxResults(pageSize).list();
return list;
}
@Override
public List<T> createCriteria(Map<String, Object> map) {
@SuppressWarnings("unchecked")
List<T> list=getCSession().createCriteria(getClass())
.add(Restrictions.allEq(map))
.list();
return list;
}
@Override
public int excudeSqlUpdate(String sql, Object... objects) {
Query query=getCSession().createSQLQuery(sql);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
return query.executeUpdate();
}
@Override
public int update(String hql, Object... objects) {
Query query=getCSession().createQuery(hql);
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
return query.executeUpdate();
}
private int getRowCount(String hql,Object...objects){
return getHibernateTemplate().find(hql, objects).size();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Page<T> page(String hql,Page<T> page) {
Query query=getCSession().createQuery(hql);
Map<String, String> params=page.getParams();
int position=0;
for (String param : params.keySet()) {
if(Integer.parseInt(params.get(param))!=0){
if(!param.endsWith("year")&&!param.endsWith("Id")&&!param.endsWith("id")&&!param.endsWith("score")&&!param.endsWith("Score")&&!param.endsWith("State")&&!param.endsWith("state")&&!param.endsWith("ype")){
query.setParameter(position, params.get(param));
}else{
query.setParameter(position, Integer.parseInt(params.get(param)));
}
position++;
}
}
int rowCount=query.list().size();
query.setFirstResult((page.getCurrentPage()-1)*page.getEveryPage());
query.setMaxResults(page.getEveryPage());
List<T> list=query.list();
page.initPage(page.getEveryPage(), rowCount, page.getCurrentPage());
page.setResult(list);
return page;
}
@Override
public int update(String hql) {
Query query=getCSession().createQuery(hql);
return query.executeUpdate();
}
@Override
public int excudeSql(String sql) {
Query query=getCSession().createSQLQuery(sql);
return query.executeUpdate();
}
@Override
public void saveOrUpdateAll(List<T> list) {
getHibernateTemplate().saveOrUpdateAll(list);
}
@Override
public void saveOrUpdate(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
}
@Override
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
}
baseservice
public interface BaseService<T> {
public abstract void delete(int id);
public abstract void delete(T entity);
public abstract T getById(int id);
public abstract List<T> findAll();
public abstract List<T> findByIds(String[] ids);
public abstract int save(T entity);
public abstract void update(T entity);
public abstract List<T> find(String hql);
public abstract List<T> find(String hql,Object... objects);
public abstract List<T> query(String hql);
public abstract List<T> query(String hql,int page,int pageSize);
public abstract List<T> query(String hql,Object...objects);
public abstract void saveOrUpdate(T entity);
public abstract void saveOrUpdateAll(List<T> list);
public abstract int update(String hql);
public abstract int update(String hql,Object...objects);
public abstract int excudeSqlUpdate(String sql,Object...objects);
public abstract int excudeSqlUpdate(String sql);
public abstract List<T> pageQuery(String hql,int page,int pageSize,Object... objects);
public abstract Page<T> page(String hql,Page<T> page);
public abstract List<T> query(String hql,int page,int pageSize,Object[] objects);
public abstract List<T> createCriteria(int page,int pageSize,Map<String, Object> map);
public abstract List<T> createCriteria(Map<String, Object> map);
}
baseserviceimpl
public class BaseServiceImpl<T> implements BaseService<T> {
private BaseDao<T> dao;
public BaseDao<T> getDao() {
return dao;
}
public void setDao(BaseDao<T> dao) {
this.dao = dao;
}
@Override
public void delete(int id) {
dao.delete(id);
}
@Override
public T getById(int id) {
return dao.getById(id);
}
@Override
public List<T> findAll() {
return dao.findAll();
}
@Override
public List<T> findByIds(String[] ids) {
return dao.findByIds(ids);
}
@Override
public int save(T entity) {
return dao.save(entity);
}
@Override
public void update(T entity) {
dao.update(entity);
}
@Override
public List<T> find(String hql) {
return dao.find(hql);
}
@Override
public List<T> find(String hql, Object... objects) {
return dao.find(hql, objects);
}
@Override
public List<T> query(String hql) {
return dao.query(hql);
}
@Override
public List<T> query(String hql, int page, int pageSize) {
return dao.query(hql, page, pageSize);
}
@Override
public List<T> query(String hql, Object... objects) {
return dao.query(hql, objects);
}
@Override
public List<T> query(String hql, int page, int pageSize, Object[] objects) {
return dao.query(hql, page, pageSize, objects);
}
@Override
public List<T> createCriteria(int page, int pageSize,
Map<String, Object> map) {
return dao.createCriteria(page, pageSize, map);
}
@Override
public List<T> createCriteria(Map<String, Object> map) {
return dao.createCriteria(map);
}
@Override
public int excudeSqlUpdate(String sql, Object... objects) {
return dao.excudeSqlUpdate(sql, objects);
}
@Override
public int update(String hql, Object... objects) {
return dao.update(hql, objects);
}
@Override
public List<T> pageQuery(String hql, int page, int pageSize,
Object... objects) {
return dao.pageQuery(hql, page, pageSize, objects);
}
@Override
public Page<T> page(String hql,Page<T> page) {
return dao.page(hql, page);
}
@Override
public int update(String hql) {
return dao.update(hql);
}
@Override
public int excudeSqlUpdate(String sql) {
return dao.excudeSql(sql);
}
@Override
public void saveOrUpdateAll(List<T> list) {
dao.saveOrUpdateAll(list);
}
@Override
public void saveOrUpdate(T entity) {
dao.saveOrUpdate(entity);
}
@Override
public void delete(T entity) {
dao.delete(entity);
}
}
具体的其他dao和service就是分别继承basedao和baseservice,daoimpl和serviceimpl分别集成basedaoimpl和baseserviceimpl和实现接口就可以了