Book 实体类及注解
package cn.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; /** * BOOK 实体类 */ @Entity @Table(name="BOOK") public class Book { @Id @GeneratedValue(generator="BOOK_SEQ",strategy=GenerationType.SEQUENCE) @SequenceGenerator(name="BOOK_SEQ",sequenceName="BOOK_SEQ",allocationSize=1,initialValue=1) private int id;//编号 @Column private String isbn;//书号 @Column private String title;//标题 @Column private double price;//价格 @Column private Date pubdate;//出版日期 @Column private String intro;//简介 public Book() { } public Book(String isbn, String title, double price, Date pubdate, String intro) { this.isbn = isbn; this.title = title; this.price = price; this.pubdate = pubdate; this.intro = intro; } public Book(int id, String isbn, String title, double price, Date pubdate, String intro) { this.id = id; this.isbn = isbn; this.title = title; this.price = price; this.pubdate = pubdate; this.intro = intro; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Date getPubdate() { return pubdate; } public void setPubdate(Date pubdate) { this.pubdate = pubdate; } public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } }
Test 测试类
package cn.test; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Order; import cn.entity.Book; import cn.util.HibernateSessionFactory; public class Test { public static void main(String[] args) { Test test = new Test(); // save(); // System.out.println(test.findTotal()); // List<Book> books = new ArrayList<Book>(); // books = test.findPageBooks(1, 2, "title", "asc"); // for (Book book : books) { // System.out.println(book.getId()+" "+book.getTitle()+" "+book.getPubdate()); // } // Book book = new Book("345345-232-132", "Java编程思想2", 99.00, new Date(), "一本全面介绍Java的书籍,适合任何层次的读者"); // int id = test.addBook(book); // System.out.println("这是添加的结果"+id); // System.out.println("这是删除的结果"+test.deleteBook(108)); // Book book = new Book(104,"345345-232-132", "Java编程思想2", 99.00, new Date(), "一本全面介绍Java的书籍,适合任何层次的读者"); // int id = test.updateBook(book); // System.out.println("这是修改的结果"+id); } private static void save() { Book book = new Book("345345-232-132", "Java编程思想", 99.00, new Date(), "一本全面介绍Java的书籍,适合任何层次的读者"); Session session=HibernateSessionFactory.getSession(); Transaction tx=null; try { tx=session.beginTransaction(); session.save(book); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } } public long findTotal(){ Session session=HibernateSessionFactory.getSession(); Long count = (Long)session.createQuery("select count(*) from Book").uniqueResult(); return count; } public List<Book> findPageBooks(final int pageIndex, final int pageSize, final String sort, final String order){ Session session=HibernateSessionFactory.getSession(); Criteria criteria = session.createCriteria(Book.class); if("desc".equals(order)){ criteria.addOrder(Order.desc(sort)); }else{ criteria.addOrder(Order.asc(sort)); } criteria.setFirstResult(pageIndex).setMaxResults(pageSize); List<Book> books = criteria.list(); HibernateSessionFactory.closeSession(); return books; } public int addBook(Book book){ Session session=HibernateSessionFactory.getSession(); Transaction tx=null; int id = 0; try { tx=session.beginTransaction(); id = (Integer)session.save(book); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return id; } public int deleteBook(int id){ Session session=HibernateSessionFactory.getSession(); Transaction tx = null; int num = 0; try { tx=session.beginTransaction(); Book book = new Book(); book.setId(id); session.delete(book); tx.commit(); num = 1; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return num; } public int updateBook(Book book){ Session session=HibernateSessionFactory.getSession(); Transaction tx = null; int id = 0; try { tx=session.beginTransaction(); session.update(book); tx.commit(); id = 1; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return id; } }
BookDao 数据访问层
package cn.dao; import java.util.List; import cn.entity.Book; /** * Book 数据访问层接口 * */ public interface BookDao { /** * 查询共有多少记录 * * @return */ public long findTotal(); /** * 查询一页的数据 * * @param begin 从哪条开始 0 * @param end 得到多少条 * @param sort 排序的列 * @param order 排序的方式 desc/asc * @return */ public List<Book> findPageBooks(final int begin, final int end, final String sort, final String order); /** * 增加一条数据 * * @param book 传来的参数不包括 id * @return */ public int addBook(Book book); /** * 删除一条数据 * * @param id 根据 id 作删除 * @return */ public int deleteBook(int id); /** * 修改一条数据 * * @param book 传来的参数包括 id * @return */ public int updateBook(Book book); }
package cn.dao.impl; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Order; import cn.dao.BookDao; import cn.entity.Book; import cn.util.HibernateSessionFactory; /** * Book 数据访问层实现 * */ public class BookDaoImpl implements BookDao { /** * 查询共有多少记录 * * @return */ public long findTotal(){ Session session=HibernateSessionFactory.getSession(); Long count = (Long)session.createQuery("select count(*) from Book").uniqueResult(); return count; } /** * 查询一页的数据 * * @param begin 从哪条开始 0 * @param end 得到多少条 * @param sort 排序的列 * @param order 排序的方式 desc/asc * @return */ public List<Book> findPageBooks(final int begin, final int end, final String sort, final String order){ Session session=HibernateSessionFactory.getSession(); Criteria criteria = session.createCriteria(Book.class); if("desc".equals(order)){ criteria.addOrder(Order.desc(sort)); }else{ criteria.addOrder(Order.asc(sort)); } criteria.setFirstResult(begin).setMaxResults(end); List<Book> books = criteria.list(); HibernateSessionFactory.closeSession(); return books; } /** * 增加一条数据 * * @param book 传来的参数不包括 id * @return */ public int addBook(Book book){ Session session=HibernateSessionFactory.getSession(); Transaction tx=null; int id = 0; try { tx=session.beginTransaction(); id = (Integer)session.save(book); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return id; } /** * 删除一条数据 * * @param id 根据 id 作删除 * @return */ public int deleteBook(int id){ Session session=HibernateSessionFactory.getSession(); Transaction tx = null; int num = 0; try { tx=session.beginTransaction(); Book book = new Book(); book.setId(id); session.delete(book); tx.commit(); num = 1; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return num; } /** * 修改一条数据 * * @param book 传来的参数包括 id * @return */ public int updateBook(Book book){ Session session=HibernateSessionFactory.getSession(); Transaction tx = null; int id = 0; try { tx=session.beginTransaction(); session.update(book); tx.commit(); id = 1; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return id; } }
BookBiz 业务逻辑层
package cn.biz; import java.util.List; import cn.entity.Book; /** * Book 业务逻辑层接口 * */ public interface BookBiz { public long findTotal(); /** * 查询一页的数据 * * @param pageIndex 从哪页开始 * @param pageSize 得到的页数 * @param sort 排序的列 * @param order 排序的方式 desc/asc * @return */ public List<Book> findPageBooks(final int pageIndex, final int pageSize, final String sort, final String order); public int addBook(Book book); public int deleteBook(int id); public int updateBook(Book book); }
package cn.biz.impl; import java.util.List; import cn.biz.BookBiz; import cn.dao.BookDao; import cn.dao.impl.BookDaoImpl; import cn.entity.Book; /** * Book 业务逻辑层实现 * */ public class BookBizImpl implements BookBiz { private BookDao bookDao = new BookDaoImpl(); public long findTotal() { return bookDao.findTotal(); } /** * 查询一页的数据 * * @param pageIndex 当前页号 * @param pageSize 页面大小 * @param sort 排序的列 * @param order 排序的方式 desc/asc * @return */ public List<Book> findPageBooks(int pageIndex, int pageSize, String sort, String order) { int begin = (pageIndex - 1)*pageSize; return bookDao.findPageBooks(begin, pageSize, sort, order); } public int addBook(Book book) { return bookDao.addBook(book); } public int deleteBook(int id) { return bookDao.deleteBook(id); } public int updateBook(Book book) { return bookDao.updateBook(book); } }