Step1:数据库设计
use ibatisstudy;
drop table if exists sbook;
create table if not exists sbook
(
id int primary key auto_increment,
title varchar(50),
author varchar(20),
total int,
price float,
isbn varchar(20),
publisher varchar(50)
);
Step2:搭建框架,创建web项目,导入jar包:
Struts2支持:
commons-collections.jar
commons-dbcp.jar
commons-logging-1.0.4.jar
commons-pool.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar
Spring支持
spring.jar
aspectjweaver.jar
antlr-2.7.2.jar
Struts2和Spring整合:
struts2-spring-plugin-2.0.11.2.jar
ibatis支持
ibatis-2.3.4.726.jar
数据库驱动:
mysql-connector-java-5.1.7-bin.jar
(仅仅需要导入这些就足矣)
Step3:配置SSI2三部分各部分需要的配置文件:
1、Web.xml部分
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
WEB-INF/classes/com/us/jack/config/applicationContext.xml
WEB-INF/classes/com/us/jack/config/applicationContext-services.xml
org.springframework.web.context.ContextLoaderListener
org.apache.struts2.dispatcher.FilterDispatcher
2、Struts2部分:
struts.properties部分:
struts.i18n.encoding=utf-8
struts.xml部分:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
sbookAction.xml部分:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
3、Spring部分:
applicationContext.xml部分:
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
destroy-method="close">
applicationContext-services.xml部分:
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
dataAccessContext.xml部分:
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
4、IBatis部分:
sqlMapConfig.xml部分:
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
SBook.xml部分:
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
SELECT LAST_INSERT_ID() AS VALUE
]]>
INSERT INTO sbook(title,author,total,price,isbn,publisher)
VALUES(#title#,#author#,#total#,#price#,#isbn#,#publisher#)
]]>
DELETE FROM SBOOK WHERE ID=#id#
]]>
UPDATE SBOOK SET
title=#title#,author=#author#,price=#price#,total=#total#,isbn=#isbn#,publisher=#publisher#
WHERE id=#id#
]]>
Step4:后台代码部分:
1、pojo实体类部分:SBook.java
package com.us.jack.pojo;
/**
* 图书类
* @author jack
*
*/
public class SBook {
private int id;
private String title;
private String author;
private int total;
private float price;
private String isbn;
private String publisher;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setPrice(float price) {
this.price = price;
}
public float getPrice(){
return price;
}
}
2、DAO层:
ISBookDAO.java文件:
package com.us.jack.dao;
import java.util.List;
import com.us.jack.pojo.SBook;
public interface ISBookDAO {
/**
* 添加一本图书至数据库中
*
* @param book
* 图书对象
* @throws RuntimeException
*/
public void saveBook(SBook book) throws RuntimeException;
/**
* 删除图书信息
*
* @param id
* ID编码
* @throws RuntimeException
*/
public void deleteBook(int id) throws RuntimeException;
/**
* 通过出版社查找此出版社出版的所有图书列表
*
* @param publisher
* 出版社名称
* @return 返回特定出版社下的所有图书列表
* @throws RuntimeException
*/
public List
throws RuntimeException;
/**
* 通过图书对应的唯一的ISBN号查找图书
*
* @param isbn
* 图书唯一的ISBN号码
* @return 返回此ISBN号对应的图书
* @throws RuntimeException
*/
public SBook findBookByISBN(String isbn) throws RuntimeException;
/**
* 更新一本图书的信息
*
* @param book
* 图书对象
* @throws RuntimeException
*/
public void updateBook(SBook book) throws RuntimeException;
/**
* 查找库中所有的图书
*
* @return 返回图书列表
* @throws RuntimeException
*/
public List findAllBook() throws RuntimeException;
/**
* 通过ID查找特定的图书
*
* @param id
* 图书的ID号
* @return 返回此ID对应的图书信息
* @throws RuntimeException
*/
public SBook findBookById(int id) throws RuntimeException;
}
SBookDAO.java文件:
package com.us.jack.dao.impl;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.us.jack.dao.ISBookDAO;
import com.us.jack.pojo.SBook;
public class SBookDAO extends SqlMapClientDaoSupport implements ISBookDAO {
public SBook findBookByISBN(String isbn) throws RuntimeException {
return (SBook) this.getSqlMapClientTemplate().queryForObject(
"findBookByISBN", isbn);
}
@SuppressWarnings("unchecked")
public List
throws RuntimeException {
return this.getSqlMapClientTemplate().queryForList(
"findBookByPublisher", publisher);
}
public void saveBook(SBook book) throws RuntimeException {
this.getSqlMapClientTemplate().insert("saveBook", book);
}
public void deleteBook(int id) throws RuntimeException {
this.getSqlMapClientTemplate().delete("deleteBook", id);
}
public void updateBook(SBook book) throws RuntimeException {
this.getSqlMapClientTemplate().update("updateBook", book);
}
@SuppressWarnings("unchecked")
public List findAllBook() throws RuntimeException {
return this.getSqlMapClientTemplate().queryForList("findAllBook");
}
public SBook findBookById(int id) throws RuntimeException {
return (SBook) this.getSqlMapClientTemplate().queryForObject(
"findBookById", id);
}
}
3、业务逻辑层部分
ISBookServices.java文件:
package com.us.jack.services;
import java.util.List;
import com.us.jack.pojo.SBook;
public interface ISBookServices {
/**
* 添加一本图书至数据库中
*
* @param book
* 图书对象
* @throws RuntimeException
*/
public void saveBook(SBook book) throws RuntimeException;
/**
* 删除图书信息
*
* @param id
* ID编码
* @throws RuntimeException
*/
public void removeBook(int id) throws RuntimeException;
/**
* 通过出版社查找此出版社出版的所有图书列表
*
* @param publisher
* 出版社名称
* @return 返回特定出版社下的所有图书列表
* @throws RuntimeException
*/
public List
throws RuntimeException;
/**
* 通过图书对应的唯一的ISBN号查找图书
*
* @param isbn
* 图书唯一的ISBN号码
* @return 返回此ISBN号对应的图书
* @throws RuntimeException
*/
public SBook getBookByISBN(String isbn) throws RuntimeException;
/**
* 更新一本图书的信息
*
* @param book
* 图书对象
* @throws RuntimeException
*/
public void updateBook(SBook book) throws RuntimeException;
/**
* 查找库中所有的图书
*
* @return 返回图书列表
* @throws RuntimeException
*/
public List getAllBook() throws RuntimeException;
/**
* 通过图书ID号得到图书对象
*
* @param id
* 图书ID号码
* @return 返回此ID对应的图书信息
* @throws RuntimeException
*/
public SBook getBookById(int id) throws RuntimeException;
}
SBookServices.java文件:
package com.us.jack.services.impl;
import java.util.List;
import com.us.jack.dao.ISBookDAO;
import com.us.jack.pojo.SBook;
import com.us.jack.services.ISBookServices;
public class SBookServices implements ISBookServices {
private ISBookDAO sbookDAO;
@SuppressWarnings("unchecked")
public List getAllBook() throws RuntimeException {
return sbookDAO.findAllBook();
}
public SBook getBookByISBN(String isbn) throws RuntimeException {
return sbookDAO.findBookByISBN(isbn);
}
public List
throws RuntimeException {
return sbookDAO.findBooksByPublisher(publisher);
}
public void removeBook(int id) throws RuntimeException {
sbookDAO.deleteBook(id);
}
public void saveBook(SBook book) throws RuntimeException {
sbookDAO.saveBook(book);
}
public void updateBook(SBook book) throws RuntimeException {
sbookDAO.updateBook(book);
}
public SBook getBookById(int id) throws RuntimeException {
return sbookDAO.findBookById(id);
}
public void setSbookDAO(ISBookDAO sbookDAO) {
this.sbookDAO = sbookDAO;
}
}
4、控制层
SBookAction.JAVA文件:
package com.us.jack.action;
import java.util.List;
import com.us.jack.pojo.SBook;
import com.us.jack.services.ISBookServices;
public class SBookAction {
private ISBookServices sbookServices;
private SBook sbook;
private String tips;
private String bookId;
@SuppressWarnings("unchecked")
private List bookList;
/**
* 添加图书信息
*
* @return 返回添加是否成功
*/
public String addSBook() {
String result = "error";
try {
sbookServices.saveBook(sbook);
this.setTips("添加成功");
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题");
}
return result;
}
/**
* 查看所有图书
*
* @return
*/
public String viewSBook() {
String result = "error";
try {
bookList = sbookServices.getAllBook();
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题,请稍后访问");
}
return result;
}
/**
* 修改图书信息
*
* @return
*/
public String modifySBook() {
String result = "error";
try {
sbook = sbookServices.getBookById(Integer.parseInt(this.getBookId()));
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题");
}
return result;
}
public String updateSBook(){
String result = "error";
try{
sbookServices.updateBook(sbook);
result = "success";
}catch(Exception e){
e.printStackTrace();
this.setTips("更新操作失败");
}
return result;
}
/**
* 删除图书
* @return
*/
public String removeSBook(){
String result = "error";
try{
sbookServices.removeBook(Integer.parseInt(this.getBookId()));
result = "success";
}catch(Exception e){
e.printStackTrace();
this.setTips("删除操作失败");
}
return result;
}
public SBook getSbook() {
return sbook;
}
public void setSbook(SBook sbook) {
this.sbook = sbook;
}
public void setSbookServices(ISBookServices sbookServices) {
this.sbookServices = sbookServices;
}
@SuppressWarnings("unchecked")
public List getBookList() {
return bookList;
}
@SuppressWarnings("unchecked")
public void setBookList(List bookList) {
this.bookList = bookList;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
}
Step5:前台页面的开发:
1、增加页面文件addBook.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
库存图书 | ||||||
---|---|---|---|---|---|---|
添加新书 | ||||||
书名 | 作者 | 价格 | 库存量 | ISBN号 | 出版社 | 操作 |
修改信息 删除 |
到此为止用Struts2.1.8+Spring2.5.6+ibatis整合开发框架来实现一个图书管理模块的增删查改功能已经完整地实现了......