userTbDAO

本文档详细介绍了UserTbDAO类的各种操作方法,包括保存、删除、查找等,并提供了分页查询的具体实现。
package db.hibernate;

import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;

import util.PageResult;

/**
* A data access object (DAO) providing persistence and search support for
* UserTb entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see db.hibernate.UserTb
* @author MyEclipse Persistence Tools
*/

public class UserTbDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(UserTbDAO.class);
// property constants
public static final String USER_NAME = "userName";
public static final String USER_ADR = "userAdr";

public void save(UserTb transientInstance) {
log.debug("saving UserTb instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(UserTb persistentInstance) {
log.debug("deleting UserTb instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public UserTb findById(java.lang.Integer id) {
log.debug("getting UserTb instance with id: " + id);
try {
UserTb instance = (UserTb) getSession().get("db.hibernate.UserTb",
id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(UserTb instance) {
log.debug("finding UserTb instance by example");
try {
List results = getSession().createCriteria("db.hibernate.UserTb")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding UserTb instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from UserTb as model where model."
+ propertyName + "= ?";
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;
}
}

public List findByUserName(Object userName) {
return findByProperty(USER_NAME, userName);
}

public List findByUserAdr(Object userAdr) {
return findByProperty(USER_ADR, userAdr);
}

public List findAll() {
log.debug("finding all UserTb instances");
try {
String queryString = "from UserTb";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public UserTb merge(UserTb detachedInstance) {
log.debug("merging UserTb instance");
try {
UserTb result = (UserTb) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(UserTb instance) {
log.debug("attaching dirty UserTb instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(UserTb instance) {
log.debug("attaching clean UserTb instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public List findByExampleByPage(PageResult pageResult,UserTb user){
Criteria criteria=super.getSession().createCriteria(UserTb.class);
Example example=Example.create(user);
example.enableLike(MatchMode.ANYWHERE);
example.excludeZeroes();
criteria.add(example);
criteria.setProjection(Projections.rowCount());
int numOfRecords=((Integer)criteria.uniqueResult()).intValue();
pageResult.setNumOfRecords(numOfRecords);
criteria.setProjection(null);
int numOfRecordForStarting = pageResult.getNumOfRecordForStarting();
int sizeOfPage =pageResult.getSizeOfPage();
criteria.setFirstResult(numOfRecordForStarting);
criteria.setMaxResults(sizeOfPage);
List list=criteria.list();
return list;
}

public static void main(String[] args) {
PageResult pageResult=new PageResult();
pageResult.setCurrentPage(0);
pageResult.setSizeOfPage(3);
UserTb user=new UserTb();
List list=new UserTbDAO().findByExampleByPage(pageResult, user);
System.err.println(list.size());
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
UserTb utb=(UserTb) iterator.next();
System.out.println(utb.getUserName());
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值