打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
s2sh 底层封装(增,删,改,查,分页)
【转】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 class="native"/>
</id>
<property name="name" 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;
}
}
public interface PersonService extends DAO<Person>{
} public class PersonServiceBean extends DaoSupport<Person> implements PersonService {
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring+JPA 通用DAO及实现
jquery datatable后台封装数据
ASP.NET十分有用的页面间传值方法
hibernate DAO 泛型 另
使用泛型类简化ibatis系统架构
linq orderby通用类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服