package cn.jbit.houserent.dao.hibimpl;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
import cn.jbit.houserent.bean.House;
import cn.jbit.houserent.dao.BaseHibernateDAO;
import cn.jbit.houserent.dao.HouseDao;
import cn.jbit.houserent.util.QueryProperties;
/**
* @author zhiyao.xi
*
*/
public class HouseDaoHibImpl extends BaseHibernateDAO implements HouseDao {
private static final Log log = LogFactory.getLog(HouseDaoHibImpl.class);
/*
* (non-Javadoc)
*
* @see cn.jbit.houserent.dao.HouseDao#save(cn.jbit.houserent.bean.House)
*/
public void save(House transientInstance) {
log.debug("saving House instance");
Transaction tx = null;
try {
tx = getSession().beginTransaction();
getSession().save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
tx.rollback();
log.error("save failed", re);
throw re;
} finally {
closeSession();
}
}
/*
* (non-Javadoc)
*
* @see cn.jbit.houserent.dao.HouseDao#delete(cn.jbit.houserent.bean.House)
*/
public void delete(House persistentInstance) {
log.debug("deleting House instance");
Transaction tx = getSession().beginTransaction();
try {
getSession().delete(persistentInstance);
tx.commit();
log.debug("delete successful");
} catch (RuntimeException re) {
tx.rollback();
log.error("delete failed", re);
throw re;
} finally {
closeSession();
}
}
/*
* (non-Javadoc)
*
* @see cn.jbit.houserent.dao.HouseDao#findById(java.lang.Integer)
*/
public House findById(java.lang.Integer id) {
log.debug("getting House instance with id: " + id);
try {
House instance = (House) getSession().get(
"cn.jbit.houserent.bean.House", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
} finally {
closeSession();
}
}
/*
* (non-Javadoc)
*
* @see
* cn.jbit.houserent.dao.HouseDao#findByExample(cn.jbit.houserent.bean.House
* )
*/
@SuppressWarnings("unchecked")
public List<House> findByExample(House instance) {
log.debug("finding House instance by example");
List<House> results = null;
try {
results = getSession().createCriteria(
"cn.jbit.houserent.bean.House").add(
Example.create(instance)).list();
return results;
} catch (RuntimeException re) {
return results;
} finally {
closeSession();
}
}
/*
* (non-Javadoc)
*
* @see cn.jbit.houserent.dao.HouseDao#findByProperty(java.lang.String,
* java.lang.Object)
*/
@SuppressWarnings("unchecked")
public List<House> findByProperty(String propertyName, Object value) {
log.debug("finding House instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from House as model where model."
+ propertyName + "like ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, "%" + value + "%");
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
} finally {
closeSession();
}
}
/*
* (non-Javadoc)
*
* @see cn.jbit.houserent.dao.HouseDao#findAll()
*/
@SuppressWarnings("unchecked")
public List<House> findAll() {
log.debug("finding all House instances");
try {
String queryString = "from House";
Query queryObject = getSession().createQuery(queryString);
queryObject.setMaxResults(100);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
} finally {
closeSession();
}
}
/**
* 获得总条数
*
* @return
*/
public int totalCount() {
List<House> total = findAll();
if (total == null)
return 0;
else
return total.size();
}
public List<House> query(int first, int size) {
Configuration conf = null;
SessionFactory sessionFactory = null;
Session session = null;
List<House> list = null;
try {
// 1.读取配置文件
conf = new Configuration().configure();
// 2.创建SessionFactory
sessionFactory = conf.buildSessionFactory();
// 3. 打开session
session = sessionFactory.openSession();
// 4. 加载数据操作
Criteria criteria = session.createCriteria(House.class);
criteria.setFirstResult(first);
criteria.setMaxResults(size);
list = criteria.list();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
// 5. 关闭session
session.close();
sessionFactory.close();
}
return list;
}
@SuppressWarnings("unchecked")
public List<House> findHouseByProperties(QueryProperties qp) {
log.debug("finding House by properties");
try {
StringBuffer queryString = new StringBuffer();
queryString.append("from House where ");
queryString.append("(title like :title) ");
queryString.append("and (price between :low_price and :high_price) ");
queryString.append("and (street_id like :street_id) ");
queryString.append("and (type_id like :type_id) ");
queryString
.append("and (floorage between :small_floorage and :big_floorage) ");
// queryString.append("and (date between :start_date and :end_date)");
System.out.println(queryString.toString());
Query queryObject = getSession()
.createQuery(queryString.toString());
queryObject.setProperties(qp);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find house by properties failed", re);
throw re;
} finally {
closeSession();
}
}
public int totalSearch(QueryProperties qp) {
List<House> l = findHouseByProperties(qp);
if(l == null)
return 0;
else
return l.size();
}
public List<House> findHouseByProperties(int first,int size,QueryProperties qp) {
log.debug("finding House by properties");
try {
StringBuffer queryString = new StringBuffer();
queryString.append("from House where ");
queryString.append("(title like :title) ");
queryString.append("and (price between :low_price and :high_price) ");
queryString.append("and (street_id like :street_id) ");
queryString.append("and (type_id like :type_id) ");
queryString
.append("and (floorage between :small_floorage and :big_floorage) ");
// queryString.append("and (date between :start_date and :end_date)");
System.out.println(queryString.toString());
Query queryObject = getSession()
.createQuery(queryString.toString());
queryObject.setProperties(qp);
queryObject.setFirstResult(first);
queryObject.setMaxResults(size);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find house by properties failed", re);
throw re;
} finally {
closeSession();
}
}
public void update(House transientInstance) {
log.debug("updating House instance");
Transaction tx = getSession().beginTransaction();
try {
getSession().update(transientInstance);
tx.commit();
log.debug("update successful");
} catch (RuntimeException re) {
tx.rollback();
log.error("update failed", re);
throw re;
} finally {
closeSession();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ACCP6.0 Y2 租房网 (184个子文件)
HouseDaoHibImpl.class 9KB
HouseAction.class 7KB
UserDaoHibImpl.class 5KB
StreetDaoHibImpl.class 5KB
HouseBizImpl.class 5KB
TypeDaoHibImpl.class 5KB
DistrictDaoHibImpl.class 5KB
Add.class 5KB
Login.class 3KB
House.class 3KB
Manage.class 3KB
CharacterEncodingFilter.class 2KB
UserBizImpl.class 2KB
QueryProperties.class 2KB
HibernateUtil.class 2KB
CharacterEncodingFilter$HttpRequestWrapper.class 2KB
User.class 2KB
DateConverter.class 2KB
AuthorizationInterceptor.class 1KB
District.class 1KB
Street.class 1KB
StreetBizImpl.class 1KB
TimeInterceptor.class 1KB
HouseBiz.class 1018B
Type.class 1014B
QueryUser.class 989B
HouseDao.class 928B
TypeBizImpl.class 922B
Default.class 798B
DistrictDao.class 744B
UserDao.class 729B
StreetDao.class 726B
TypeDao.class 708B
BaseDao.class 648B
BaseHibernateDAO.class 559B
UserBiz.class 375B
StreetBiz.class 318B
TypeBiz.class 192B
.classpath 4KB
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 7KB
thumb_house.gif 7KB
logo.gif 4KB
btn_bg.gif 968B
selector_arrow.gif 79B
hibernate3.jar 2.31MB
xwork-2.1.2.jar 2.18MB
ojdbc6.jar 1.89MB
struts2-dojo-plugin-2.1.6.jar 1.65MB
jgroups-2.2.8.jar 1.53MB
freemarker-2.3.13.jar 850KB
struts2-core-2.1.6.jar 719KB
javassist-3.9.0.GA.jar 583KB
jboss-jmx.jar 578KB
jboss-common.jar 573KB
sqljdbc.jar 570KB
commons-collections-3.1.jar 546KB
struts.jar 532KB
c3p0-0.9.0.jar 477KB
proxool-0.8.3.jar 459KB
antlr-2.7.6.jar 433KB
antlr-2.7.6rc1.jar 427KB
log4j-1.2.15.jar 383KB
jboss-cache.jar 364KB
log4j-1.2.11.jar 343KB
cglib-nodep-2.1_3.jar 317KB
dom4j-1.6.1.jar 307KB
commons-httpclient.jar 298KB
cglib-2.1.3.jar 276KB
commons-lang.jar 237KB
jaxen-1.1-beta-7.jar 223KB
jboss-system.jar 218KB
commons-beanutils.jar 184KB
concurrent-1.3.2.jar 166KB
commons-digester.jar 164KB
ognl-2.6.11.jar 164KB
xml-apis.jar 121KB
commons-dbcp.jar 119KB
oscache-2.1.jar 112KB
jaas.jar 101KB
commons-validator.jar 82KB
commons-io.jar 82KB
commons-discovery.jar 70KB
commons-pool.jar 61KB
commons-logging.jar 59KB
commons-fileupload-1.2.1.jar 56KB
ehcache-1.1.jar 46KB
commons-codec.jar 46KB
swarmcache-1.0rc2.jar 30KB
asm.jar 26KB
jacc-1_0-fr.jar 24KB
slf4j-api-1.5.8.jar 23KB
connector.jar 18KB
asm-attrs.jar 17KB
jta-1.1.jar 13KB
slf4j-log4j12-1.5.2.jar 9KB
jta.jar 8KB
jdbc2_0-stdext.jar 7KB
HouseDaoHibImpl.java 7KB
HouseAction.java 6KB
共 184 条
- 1
- 2
资源评论
- Kav30002013-09-14实际上这个是课件,请谨慎下载。(就是说未调试过的项目)
- keiuwlg2015-04-13包含课件和源码
wjjy2011
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功