ssh三大框架整合后的增删改查以及分页

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实体类:
Book:
package com.ssh.vero.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tbl_book")
public class Book {

private int id;
private String bookName;
private String writer;
private double price;
private String publish;

@Id
@GeneratedValue
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
}

Paging:
package com.ssh.vero.entity;

import java.util.List;

public class Paging {

private List books;
private int pageNow;//所在的页数
private int pageSize;//每页的数量
private int pageCount;//总页数
private long rowCount;//总条数

public List getBooks() {
return books;
}

public void setBooks(List books) {
this.books = books;
}

public int getPageNow() {
return pageNow;
}

public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getPageCount() {
return pageCount;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public long getRowCount() {
return rowCount;
}

public void setRowCount(long rowCount) {
this.rowCount = rowCount;
}
}

Dao:
IBookDao:
package com.ssh.vero.dao;

import java.util.List;

import com.ssh.vero.entity.Book;
import com.ssh.vero.entity.Paging;

public interface IBookDao {

void save(Book book);

void update(Book book);

//void delete(int id);
void delete(Book book);

Book get(int id);

List getAll();

Paging selectBooksByName(String bookName,int pageNow,int pageSize);

List findByName(String bookName);

}

BookDaoImpl:
package com.ssh.vero.dao.impl;

import java.util.ArrayList;
import java.util.List;

import er;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.ssh.vero.dao.IBookDao;
import com.ssh.vero.entity.Book;
import com.ssh.vero.entity.Paging;

public class BookDaoImpl implements IBookDao {


private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

private Session getSession(){
return sessionFactory.getCurrentSession();
}

@Override
public void save(Book book) {
getSession().save(book);
}

@Override
public void update(Book book) {
getSession().update(book);

}

/*@Override
public void delete(int id) {
Object obj = get(id);
if(obj != null){
getSession().delete(obj);
}
}*/

@Override
public void delete(Book book){
getSession().delete(book);
}

@Override
public Book get(int id) {
Book book = (Book) getSession().get(Book.class, id);
return book;
}

@Override
public List getAll() {
List books = getSession().createCriteria(Book.class).list();
return books;
}

@Override
public Paging selectBooksByName(String bookName,int pageNow,int pageSize) {

StringBuffer hql = new StringBuffer("from Book where 1=1");
if(bookName != null && !"".equals(bookName.trim())){
hql.append("and bookName=:bookName");
}

Query q = getSession().createQuery(hql.toString())
.setFirstResult((pageNow-1)*pageSize)
.setMaxResults(pageSize);

if(bookName != null && !"".equals(bookName.trim())){
q.setString("bookName", bookName);
}

Paging paging = new Paging();

List books = q.list();

long rowCount = this.getRowCount(bookName);
paging.setRowCount(rowCount);

int pageCount = 0;
if(rowCount % pageSize == 0){
pageCount = (int) (rowCount / pageSize);
}else{
pageCount = (int) (rowCount / pageSize)+1;
}

paging.setBooks(books);
paging.setPageNow(pageNow);
paging.setPageSize(pageSize);
paging.setPageCount(pageCount);

return paging;
}

private long getRowCount(String bookName) {

StringBuffer hql = new StringBuffer("select count(*) from Book where 1=1 ");
if(bookName != null && !"".equals(bookName.trim())){
hql.append(" and bookName=:bookName ");
}

Query q = getSession().createQuery(hql.toString());

if(bookName != null && !"".equals(bookName.trim())){
q.setString("bookName", bookName);
}

Long count = (Long) q.uniqueResult();

return count;
}

@Override
public List findByName(String bookName) {

StringBuffer hql = new StringBuffer("from Book where 1=1");
hql.append("and bookName like '%bookName%'");

Query q = getSession().createQuery(hql.toString());
if(bookName != null && !"".equals(bookName.trim())){
q.setString("bookName", bookName);
}

List books = q.list();

return books;
}

}


service:
IBookService:
package com.ssh.vero.service;

import java.util.List;

import com.ssh.vero.entity.Book;
import com.ssh.vero.entity.Paging;

publi

c interface IBookSerivce {

void save(Book book);

void update(Book book);

//void delete(int id);
void delete(Book book);

Book get(int id);

List getAll();

Paging selectBooksByName(String bookName,int pageNow,int pageSize);

List findByName(String bookName);
}

BookServiceImpl:
package com.ssh.vero.service.impl;

import java.util.List;

import com.ssh.vero.dao.IBookDao;
import com.ssh.vero.entity.Book;
import com.ssh.vero.entity.Paging;
import com.ssh.vero.service.IBookSerivce;

public class BookServiceImpl implements IBookSerivce {

private IBookDao bookDao;

public IBookDao getBookDao() {
return bookDao;
}

public void setBookDao(IBookDao bookDao) {
this.bookDao = bookDao;
}

@Override
public void save(Book book) {
bookDao.save(book);
}

@Override
public void update(Book book) {
bookDao.update(book);
}

/*
* @Override public void delete(int id) { bookDao.delete(id); }
*/

@Override
public void delete(Book book) {
bookDao.delete(book);
}

@Override
public Book get(int id) {
return bookDao.get(id);
}

@Override
public List getAll() {
return bookDao.getAll();
}

@Override
public Paging selectBooksByName(String bookName, int pageNow, int pageSize) {
return bookDao.selectBooksByName(bookName, pageNow, pageSize);
}

@Override
public List findByName(String bookName) {

return bookDao.findByName(bookName);
}
}

Controller:
BookController;
package com.ssh.vero.controller;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.vero.entity.Book;
import com.ssh.vero.entity.Paging;
import com.ssh.vero.service.IBookSerivce;

public class BookController extends ActionSupport {

private List books;

private Book book;

private String bookName;

private int id;

private int pageNow;

//private int pageSize;

private Paging paging;

private IBookSerivce bookService;

public void setBookService(IBookSerivce bookService) {
this.bookService = bookService;
}

public BookController() {
}

public String list(){
//books = bookService.getAll();
if(pageNow == 0){
pageNow = 1;
}
paging = bookService.selectBooksByName(bookName, pageNow, 2);
return "list";
}

public String save(){
bookService.save(book);
return "save";
}

public String delete(){
book = bookService.get(id);
if(book != null){
bookService.delete(book);
}
return "delete";
}

public String update(){
//book = bookService.get(id);
if(book != null){
bookService.update(book);
}
return "update";
}

public String get(){
book = bookService.get(id);
return "get";
}

public String find(){
books = bookService.findByName(bookName);

return "find";
}

public List getBooks() {
return books;
}

public void setBooks(List books) {
this.books = books;
}

public Book getBook() {
return book;
}

public void setBook(Book book) {
this.book = book;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getPageNow() {
return pageNow;
}

public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}

public Paging getPaging() {
return paging;
}

public void setPaging(Paging paging) {
this.paging = paging;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}
}

jsp页面:
list.jsp:
<%@page import="com.ssh.vero.service.impl.BookServiceImpl"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags"%>




首页



欢迎来到用户列表界面










图书添加




















图书ID图书名称作者价格出版社操作
修改||
删除














首页



上一页



下一页



尾页







save.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>





用户添加页面




欢迎来到用户添加页面























书名

作者

价格

出版社








update.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>




修改页面



修改页面






























书名

作者

价格

出版社








配置文件:
applicationContext.xml:

xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/tx
/schema/tx/spring-tx.xsd
/schema/ao

p
/schema/aop/spring-aop.xsd">












org.hibernate.dialect.Oracle10gDialect
true

































struts.xml:

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"/dtds/struts-2.3.dtd">






/list.jsp



list



list



list



<

result name="get" type="dispatcher">/update.jsp



/find.jsp





相关文档
最新文档