📄 bookhibernatedao.java
字号:
package com.ascent.dao.hibernate;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.ascent.bean.Book;
import com.ascent.dao.IBookDAO;
public class BookHibernateDAO extends HibernateDaoSupport implements IBookDAO {
private static final Logger LOGGER = LogManager
.getLogger(BookHibernateDAO.class);
private static final String LOAD_ALL = "from Book bk order by bk.bookId desc";
private static final String LOAD_BY_NAME = "from Book bk where bk.bookName = ? ";
private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor = ? ";
private static final String LOAD_BY_ID = "from Book bk where bk.bookId = ? ";
private static final String LOAD_FIVE = "from Book bk order by bk.bookId desc limit 5,5 ";
/**
*
*/
public BookHibernateDAO() {
super();
}
/**
*
* @param Book
* @return Book
*/
public Book saveBook(Book book) {
try {
this.getHibernateTemplate().save(book);
LOGGER.debug("保存书籍信息到数据库成功!");
return book;
} catch (Exception ex) {
LOGGER.error("保存书籍信息到数据库失败!");
ex.printStackTrace();
return null;
}
}
/**
*
* @param id
* Integer
*
* @return Book
*/
public Book getBook(Integer id) {
LOGGER.debug("根据书籍ID得到书籍信息!");
return (Book) this.getHibernateTemplate().get(Book.class, id);
}
/**
*
* @return List
*/
public List findBookAll() {
try {
LOGGER.debug("得到所有书籍列表!");
return this.getHibernateTemplate().find(LOAD_ALL);
} catch (Exception ex) {
LOGGER.error("获取所有书籍列表失败!");
return new ArrayList();
}
}
/**
*
* @param type
* String
*
* @return List
*/
public List findBookById(Integer id) {
try {
LOGGER.debug("根据id得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_ID, id);
} catch (Exception ex) {
LOGGER.error("根据id获取书籍信息失败!");
ex.printStackTrace();
return null;
}
}
/**
*
* @param type
* String
*
* @return List
*/
public List findBookByName(String name) {
try {
LOGGER.debug("根据书籍名得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_NAME, name);
} catch (Exception ex) {
LOGGER.error("根据书籍名获取书籍信息失败!");
ex.printStackTrace();
return null;
}
}
/**
*
* @param Book
*
*/
public List findBookByAuthor(String author) {
try {
LOGGER.debug("根据作者得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_AUTHOR, author);
} catch (Exception ex) {
LOGGER.error("根据作者获取用户书籍失败!");
ex.printStackTrace();
return null;
}
}
/**
*
* @param Book
*
*/
public List findBookFive() {
// List l = null;
try {
LOGGER.debug("得到前5本书籍信息!");
// Query q = this.getHibernateTemplate().getSessionFactory()
// .openSession().createQuery(LOAD_ALL);
// q.setMaxResults(5);
// l = q.list();
// return l;
return this.getHibernateTemplate().find(LOAD_FIVE);
} catch (Exception ex) {
LOGGER.error("得到前5本书籍信息失败!");
ex.printStackTrace();
return null;
}
}
public Book updateBook(Book book) {
try {
this.getHibernateTemplate().update(book);
LOGGER.debug("更新书籍信息到数据库成功!");
return book;
} catch (Exception ex) {
LOGGER.error("更新书籍信息到数据库失败!");
ex.printStackTrace();
return null;
}
}
public void removeBook(Book book) {
LOGGER.debug("从数据库中删除指定书籍信息!");
this.getHibernateTemplate().delete(book);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -