DAO.java public interface DAO { /** * 1.获取所要显示的分页数据(一般以list形式来存放) * 2.获取记录的总数(计算总页数) * 3.需要定义一个返回类型,获取两个参数 * @param <T> * @param entityClass 实体类 * @param firstindex 开始索引 * @param maxresult 需要获取记录数 * @return */ public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult);} QueryResult.java(需要定义一个返回类型,获取两个参数) package com.itcast.bean; import java.util.List; public class QueryResult<T> { private List<T> resultlist; private long totalrecord; public List<T> getResultlist(){ return resultlist; } public void setResultlist(List<T> resultlist) { this.resultlist = resultlist; } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; } } DaoSupport.java import com.itcast.bean.QueryResult; public abstract class DaoSupport implements DAO { @SuppressWarnings("unchecked") @Override @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult) { //创建查询对象 QueryResult qr = new QueryResult<T>(); //获取实体名称 String entityname = getEntityName(entityClass); Query query = em.createQuery("select o from "+entityname+" o"); query.setFirstResult(firstindex).setMaxResults(maxresult); qr.setResultlist(query.getResultList()); query = em.createQuery("select count(o) from "+entityname+" o"); qr.setTotalrecord((Long)query.getSingleResult()); return qr; } /** * 获取实体名称 * @param <T> * @param entityClass实体类 * @return */ protected <T> String getEntityName(Class<T> entityClass){ String entityname = entityClass.getSimpleName(); Entity entity = entityClass.getAnnotation(Entity.class); if(entity.name()!=null && !"".equals(entity.name())){ entityname = entity.name(); } return entityname; } }
@Test public void scrollDataTest() { QueryResult<ProductType> qr = productService.getStrollData(ProductType.class, 0, 5); for(ProductType t : qr.getResultList()){ System.out.println(t.getName()); } }
package com.bean.product; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; @Entity public class ProductType implements Serializable { /** * 实体bean应用到ejb3,便于网络传送。 */ private static final long serialVersionUID = -3711997324115011724L; private Integer typeid; private String name; //google 描述 private String note; private boolean visible = true; //子类别 private Set<ProductType> childTypes = new HashSet<ProductType>(); //父类别 private ProductType parent; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getTypeid() { return typeid; } public void setTypeid(Integer typeid) { this.typeid = typeid; } @Column(length = 36 , nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(length = 300) public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Column(nullable = false) public boolean isVisible() { return visible; } public void setVisible(boolean visible) { this.visible = visible; } @OneToMany(cascade = {CascadeType.REFRESH,CascadeType.REMOVE}, mappedBy="parent") public Set<ProductType> getChildTypes() { return childTypes; } public void setChildTypes(Set<ProductType> childTypes) { this.childTypes = childTypes; } //false:不是可选的=必须要有。true是可选的,可以没有。默认下为true @ManyToOne(cascade = (CascadeType.REFRESH),optional = true ) @JoinColumn(name="parentid") public ProductType getParent() { return parent; } public void setParent(ProductType parent) { this.parent = parent; } /* 对象之间的比较,所以需要重写hashCode and equals * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((typeid == null) ? 0 : typeid.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ProductType other = (ProductType) obj; if (typeid == null) { if (other.typeid != null) return false; } else if (!typeid.equals(other.typeid)) return false; return true; } }