📄 prodserviceimpl.java
字号:
package netstore.service;
import java.sql.Timestamp;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import netstore.businessobjects.*;
// Import the exceptions used
import netstore.framework.exceptions.DatastoreException;
import javax.servlet.ServletContext;
// Import the implementation specific packages
//hibernate 3.0
import org.hibernate.*;
import org.apache.log4j.*;
/**
* 业务代理实现
* @author huangyongfeng
*
*/
public class ProdServiceImpl extends NetstoreServiceImpl{
private static Logger logger = Logger.getLogger(ProdServiceImpl.class.getName());
/**
* Create the service *
*/
public ProdServiceImpl() throws DatastoreException {
super();
}
/**
* Return a list of items.
* 获取产品,从第几条开始,获取几条
* 查询时也加入事务?
*/
public List getItems(int beginPage,int length) throws DatastoreException {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String hql = "from Item i";
//不查询出全部内容而得到记录总数
//((Integer)session.iterate(hql).next()).intValue()
Query query = session.createQuery("select count(*) " + hql);
rowCount = ((Integer)query.iterate().next()).intValue();
//rowCount = ((Integer)session.iterate("select count(*) " + hql).next()).intValue();
logger.info("rowCount:"+rowCount);
this.setRowCount(rowCount);
//查询所需页资料
query = session.createQuery("from Item i order by i.basePrice asc");
if(beginPage > 0)
{
query.setFirstResult(length * (beginPage - 1));
}
query.setMaxResults(length);
List result = query.list();
tx.commit();
return toGBEncoding(result);
}catch (Exception ex) {
HibernateUtil.rollbackTransaction(tx);
throw DatastoreException.datastoreError(ex);
} finally {
// No matter what, close the session
HibernateUtil.closeSession(session);
}
}
/**
* 通过id获取产品对象
*/
public Item getItemById( Long id)throws DatastoreException{
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Item item =(Item)session.get(Item.class,id);;
tx.commit();
//实际此时直接返回对象也可
return toGBEncoding(item);
}catch (Exception ex) {
HibernateUtil.rollbackTransaction(tx);
throw DatastoreException.datastoreError(ex);
} finally {
// No matter what, close the session
HibernateUtil.closeSession(session);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -