⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bizserviceimpl.java

📁 首先确保MySQL4.0数据库已经正确安装
💻 JAVA
字号:
package edu.jnestore.service;

import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import edu.jnestore.beans.*;
import edu.jnestore.util.*;
import edu.jnestore.exceptions.*;
import java.util.*;
/**
 *BizServiceImpl是业务操作接口的实现类,
 *通过Hiberante提供的持久对象完成与数据库相关联的业务操作
 */
public class BizServiceImpl implements IBizService {

  private Session session;
  //login方法用来进行登陆验证,如果验证通过,返回一个Account对象
  public Account login(String userid,String password) throws LoginFailedException , DBException{
      Account account = null;
      try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Account where userId='"+ userid +  "' and password='" + password + "'");
		HibernateUtilPlus.closeSession();		
		if(q.list().size()==1) {		  
		    account = (Account)(q.list().get(0));	
		}	 					  
				 
	   }catch(Exception e) {	     	
			   e.printStackTrace();	 
			   throw new DBException("database operation failed");
	   }	
	   if(account==null) {
	    throw new LoginFailedException();	
	   }		
	   return account;		  
	 
   }
  //newUser方法用来完成用户注册,用户注册信息存储在Account类型的参数act中
  public void newUser(Account act) throws UserExistsException,DBException{
       if(userExist(act.getUserid())) {
       	 throw new UserExistsException();       	 
      } 
      else {
	       try {	            
			session = HibernateUtilPlus.currentSession();
			session.save(act);
			session.flush();
			HibernateUtilPlus.closeSession();			  
		}catch(Exception e) {
				   e.printStackTrace();
				   throw new DBException("database operation failed");
		} 
	}
 }
  //getAllBooks方法取出数据库中所有的图书
  public Book[] getAllBooks() throws DBException { 
   Book[] book1  = null;
   try {
	session = HibernateUtilPlus.currentSession();
	Query q = session.createQuery("from Book as p ");
	 Object[] o = q.list().toArray();
	 if (o !=null ) { 
	   book1 = new Book[o.length];			
	  for(int i=0;i<o.length;i++) {
		book1[i] = (Book)o[i];
	  }
	 } 
	 HibernateUtilPlus.closeSession();	
	
	}catch(Exception e) {
		   e.printStackTrace();	 
		   throw new DBException("database operation failed");
	 }
	  return book1;    	 	
 
 }
 //getBookWithISBN方法依据图书编号进行查询
 public Book getBookWithISBN(String bookisbn) throws DBException  {
    Book book = null;
    try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Book as p where p.isbnid ='" + bookisbn + "'");
		HibernateUtilPlus.closeSession();			 
		 book = (Book)q.list().get(0);
        }catch(Exception e) {
				  throw new DBException("database operation failed"); 
	   }
	  return book; 	  
 	
	
 }	
  //getBookByAuthor依据作者进行查询
  public PageDataBean getBookByAuthor(String authorName) throws DBException  {
    PageDataBean pageBean = new PageDataBean();
    try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Book as p where p.author like '%" + authorName + "%'");
		 ArrayList list1 = new ArrayList();
		Object[] o = q.list().toArray();
	       if (o !=null ) { 
	         for(int i=0;i<o.length;i++) {
		   list1.add((Book)o[i]);
	         }
	        } 
	        
	        pageBean.setData(list1);
		HibernateUtilPlus.closeSession();			 
		 
        }catch(Exception e) {
        	                  e.printStackTrace();
				  throw new DBException("database operation failed"); 
	}
	return pageBean; 	 	
	
 }	
  //getBookByName方法依据书名进行查询
  public PageDataBean getBookByName(String bookName) throws DBException  {
   PageDataBean pageBean = new PageDataBean();
    try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Book as p where p.name  like '%" + bookName + "%'");
		 
		 ArrayList list1 = new ArrayList();
		Object[] o = q.list().toArray();
	       if (o !=null ) { 
	         for(int i=0;i<o.length;i++) {
		   list1.add((Book)o[i]);
	         }
	        } 
	        pageBean.setData(list1);
		HibernateUtilPlus.closeSession();			 
		 
        }catch(Exception e) {
        	                  e.printStackTrace();
				  throw new DBException("database operation failed"); 
	   }
	  return pageBean; 	  
 	
	
 }	
 
//processOrder方法完成订单处理
 public void processOrder(Order order) throws DBException {
 	//在本次实现中不对订单进行处理	
 	
 }
   //userExist方法判断指定的userid是否在系统中已存在
   private  boolean userExist(String userid ) throws DBException {
     try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Account where userId='"+ userid + "'" );
	        HibernateUtilPlus.closeSession();		
		if(q.list().size()!=0) 				
			 return true;					  
				 
		}catch(Exception e) {
				   e.printStackTrace();	 
				    throw new DBException("database operation failed");
		 }					  
		return false;	
	 }
       //返回book表中记录数量	 
       private  int getAvailableCount()throws Exception
       {
       	   int count =0;
	   try {
		session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Book");		
		count = q.list().size();
		HibernateUtilPlus.closeSession();      					  
				 
		}catch(Exception e) {
				   e.printStackTrace();	 
				    throw new DBException("database operation failed");
		 }					  
		return count;	
	 }
	
       
	//根据总行数计算总页数
	private int countTotalPage(int totalRows,int rowsPerPage) {     
          if (totalRows % rowsPerPage ==0){
           return totalRows/rowsPerPage;
         }else{
          return totalRows/rowsPerPage + 1;        
        }
      }
	//getPageData方法用来获取page参数指定页的图书数据,rowsPerPage指定每页几行数据
        public PageDataBean getPageData(String page,String rowsPerPage)throws Exception
	{
		
		int totalRows = getAvailableCount();
		int rowsPage = Integer.parseInt(rowsPerPage);
		int totalPages =  countTotalPage(totalRows,rowsPage);
		
		ArrayList data = new ArrayList();		
		PageDataBean pageBean=new PageDataBean();
			
		try
		{
			
			int pageNum=Integer.parseInt(page);
			session = HibernateUtilPlus.currentSession();
		Query q = session.createQuery("from Book" );
		q.setFirstResult((pageNum-1)*rowsPage);
		q.setMaxResults(rowsPage);
		 Object[] o = q.list().toArray();
	       if (o !=null ) { 	          
	          for(int i=0;i<o.length;i++) {
		     data.add((Book)o[i]);
	         }
	       } 
		
	        HibernateUtilPlus.closeSession();		
					
			pageBean.setCurPage(pageNum);
			pageBean.setData(data);
			pageBean.setTotalPage(totalPages);
			
			return pageBean;
		}
		catch(Exception e)
		{
		   	e.printStackTrace();
			throw e;
			
		}
	  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -