当前位置 : 主页 > 编程语言 > java >

SSH整合的配置文件,Hibernate的映射文件就放了一个,模板是一样的

来源:互联网 收集:自由互联 发布时间:2021-06-28
SSH整合 详情请看源码 BookAction.java package action;import java.util.List;import org.apache.struts2.ServletActionContext;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.spri
SSH整合
详情请看源码
BookAction.java
package action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import pojo.Book;
import pojo.BookItem;
import service.BookItemService;
import service.BookService;


public class BookAction extends ActionSupport {
	private BookService bs;
	private BookItemService bis;
	private Integer currentPage;
	private Integer pageNum=3;
	private Integer totalPages;
	
	public Integer getCurrentPage() {
		return currentPage;
	}
	public Integer getPageNum() {
		return pageNum;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public BookService getBs() {
		return bs;
	}
	public void setBs(BookService bs) {
		this.bs = bs;
	}	
	public BookItemService getBis() {
		return bis;
	}
	public void setBis(BookItemService bis) {
		this.bis = bis;
	}
	
	public String getAllBooks() {
		List
 
   list = bs.getAllBooks();
		ActionContext.getContext().getSession().put("list", list);
		return SUCCESS;
	}
	
	public String buyBookResult() {
		int bId=Integer.parseInt(ServletActionContext.getRequest().getParameter("bId"));
		Book book=bs.findBookById(bId);
		ActionContext.getContext().getSession().put("book", book);
		List
  
    itemList = bis.getAllBookItems(); boolean flag=true; for (BookItem bookItem : itemList) { if(book.getbId()==bookItem.getBookId()) { flag=false; bookItem.setBook(book); bis.updateBookItem(bookItem); } } if(flag) { BookItem item=new BookItem(book,1,book.getbPrice(),book.getbId()); bis.addBookItem(item); } return SUCCESS; } public String deleteBookItem() { int itemId=Integer.parseInt(ServletActionContext.getRequest().getParameter("itemId")); bis.deleteItemById(itemId); return SUCCESS; } public String deleteBookCart() { bis.deleteAll(); return SUCCESS; } public String lookCart() { double totalPrice=0.0; List
   
     itemList = bis.getAllBookItems(); for (BookItem bookItem : itemList) { Book book =bs.findBookById(bookItem.getBookId()); bookItem.setBook(book); totalPrice+=bookItem.getItemPrice(); } ActionContext.getContext().getSession().put("itemList", itemList); ActionContext.getContext().getSession().put("totalPrice", totalPrice); return SUCCESS; } public String showByPages() { Integer currentPage = (Integer) ServletActionContext.getRequest().getSession().getAttribute("currentPage"); if(currentPage==null) { currentPage=1; } Integer totalNum=bs.getAllCount(); if(totalNum%pageNum==0) { totalPages=totalNum/pageNum; }else{ totalPages=totalNum/pageNum+1; } List
    
      list = bs.showByPages(currentPage, pageNum); ActionContext.getContext().getSession().put("currentPage", currentPage); ActionContext.getContext().getSession().put("totalPages", totalPages); ActionContext.getContext().getSession().put("list", list); return SUCCESS; } public String showFirstPage() { Integer currentPage = (Integer) ServletActionContext.getRequest().getSession().getAttribute("currentPage"); currentPage=1; bs.showByPages(currentPage, pageNum); ActionContext.getContext().getSession().put("currentPage", currentPage); return SUCCESS; } public String showLastPage() { Integer currentPage = (Integer) ServletActionContext.getRequest().getSession().getAttribute("currentPage"); Integer totalPages = (Integer) ServletActionContext.getRequest().getSession().getAttribute("totalPages"); currentPage=totalPages; bs.showByPages(currentPage, pageNum); ActionContext.getContext().getSession().put("currentPage", currentPage); return SUCCESS; } public String showPrePage() { Integer currentPage = (Integer) ServletActionContext.getRequest().getSession().getAttribute("currentPage"); if(currentPage==1) { currentPage=1; }else { currentPage-=1; } bs.showByPages(currentPage, pageNum); ActionContext.getContext().getSession().put("currentPage", currentPage); return SUCCESS; } public String showNextPage() { Integer currentPage = (Integer) ServletActionContext.getRequest().getSession().getAttribute("currentPage"); Integer totalPages = (Integer) ServletActionContext.getRequest().getSession().getAttribute("totalPages"); if(currentPage==totalPages) { currentPage=totalPages; }else { currentPage+=1; } bs.showByPages(currentPage, pageNum); ActionContext.getContext().getSession().put("currentPage", currentPage); return SUCCESS; } }
    
   
  
 
struts.xml
 


 
	
  
	
  
	
  
	
  
	
  
	
	
  

	
   
    
    
     /BuyResult.jsp
     
    
    
    
     /cart.jsp
     
    
    
     
     lookCart 
     
    
    
     
     lookCart 
     
    
    
    
     /BuyBook.jsp
     
    
    
    
     /BuyBook.jsp
     
    
    
    
     /BuyBook.jsp
     
    
    
    
     /BuyBook.jsp
     
    
    
    
     /BuyBook.jsp
     
    
  

 
applicationContext.xml
 

 

	
   
    
  
	
   
    
  
	
   
    
  
	
   
    
  
	
   
    
    
    
  
	
	
  
	
   
    
    
    
    
  

	
  
	
   
    
    
    
     
      
     
      org.hibernate.dialect.MySQLDialect
      
     
      org.springframework.orm.hibernate5.SpringSessionContext
      
      
     
      true
      
     
      true
      
     
      update
      
     
    
    
    
  

	
  
	
   
    
  

	
  
	
  



 
Book.hbm.xml
网友评论