package com.library.books.dao;
import java.sql.SQLException;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import java.util.List;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.library.books.entity.Books;
import com.library.books.dao.interfaces.BooksDAO;
/**
* A data access object (DAO) providing persistence and search support for Books
* 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.library.books.entity.Books
* @author MyEclipse Persistence Tools
*/
public class BooksDAOImpl extends HibernateDaoSupport implements BooksDAO{
private static final Log log = LogFactory.getLog(BooksDAOImpl.class);
// property constants
public static final String BOOKNAME = "bookname";
public static final String AUTHOR = "author";
public static final String PUBLISH = "publish";
public static final String ISNB = "isnb";
public static final String PRICE = "price";
public static final String CONTENT = "content";
protected void initDao() {
// do nothing
}
public void save(Books transientInstance) {
log.debug("saving Books instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Books persistentInstance) {
log.debug("deleting Books instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Books findById(java.lang.Integer id) {
log.debug("getting Books instance with id: " + id);
try {
Books instance = (Books) getHibernateTemplate().get(
"com.library.books.entity.Books", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Books instance) {
log.debug("finding Books 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 Books instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Books 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(BOOKNAME, bookname);
}
public List findByAuthor(Object author) {
return findByProperty(AUTHOR, author);
}
public List findByPublish(Object publish) {
return findByProperty(PUBLISH, publish);
}
public List findByIsnb(Object isnb) {
return findByProperty(ISNB, isnb);
}
public List findByPrice(Object price) {
return findByProperty(PRICE, price);
}
public List findByContent(Object content) {
return findByProperty(CONTENT, content);
}
public List findAll() {
log.debug("finding all Books instances");
try {
String queryString = "from Books";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Books merge(Books detachedInstance) {
log.debug("merging Books instance");
try {
Books result = (Books) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Books instance) {
log.debug("attaching dirty Books instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Books instance) {
log.debug("attaching clean Books instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static BooksDAOImpl getFromApplicationContext(ApplicationContext ctx) {
return (BooksDAOImpl) ctx.getBean("BooksDAO");
}
public void addBook(Books book){
this.getHibernateTemplate().save(book);
}
public List queryByName(String bookname){
return this.findByBookname(bookname);
}
public void updateBook(Books book){
this.getHibernateTemplate().update(book);
}
public List findAllBooks(){
return this.findAll();
}
public List queryBooksByValue(String queryName,String queryValue) {
System.out.println("Fieldname: "+queryName+",Value: "+queryValue);
try {
String hql="FROM Books WHERE "+queryName+" like '%"+queryValue+"%'"+"ORDER BY bookname";
List<Books> list = this.getHibernateTemplate().find(hql);
return list ;
}catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public Books findBookById(int id){
return this.findById(id);
}
public void deleteBook(Books book){
this.getHibernateTemplate().delete(book);
}
//获取全部数据的总行数
public int getRows() {
String hql="FROM Books";
List list=this.getHibernateTemplate().find(hql);
return list.size();
}
//分页获取全部数据
public List getBooks(int pageSize, int startRow) throws HibernateException{
final int pageSize1=pageSize;
final int startRow1=startRow;
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public List doInHibernate(Session session) throws HibernateException, SQLException {
// TODO 自动生成方法存根
Query query=session.createQuery("FROM Books ORDER BY bookname");
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
});
}
//分页全部查询或按属性查询
public List getBooks(String queryName,String queryValue,int pageSize, int startRow) {
//使用final是因为嵌套内部的方法不能使用外部的变量
final int pageSize1 = pageSize;
final int startRow1=startRow;
final String queryName1 = queryName;
final String queryValue1 = queryValue;
String sql="";
if(queryName1==null||queryName1.equals("")||queryValue1==null||queryValue1.equals(""))
sql="FROM Books ORDER BY bookname";
else
sql="FROM Books WHERE "+queryName1+" like '%"+queryValue1+"%'"+"ORDER BY bookname";
//使用final是因为嵌套内部的方法不能使用外部的变量
final String sql1=sql;
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public List doInHibernate(Session session) throws HibernateException, SQLException {
// TODO 自动生成方法存根
Query query=session.createQuery(sql1);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
});
}
//分页获得全部
评论0
最新资源