首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ssh 底层打包(增,删,改,查,分页)

2012-09-22 
ssh 底层封装(增,删,改,查,分页)public class QueryResultT {private ListT resultlistprivate long

ssh 底层封装(增,删,改,查,分页)
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;
}
}

public class PageView<T> {
/** 分页数据 **/
private List<T> records;
/** 页码开始索引和结束索引 **/
private PageIndex pageindex;
/** 总页数 **/
private long totalpage = 1;
/** 每页显示记录数 **/
private int maxresult = 12;
/** 当前页 **/
private int currentpage = 1;
/** 总记录数 **/
private long totalrecord;
/** 页码数量 **/
private int pagecode = 10;
/** 要获取记录的开始索引 **/
public int getFirstResult() {
   return (this.currentpage-1)*this.maxresult;
}
public int getPagecode() {
   return pagecode;
}

public void setPagecode(int pagecode) {
   this.pagecode = pagecode;
}

public PageView(int maxresult, int currentpage) {
   this.maxresult = maxresult;
   this.currentpage = currentpage;
}

public void setQueryResult(QueryResult<T> qr){
   setTotalrecord(qr.getTotalrecord());
   setRecords(qr.getResultlist());
}

public long getTotalrecord() {
   return totalrecord;
}
public void setTotalrecord(long totalrecord) {
   this.totalrecord = totalrecord;
   setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
}
public List<T> getRecords() {
   return records;
}
public void setRecords(List<T> records) {
   this.records = records;
}
public PageIndex getPageindex() {
   return pageindex;
}
public long getTotalpage() {
   return totalpage;
}
public void setTotalpage(long totalpage) {
   this.totalpage = totalpage;
   this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
}
public int getMaxresult() {
   return maxresult;
}
public int getCurrentpage() {
   return currentpage;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.ssh.bean.user">
    <class name="Person" table="person">
    <cache usage="read-write" region="org.ssh.bean.Person"/>
        <id name="id">
            <generator length="20" not-null="true"/>
    </class>
</hibernate-mapping>


public class Person {
private Integer id;
private String name;

public Person(){}

public Person(String name) {
   this.name = name;
}

public Person(Integer id, String name) {
   this.id = id;
   this.name = name;
}
public Integer getId() {
   return id;
}
public void setId(Integer id) {
   this.id = id;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}

}
public class PageIndex {
private long startindex;
private long endindex;

public PageIndex(long startindex, long endindex) {
   this.startindex = startindex;
   this.endindex = endindex;
}
public long getStartindex() {
   return startindex;
}
public void setStartindex(long startindex) {
   this.startindex = startindex;
}
public long getEndindex() {
   return endindex;
}
public void setEndindex(long endindex) {
   this.endindex = endindex;
}

public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
    long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
    long endpage = currentPage+viewpagecount/2;
    if(startpage<1){
     startpage = 1;
     if(totalpage>=viewpagecount) endpage = viewpagecount;
     else endpage = totalpage;
    }
    if(endpage>totalpage){
     endpage = totalpage;
     if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
     else startpage = 1;
    }
    return new PageIndex(startpage, endpage); 
}
}
public interface DAO<T> {
/**
* 获取记录总数
* @param entityClass 实体类
* @return
*/
public long getCount();
/**
* 保存实体
* @param entity 实体id
*/
public void save(Object entity);
/**
* 更新实体
* @param entity 实体id
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public void delete(Serializable ... entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public T find(Serializable entityId);
/**
* 分页获取数据
* @param firstindex 开始索引
* @param maxresult 需要获取的记录数
* @param wherejpql 不带where关键字的条件语句,属性前带有(o.)前缀,语句中只支持使用位置参数 如:o.username=? and o.gender=?
* @param queryParams 条件语句中使用到的位置参数值,如果没有使用位置参数,可以设置为null
* @param orderby 排序,key为属性名称,值为asc/desc,如:LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
   orderby.put("regTime", "desc");
* @return
*/
public QueryResult<T> getScrollData(int firstResult, int maxResult, String wherejpql, Object[] queryParams,LinkedHashMap<String, String> orderby);
/**
* 分页获取数据
* @param firstindex 开始索引
* @param maxresult 需要获取的记录数
* @param wherejpql 不带where关键字的条件语句,属性前带有(o.)前缀,语句中只支持使用位置参数 如:o.username=? and o.gender=?
* @param queryParams 条件语句中使用到的位置参数值,如果没有使用位置参数,可以设置为null
* @return
*/
public QueryResult<T> getScrollData(int firstResult, int maxResult, String wherejpql, Object[] queryParams);
/**
* 分页获取数据
* @param firstindex 开始索引
* @param maxresult 需要获取的记录数
* @param orderby 排序,key为属性名称,值为asc/desc,如:LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
   orderby.put("regTime", "desc");
* @return
*/
public QueryResult<T> getScrollData(int firstResult, int maxResult, LinkedHashMap<String, String> orderby);
/**
* 分页获取数据
* @param firstindex 开始索引
* @param maxresult 需要获取的记录数
* @return
*/
public QueryResult<T> getScrollData(int firstResult, int maxResult);
/**
* 获取全部数据
* @return
*/
public QueryResult<T> getScrollData();
}

@SuppressWarnings("unchecked")
@Transactional
public abstract class DaoSupport<T> implements DAO<T>{
protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());
@Resource private SessionFactory sessionFactory;
/**
* 获取当前Session
* @return
*/
protected Session getSession(){
   return this.sessionFactory.getCurrentSession();
}

public void delete(Serializable ... entityids) {
   for(Serializable id : entityids){
    getSession().delete(getSession().load(this.entityClass, id));
   }
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public T find(Serializable entityId) {
   if(entityId==null) throw new RuntimeException(this.entityClass.getName()+ ":传入的实体id不能为空");
   return (T)getSession().get(this.entityClass, entityId);
}

public void save(Object entity) {
   getSession().persist(entity);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public long getCount() {
   return (Long)getSession().createQuery("select count(*) from "+ getEntityName(this.entityClass)+ " o")
     .uniqueResult();
}

public void update(Object entity) {
   getSession().merge(entity);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstResult, int maxResult, LinkedHashMap<String, String> orderby) {
   return getScrollData(firstResult, maxResult, null, null, orderby);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstResult, int maxResult, String wherejpql, Object[] queryParams) {
   return getScrollData(firstResult, maxResult, wherejpql, queryParams, null);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstResult, int maxResult) {
   return getScrollData(firstResult, maxResult, null, null, null);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData() {
   return getScrollData(-1, -1);
}

@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstResult, int maxResult
    , String wherejpql, Object[] queryParams,LinkedHashMap<String, String> orderby) {
   QueryResult qr = new QueryResult<T>();
   String entityname = getEntityName(this.entityClass);
   String whereql = wherejpql==null || "".equals(wherejpql.trim())? "": "where "+ wherejpql;
   Query query = getSession().createQuery("select o from "+ entityname+ " o "+ whereql+ buildOrderby(orderby));
   setQueryParams(query, queryParams);
   if(firstResult!=-1 && maxResult!=-1) query.setFirstResult(firstResult).setMaxResults(maxResult);
   qr.setResultlist(query.list());
   query = getSession().createQuery("select count(*) from "+ entityname+ " o "+ whereql);
   setQueryParams(query, queryParams);
   qr.setTotalrecord((Long)query.uniqueResult());
   return qr;
}
/**
* 为Quyer对象设置参数
* @param query Quyer对象
* @param queryParams 参数
*/
protected static void setQueryParams(Query query, Object[] queryParams){
   if(queryParams!=null && queryParams.length>0){
    for(int i=0; i<queryParams.length; i++){
     query.setParameter(i, queryParams[i]);
    }
   }
}
/**
* 构建order by语句
* @param orderby
* @return
*/
protected static String buildOrderby(LinkedHashMap<String, String> orderby){
   StringBuffer orderbyql = new StringBuffer("");
   if(orderby!=null && orderby.size()>0){
    orderbyql.append(" order by ");
    for(String key : orderby.keySet()){
     orderbyql.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
    }
    orderbyql.deleteCharAt(orderbyql.length()-1);
   }
   return orderbyql.toString();
}
/**
* 获取实体的名称
* @param <E>
* @param clazz 实体类
* @return
*/
protected static <E> String getEntityName(Class<E> clazz){
   String entityname = clazz.getSimpleName();//约束:不要在hbm.xml映射文件中的<class>节点指定entity-name属性
   return entityname;
}
}

热点排行