package com.wang.test.model.dao;
import java.sql.SQLException;
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.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wang.test.model.hibernate.TBook;
/**
* A data access object (DAO) providing persistence and search support for TBook
* 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 com.wang.test.model.hibernate.TBook
* @author MyEclipse Persistence Tools
*/
public class TBookDAO extends HibernateDaoSupport implements ITBookDAO{
private int totalCount;
private static final Log log = LogFactory.getLog(TBookDAO.class);
// property constants
public static final String BOOK_NAME = "bookName";
public static final String PRODUCTE = "producte";
public static final String PRICE = "price";
protected void initDao() {
// do nothing
}
public void save(TBook tBook) {
log.debug("saving TBook instance");
try {
getHibernateTemplate().save(tBook);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(TBook tBook) {
log.debug("deleting TBook instance");
try {
getHibernateTemplate().delete(tBook);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public TBook findById(java.lang.Integer id) {
log.debug("getting TBook instance with id: " + id);
try {
TBook instance = (TBook) getHibernateTemplate().get(
"com.wang.test.model.hibernate.TBook", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(TBook instance) {
log.debug("finding TBook instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
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 TBook instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TBook as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByBookName(Object bookName) {
return findByProperty(BOOK_NAME, bookName);
}
public List findByProducte(Object producte) {
return findByProperty(PRODUCTE, producte);
}
public List findByPrice(Object price) {
return findByProperty(PRICE, price);
}
public List findAll() {
log.debug("finding all TBook instances");
try {
String queryString = "from TBook";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public TBook merge(TBook detachedInstance) {
log.debug("merging TBook instance");
try {
TBook result = (TBook) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(TBook instance) {
log.debug("attaching dirty TBook instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(TBook instance) {
log.debug("attaching clean TBook instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static TBookDAO getFromApplicationContext(ApplicationContext ctx) {
return (TBookDAO) ctx.getBean("TBookDAO");
}
public void update(TBook book) {
// TODO Auto-generated method stub
log.debug("修改一本书的记录");
try {
getHibernateTemplate().update(book);
} catch(RuntimeException re) {
log.error("修改失败!",re);
throw re;
}
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalCount(TBook book) {
StringBuffer sb = new StringBuffer("select count(*) from TBook ");
if ("".equals(book.getBookName()) || book.getBookName()==null)
{}
else
{
sb.append(" where bookName='");
sb.append(book.getBookName());
sb.append("'");
}
return new Integer(getHibernateTemplate().find(sb.toString()).get(0).toString());
//System.out.println("tiao jian count:"+totalCount);
// TODO Auto-generated method stub
//return this.totalCount;
}
public List<TBook> findByBook(TBook book, int startRow,int pageSize) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer(" from TBook");
if ("".equals(book.getBookName()) || book.getBookName()==null)
{}
else
{
sb.append(" where bookName='");
sb.append(book.getBookName());
sb.append("'");
}
if ("".equals(book.getProducte()) || book.getProducte() == null)
{}
else
{
sb.append(" producte='");
sb.append(book.getProducte());
sb.append("'");
}
sb.append(" order by bookName");
Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(sb.toString());
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List<TBook> books = query.list();
getHibernateTemplate().getSessionFactory().close();
return books;
/*return (List<TBook>)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(TBook.class);
if ("".equals(book.getBookName()) || book.getBookName()==null)
{}
else
criteria = criteria.add(Restrictions.like("bookName",book.getBookName()+"%"));
System.out.println("bookName:" + book.getBookName());
setTotalCount(criteria.list().size());
System.out.println("tiao jian count:"+getTotalCount());
criteria.setFirstResult(startRow);
criteria.setMaxResults(pageSize);
return criteria.list();
}
});*/
}
public List<TBook> findByPage(final int startRow, final int pageSize) {
// TODO Auto-generated method stub
return (List<TBook>)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(TBook.class);
setTotalCount(criteria.list().size());
criteria.setFirstResult(startRow);
criteria.setMaxResults(pageSize);
return criteria.list();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
books.rar (51个子文件)
books
books
.project 2KB
sql.sql 1KB
.mymetadata 288B
WebRoot
WEB-INF
web.xml 3KB
spring-form.tld 51KB
faces-config.xml 2KB
applicationContext.xml 2KB
spring.tld 13KB
.faces-config.mex 3KB
lib
classes
com
wang
test
view
bean
BookManagedBean.class 2KB
BookBean.class 2KB
BookAction.class 6KB
BookManagedBean$LocalDataModel.class 1KB
util
ExcelUtils.class 6KB
PageListDataModel.class 3KB
DataPage.class 2KB
FacesUtil.class 2KB
model
dao
测试.class 669B
ITBookDAO.class 783B
TBookDAO$1.class 1KB
TBookDAO.class 8KB
hibernate
TBook.hbm.xml 948B
TBook.class 1KB
service
IBookService.class 793B
impl
BookServiceImpl.class 2KB
tag
PageList.class 2KB
index.jsp 841B
META-INF
MANIFEST.MF 39B
book
addBook.jsp 2KB
list.jsp 5KB
editBook.jsp 2KB
newbl.jsp 2KB
bookList.jsp 3KB
.myeclipse
src
com
wang
test
view
bean
BookAction.java 4KB
BookBean.java 1KB
BookManagedBean.java 2KB
util
FacesUtil.java 1KB
PageListDataModel.java 5KB
ExcelUtils.java 5KB
DataPage.java 1KB
model
dao
测试.java 261B
TBookDAO.java 8KB
ITBookDAO.java 541B
hibernate
TBook.hbm.xml 948B
TBook.java 1KB
service
impl
BookServiceImpl.java 2KB
IBookService.java 575B
tag
PageList.java 1KB
.springBeans 273B
.classpath 5KB
.myhibernatedata 482B
共 51 条
- 1
资源评论
binxigogo
- 粉丝: 23
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功