?? 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 + -