spring和hibernate框架组合下的分页问题
protectedvoidprepareQuery(QueryqueryObject){if(getHibernateTemplate().isCacheQueries(...
protected void prepareQuery(Query queryObject) {
if (getHibernateTemplate().isCacheQueries()) {
queryObject.setCacheable(true);
if (getHibernateTemplate().getQueryCacheRegion() != null) {
queryObject.setCacheRegion(getHibernateTemplate()
.getQueryCacheRegion());
}
}
if (getHibernateTemplate().getFetchSize() > 0) {
queryObject.setFetchSize(getHibernateTemplate()
.getFetchSize());
}
if (getHibernateTemplate().getMaxResults() > 0) {
queryObject.setMaxResults(getHibernateTemplate()
.getMaxResults());
}
SessionFactoryUtils.applyTransactionTimeout(queryObject,
getSessionFactory());
}
}, true);
这段代码有看不明白的地方
isCacheQueries()
getQueryCacheRegion()
getFetchSize()
这些都是什么意思
想了解这些方法,是不是要下载hibernate和spring的源码来看呢,还要下官方文档么
我是培训班毕业的,在培训班的时候写的没这么复杂,现在看了公司同事的代码,觉得很不一样,我都不知道还要写这些东西在上边
isCacheQueries()
getQueryCacheRegion()
getFetchSize()
这些都是什么意思 展开
if (getHibernateTemplate().isCacheQueries()) {
queryObject.setCacheable(true);
if (getHibernateTemplate().getQueryCacheRegion() != null) {
queryObject.setCacheRegion(getHibernateTemplate()
.getQueryCacheRegion());
}
}
if (getHibernateTemplate().getFetchSize() > 0) {
queryObject.setFetchSize(getHibernateTemplate()
.getFetchSize());
}
if (getHibernateTemplate().getMaxResults() > 0) {
queryObject.setMaxResults(getHibernateTemplate()
.getMaxResults());
}
SessionFactoryUtils.applyTransactionTimeout(queryObject,
getSessionFactory());
}
}, true);
这段代码有看不明白的地方
isCacheQueries()
getQueryCacheRegion()
getFetchSize()
这些都是什么意思
想了解这些方法,是不是要下载hibernate和spring的源码来看呢,还要下官方文档么
我是培训班毕业的,在培训班的时候写的没这么复杂,现在看了公司同事的代码,觉得很不一样,我都不知道还要写这些东西在上边
isCacheQueries()
getQueryCacheRegion()
getFetchSize()
这些都是什么意思 展开
3个回答
展开全部
package com.h2oprolures.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.h2oprolures.dao.ProductDao;
import com.h2oprolures.entity.HpProduct;
import com.h2oprolures.entity.HpPtype;
import com.h2oprolures.entity.HpSort;
@Repository
public class ProductDaoImpl extends HibernateTemplate implements ProductDao {
@Resource(name = "sessionFactory")
public void injectSessionFactory(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
public boolean addProduct(HpProduct product) {
// HpPtype ptype = (HpPtype) getSession().get(HpPtype.class,
// product.getPtypeid());
// product.setPtype(ptype);
// product.setSortid(ptype.getSort().getId());
// HpSort sort = (HpSort) getSession().get(HpSort.class,
// product.getSortid());
// product.setSort(sort);
getSession().save(product);
return true;
}
public boolean delProductById(int id) {
HpProduct product = (HpProduct) getSession().get(HpProduct.class, id);
getSession().delete(product);
return true;
}
public HpProduct selProductById(int id) {
HpProduct product = (HpProduct) getSession().get(HpProduct.class, id);
return product;
}
public List<HpProduct> selProducts(int pagenow, int pagesize) {
List<HpProduct> products = getSession().createQuery("from HpProduct")
.setFirstResult((pagenow - 1) * pagesize).setMaxResults(
pagesize).list();
return products;
}
public int selRowNum() {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct").uniqueResult().toString());
return rownum;
}
public boolean upProduct(HpProduct product) {
HpProduct pproduct = (HpProduct) getSession().get(HpProduct.class,
product.getId());
pproduct.setId(product.getId());
pproduct.setPdesc(product.getPdesc());
pproduct.setPname(product.getPname());
pproduct.setPimg(product.getPimg());
pproduct.setRecommand(product.getRecommand());
pproduct.setInstr(product.getInstr());
pproduct.setSortid(product.getSortid());
// HpPtype ptype = (HpPtype) getSession().get(HpPtype.class,
// product.getPtypeid());
// pproduct.setPtype(ptype);
HpSort sort = (HpSort) getSession().get(HpSort.class,
product.getSortid());
// pproduct.setSort(sort);
getSession().update(pproduct);
return true;
}
public List<HpProduct> selProducts(int typeid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where ptypeid = ? order by id asc")
.setParameter(0, typeid).list();
return products;
}
public List<HpProduct> selBySort(int sortid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where sortid = ? ").setParameter(0, sortid)
.list();
return products;
}
public List<HpProduct> selByType(int pagenow, int pagesize, int typeid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where ptypeid = ?").setParameter(0, typeid)
.setFirstResult((pagenow - 1) * pagesize).setMaxResults(
pagesize).list();
return products;
}
public int selRowNumByType(int typeid) {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct where ptypeid = ?")
.setParameter(0, typeid).uniqueResult().toString());
return rownum;
}
public List<HpProduct> selProdBySID(int sid, int pagesize, int pagenow) {
List<HpProduct> products = null;
int start = (pagenow - 1) * pagesize;
products = getSession().createQuery(
"from HpProduct where sortid = " + sid + " ").setFirstResult(
start).setMaxResults(pagesize).list();
return products;
}
public int selRowNumBySID(int sid) {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct where sortid = ?")
.setParameter(0, sid).uniqueResult().toString());
return rownum;
}
public List<HpProduct> selProdBySID(int pagesize, int pagenow) {
List<HpProduct> products = null;
int start = (pagenow - 1) * pagesize;
products = getSession().createQuery(
"from HpProduct ").setFirstResult(
start).setMaxResults(pagesize).list();
return products;
}
public int selRowNumBySID() {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct ")
.uniqueResult().toString());
return rownum;
}
public List<HpProduct> selRecommand() {
List<HpProduct> products = null;
products = getSession().createQuery("from HpProduct where recommand = 1").list();
return products;
}
}
一个daoImpl基本操作都在里面了。。缺少练习。。
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.h2oprolures.dao.ProductDao;
import com.h2oprolures.entity.HpProduct;
import com.h2oprolures.entity.HpPtype;
import com.h2oprolures.entity.HpSort;
@Repository
public class ProductDaoImpl extends HibernateTemplate implements ProductDao {
@Resource(name = "sessionFactory")
public void injectSessionFactory(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
public boolean addProduct(HpProduct product) {
// HpPtype ptype = (HpPtype) getSession().get(HpPtype.class,
// product.getPtypeid());
// product.setPtype(ptype);
// product.setSortid(ptype.getSort().getId());
// HpSort sort = (HpSort) getSession().get(HpSort.class,
// product.getSortid());
// product.setSort(sort);
getSession().save(product);
return true;
}
public boolean delProductById(int id) {
HpProduct product = (HpProduct) getSession().get(HpProduct.class, id);
getSession().delete(product);
return true;
}
public HpProduct selProductById(int id) {
HpProduct product = (HpProduct) getSession().get(HpProduct.class, id);
return product;
}
public List<HpProduct> selProducts(int pagenow, int pagesize) {
List<HpProduct> products = getSession().createQuery("from HpProduct")
.setFirstResult((pagenow - 1) * pagesize).setMaxResults(
pagesize).list();
return products;
}
public int selRowNum() {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct").uniqueResult().toString());
return rownum;
}
public boolean upProduct(HpProduct product) {
HpProduct pproduct = (HpProduct) getSession().get(HpProduct.class,
product.getId());
pproduct.setId(product.getId());
pproduct.setPdesc(product.getPdesc());
pproduct.setPname(product.getPname());
pproduct.setPimg(product.getPimg());
pproduct.setRecommand(product.getRecommand());
pproduct.setInstr(product.getInstr());
pproduct.setSortid(product.getSortid());
// HpPtype ptype = (HpPtype) getSession().get(HpPtype.class,
// product.getPtypeid());
// pproduct.setPtype(ptype);
HpSort sort = (HpSort) getSession().get(HpSort.class,
product.getSortid());
// pproduct.setSort(sort);
getSession().update(pproduct);
return true;
}
public List<HpProduct> selProducts(int typeid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where ptypeid = ? order by id asc")
.setParameter(0, typeid).list();
return products;
}
public List<HpProduct> selBySort(int sortid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where sortid = ? ").setParameter(0, sortid)
.list();
return products;
}
public List<HpProduct> selByType(int pagenow, int pagesize, int typeid) {
List<HpProduct> products = getSession().createQuery(
"from HpProduct where ptypeid = ?").setParameter(0, typeid)
.setFirstResult((pagenow - 1) * pagesize).setMaxResults(
pagesize).list();
return products;
}
public int selRowNumByType(int typeid) {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct where ptypeid = ?")
.setParameter(0, typeid).uniqueResult().toString());
return rownum;
}
public List<HpProduct> selProdBySID(int sid, int pagesize, int pagenow) {
List<HpProduct> products = null;
int start = (pagenow - 1) * pagesize;
products = getSession().createQuery(
"from HpProduct where sortid = " + sid + " ").setFirstResult(
start).setMaxResults(pagesize).list();
return products;
}
public int selRowNumBySID(int sid) {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct where sortid = ?")
.setParameter(0, sid).uniqueResult().toString());
return rownum;
}
public List<HpProduct> selProdBySID(int pagesize, int pagenow) {
List<HpProduct> products = null;
int start = (pagenow - 1) * pagesize;
products = getSession().createQuery(
"from HpProduct ").setFirstResult(
start).setMaxResults(pagesize).list();
return products;
}
public int selRowNumBySID() {
int rownum = Integer.parseInt(getSession().createQuery(
"select count(*) from HpProduct ")
.uniqueResult().toString());
return rownum;
}
public List<HpProduct> selRecommand() {
List<HpProduct> products = null;
products = getSession().createQuery("from HpProduct where recommand = 1").list();
return products;
}
}
一个daoImpl基本操作都在里面了。。缺少练习。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询