---
title: "分页方法getScrollDate"
author: "sunqitang"
date: "2009-05-22T21:10:50Z"
tags: []
categories: ["企业架构"]
slug: "getscrolldate"
draft: false
lastmod: "2010-03-16T17:42:16Z"
id: 393732
url: "https://sunqitang.iteye.com/blog/393732"
---

```&quot;java&quot;
import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

import java.util.LinkedHashMap;



import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;



import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;



@Transactional

public abstract class DaoSupport implements DAO{

	@PersistenceContext protected EntityManager em;

	

	public void clear(){

		em.clear();

	}



	public  void delete(Class entityClass,Object entityid) {

		delete(entityClass, new Object[]{entityid});

	}



	public  void delete(Class entityClass,Object[] entityids) {

		for(Object id : entityids){

			em.remove(em.getReference(entityClass, id));

		}

	}

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  T find(Class entityClass, Object entityId) {

		return em.find(entityClass, entityId);

	}



	public void save(Object entity) {

		em.persist(entity);

	}

	

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  long getCount(Class entityClass) {

		return (Long)em.createQuery(&quot;select count(&quot;+ getCountField(entityClass) +&quot;) from &quot;+ getEntityName(entityClass)+ &quot; o&quot;).getSingleResult();

	}

	

	public void update(Object entity) {

		em.merge(entity);

	}

	

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  QueryResult getScrollData(Class entityClass,

			int firstindex, int maxresult, LinkedHashMap orderby) {

		return getScrollData(entityClass,firstindex,maxresult,null,null,orderby);

	}

	

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  QueryResult getScrollData(Class entityClass,

			int firstindex, int maxresult, String wherejpql, Object[] queryParams) {

		return getScrollData(entityClass,firstindex,maxresult,wherejpql,queryParams,null);

	}

	

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  QueryResult getScrollData(Class entityClass, int firstindex, int maxresult) {

		return getScrollData(entityClass,firstindex,maxresult,null,null,null);

	}

	

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  QueryResult getScrollData(Class entityClass) {

		return getScrollData(entityClass, -1, -1);

	}



	@SuppressWarnings(&quot;unchecked&quot;)

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)

	public  QueryResult getScrollData(Class entityClass, int firstindex, int maxresult

			, String wherejpql, Object[] queryParams,LinkedHashMap orderby) {

		QueryResult qr = new QueryResult();

		String entityname = getEntityName(entityClass);

		Query query = em.createQuery(&quot;select o from &quot;+ entityname+ &quot; o &quot;+(wherejpql==null? &quot;&quot;: &quot;where &quot;+ wherejpql)+ buildOrderby(orderby));

		setQueryParams(query, queryParams);

		if(firstindex!=-1 &amp;&amp; maxresult!=-1) query.setFirstResult(firstindex).setMaxResults(maxresult);

		qr.setResultlist(query.getResultList());

		query = em.createQuery(&quot;select count(&quot;+ getCountField(entityClass)+ &quot;) from &quot;+ entityname+ &quot; o &quot;+(wherejpql==null? &quot;&quot;: &quot;where &quot;+ wherejpql));

		setQueryParams(query, queryParams);

		qr.setTotalrecord((Long)query.getSingleResult());

		return qr;

	}

	

	protected void setQueryParams(Query query, Object[] queryParams){

		if(queryParams!=null &amp;&amp; queryParams.length&gt;0){

			for(int i=0; i orderby){

		StringBuffer orderbyql = new StringBuffer(&quot;&quot;);

		if(orderby!=null &amp;&amp; orderby.size()&gt;0){

			orderbyql.append(&quot; order by &quot;);

			for(String key : orderby.keySet()){

				orderbyql.append(&quot;o.&quot;).append(key).append(&quot; &quot;).append(orderby.get(key)).append(&quot;,&quot;);

			}

			orderbyql.deleteCharAt(orderbyql.length()-1);

		}

		return orderbyql.toString();

	}

	//获得实体名称

	protected  String getEntityName(Class entityClass){

		String entityname = entityClass.getSimpleName();

		Entity entity = entityClass.getAnnotation(Entity.class);

		if(entity.name()!=null &amp;&amp; !&quot;&quot;.equals(entity.name())){

			entityname = entity.name();

		}

		return entityname;

	}

	

	protected  String getCountField(Class clazz){

		String out = &quot;o&quot;;

		try {

			PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();

			for(PropertyDescriptor propertydesc : propertyDescriptors){

				Method method = propertydesc.getReadMethod();

				if(method!=null &amp;&amp; method.isAnnotationPresent(EmbeddedId.class)){					

					PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors();

					out = &quot;o.&quot;+ propertydesc.getName()+ &quot;.&quot; + (!ps[1].getName().equals(&quot;class&quot;)? ps[1].getName(): ps[0].getName());

					break;

				}

			}

		} catch (Exception e) {

			e.printStackTrace();

		}

        return out;

	}

}


```

 
QueryResult



```&quot;java&quot;
public class QueryResult {
	private List resultlist;
	private long totalrecord;
	
	public List getResultlist() {
		return resultlist;
	}
	public void setResultlist(List resultlist) {
		this.resultlist = resultlist;
	}
	public long getTotalrecord() {
		return totalrecord;
	}
	public void setTotalrecord(long totalrecord) {
		this.totalrecord = totalrecord;
	}
}
```