A、实现语言
Java
B、环境要求
JDK1.8、Eclipse、Tomcat7、SpringMVC、Spring、Mybatis、Mysql、Maven
使用SSM(SpringMVC+Spring+MyBatis)实现图书管理系统,MySql作为后台数据库,该系统包括用户登录,图书信息列表、图书的新增、修改、删除,并实现分页功能,具体要求如下:
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`author` varchar(100) NOT NULL,
`publish` varchar(100) NOT NULL,
`publishdate` date NOT NULL,
`page` int(11) DEFAULT NULL,
`price` decimal(8,2) DEFAULT NULL,
`content` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('1', 'JSP编程', '李四', '电力出版社', '2011-12-02', '300', '40.00', 'JSP编程开发');
INSERT INTO `book` VALUES ('2', 'JSP编程2', '张三', '电力出版社', '2011-12-02', '150', '16.80', 'JSP编程开发');
INSERT INTO `book` VALUES ('3', 'JSP编程3', '张三', '电力出版社', '2011-12-02', '150', '16.80', 'JSP编程开发');
INSERT INTO `book` VALUES ('7', 'java基础', 'tom', '电力', '2010-10-10', '200', '30.00', 'test');
package com.neu.bean;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class Book {
private Integer id;
private String name;
private String author;
private String publish;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date publishdate;
private Integer page;
private BigDecimal price;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Date getPublishdate() {
return publishdate;
}
public void setPublishdate(Date publishdate) {
this.publishdate = publishdate;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
<script type="text/javascript">
function goPage(){
var page = document.getElementById("page").value;
location = "${pageContext.request.contextPath }/book/getPaged.action?pageNum="+page;
}
function del(id){
if(confirm("是否删除?")){
location = "${pageContext.request.contextPath }/book/delete.action?id="+id;
}
}
function add(){
location = "${pageContext.request.contextPath }/book/getadd.action";
}
script>
head>
<body>
<table border="1" width="600">
<c:forEach items="${list }" var="book">
<tr>
<td>${ book.name }td>
<td>${ book.author }td>
<td>${ book.publish }td>
<td><fmt:formatDate value="${ book.publishdate }" pattern="yyyy-MM-dd"/> td>
<td>${ book.page }td>
<td>${ book.price }td>
<td>${ book.content }td>
<td>
<a href="${pageContext.request.contextPath }/book/edit.action?id=${book.id}">编辑a>
<a href="#" onclick="del(${book.id})">删除a>
td>
tr>
c:forEach>
<tr>
<td colspan="8">
<input type="button" value="新增图书" onclick="add()">
共 ${ count } 条记录
每页 <input type="text" size="1" value="${ pageSize }">条
第 ${ pageNum } 页/共${ pageCount } 页
<c:if test="${ pageNum ==1 }">
<a>第一页a>
c:if>
<c:if test="${ pageNum > 1 }">
<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=1">第一页a>
c:if>
<c:if test="${ pageNum ==1 }">
<a>上一页a>
c:if>
<c:if test="${ pageNum > 1 }">
<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${pageNum -1}">上一页a>
c:if>
<c:if test="${ pageNum ==pageCount }">
<a>下一页a>
c:if>
<c:if test="${ pageNum < pageCount }">
<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${pageNum +1}">下一页a>
c:if>
<c:if test="${ pageNum ==pageCount }">
<a>最后一页a>
c:if>
<c:if test="${ pageNum < pageCount }">
<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${ pageCount }">最后一页a>
c:if>
转到第<input type="text" size="1" name="page" id="page"> <input type="button" value="go" onclick="goPage()">
td>
tr>
table>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="${ pageContext.request.contextPath }/book/update.action?id=${ book.id }" method="post">
<table>
<tr>
<td>书名:td>
<td><input type="text" name="name" value="${ book.name }">td>
tr>
<tr>
<td>作者:td>
<td><input type="text" name="author" value="${ book.author }">td>
tr>
<tr>
<td>出版社:td>
<td><input type="text" name="publish" value="${ book.publish }">td>
tr>
<tr>
<td>出版日期:td>
<td><input type="text" name="publishdate" value='" ${ book.publishdate }" pattern="yyyy-MM-dd" />'> td>
tr>
<tr>
<td>页数:td>
<td><input type="text" name="page" value="${ book.page }">td>
tr>
<tr>
<td>价格:td>
<td><input type="text" name="price" value="${ book.price }">td>
tr>
<tr>
<td>内容摘要:td>
<td>
<textarea name="content" rows="3" cols="10">${ book.content }textarea>
td>
tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
td>
tr>
table>
form>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="${ pageContext.request.contextPath }/book/add.action" method="post">
<table>
<tr>
<td>书名:td>
<td><input type="text" name="name">td>
tr>
<tr>
<td>作者:td>
<td><input type="text" name="author">td>
tr>
<tr>
<td>出版社:td>
<td><input type="text" name="publish">td>
tr>
<tr>
<td>出版日期:td>
<td> td>
tr>
<tr>
<td>页数:td>
<td><input type="text" name="page">td>
tr>
<tr>
<td>价格:td>
<td><input type="text" name="price">td>
tr>
<tr>
<td>内容摘要:td>
<td>
<textarea name="content" rows="3" cols="10">textarea>
td>
tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
td>
tr>
table>
form>
body>
html>
package com.neu.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.Page;
import com.neu.bean.Book;
import com.neu.service.BookService;
@Controller
@RequestMapping("book")
public class BookController {
@Autowired
private BookService bookSerivce;
@RequestMapping("getPaged")
public String getPaged(@RequestParam(value="pageNum",defaultValue="1") int pageNum,Model model) {
int pageSize = 5;
List<Book> list = bookSerivce.getPaged(pageSize, pageNum);
model.addAttribute("list", list);
Page<Book> page = (Page<Book>)list;
int pageCount = page.getPages();//一共有多少页
int count = (int)page.getTotal();//一共有多少行
model.addAttribute("pageCount", pageCount);
model.addAttribute("count", count);
model.addAttribute("pageSize", pageSize);
model.addAttribute("pageNum", pageNum);
return "book/getpaged";
}
@RequestMapping("delete")
public String delete(int id) {
bookSerivce.delete(id);
return "forward:/book/getPaged.action";
}
@RequestMapping("edit")
public String edit(int id,Model model) {
Book book = bookSerivce.getById(id);
model.addAttribute("book", book);
return "book/edit";
}
@RequestMapping("update")
public String update(Book book) {
bookSerivce.update(book);
return "forward:/book/getPaged.action";
}
@RequestMapping("getadd")
public String getadd() {
return "book/add";
}
@RequestMapping("add")
public String add(Book book) {
bookSerivce.insert(book);
return "forward:/book/getPaged.action";
}
}
package com.neu.service;
import java.util.List;
import com.neu.bean.Book;
public interface BookService {
public List<Book> getPaged(int pageSize,int pageNum);
public int insert(Book book);
public int update(Book book);
public int delete(int id);
public Book getById(int id);
}
package com.neu.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.bean.Book;
import com.neu.mapper.BookMapper;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List<Book> getPaged(int pageSize, int pageNum) {
List<Book> list = bookMapper.getPaged(pageSize, pageNum);
return list;
}
@Override
public int insert(Book book) {
int n = bookMapper.insert(book);
return n;
}
@Override
public int update(Book book) {
int n = bookMapper.updateByPrimaryKey(book);
return n;
}
@Override
public int delete(int id) {
int n = bookMapper.deleteByPrimaryKey(id);
return n;
}
@Override
public Book getById(int id) {
Book book = bookMapper.selectByPrimaryKey(id);
return book;
}
}
package com.neu.mapper;
import com.neu.bean.Book;
import com.neu.bean.BookExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface BookMapper {
long countByExample(BookExample example);
int deleteByExample(BookExample example);
int deleteByPrimaryKey(Integer id);
int insert(Book record);
int insertSelective(Book record);
List<Book> selectByExample(BookExample example);
Book selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Book record, @Param("example") BookExample example);
int updateByExample(@Param("record") Book record, @Param("example") BookExample example);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
//分页方法为自己添加
public List<Book> getPaged(@Param("pageSize") int pageSize,@Param("pageNum")int pageNum);
}
<mapper namespace="com.neu.mapper.BookMapper">
<resultMap id="BaseResultMap" type="com.neu.bean.Book">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="author" jdbcType="VARCHAR" property="author" />
<result column="publish" jdbcType="VARCHAR" property="publish" />
<result column="publishdate" jdbcType="DATE" property="publishdate" />
<result column="page" jdbcType="INTEGER" property="page" />
<result column="price" jdbcType="DECIMAL" property="price" />
<result column="content" jdbcType="VARCHAR" property="content" />
resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
foreach>
when>
choose>
foreach>
trim>
if>
foreach>
where>
sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
foreach>
when>
choose>
foreach>
trim>
if>
foreach>
where>
sql>
<sql id="Base_Column_List">
id, name, author, publish, publishdate, page, price, content
sql>
<select id="selectByExample" parameterType="com.neu.bean.BookExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
if>
<include refid="Base_Column_List" />
from book
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
<if test="orderByClause != null">
order by ${orderByClause}
if>
select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from book
where id = #{id,jdbcType=INTEGER}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from book
where id = #{id,jdbcType=INTEGER}
delete>
<delete id="deleteByExample" parameterType="com.neu.bean.BookExample">
delete from book
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
delete>
<insert id="insert" parameterType="com.neu.bean.Book">
insert into book (id, name, author,
publish, publishdate, page,
price, content)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR},
#{publish,jdbcType=VARCHAR}, #{publishdate,jdbcType=DATE}, #{page,jdbcType=INTEGER},
#{price,jdbcType=DECIMAL}, #{content,jdbcType=VARCHAR})
insert>
<insert id="insertSelective" parameterType="com.neu.bean.Book">
insert into book
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
if>
<if test="name != null">
name,
if>
<if test="author != null">
author,
if>
<if test="publish != null">
publish,
if>
<if test="publishdate != null">
publishdate,
if>
<if test="page != null">
page,
if>
<if test="price != null">
price,
if>
<if test="content != null">
content,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
if>
<if test="author != null">
#{author,jdbcType=VARCHAR},
if>
<if test="publish != null">
#{publish,jdbcType=VARCHAR},
if>
<if test="publishdate != null">
#{publishdate,jdbcType=DATE},
if>
<if test="page != null">
#{page,jdbcType=INTEGER},
if>
<if test="price != null">
#{price,jdbcType=DECIMAL},
if>
<if test="content != null">
#{content,jdbcType=VARCHAR},
if>
trim>
insert>
<select id="countByExample" parameterType="com.neu.bean.BookExample" resultType="java.lang.Long">
select count(*) from book
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
select>
<update id="updateByExampleSelective" parameterType="map">
update book
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
if>
<if test="record.author != null">
author = #{record.author,jdbcType=VARCHAR},
if>
<if test="record.publish != null">
publish = #{record.publish,jdbcType=VARCHAR},
if>
<if test="record.publishdate != null">
publishdate = #{record.publishdate,jdbcType=DATE},
if>
<if test="record.page != null">
page = #{record.page,jdbcType=INTEGER},
if>
<if test="record.price != null">
price = #{record.price,jdbcType=DECIMAL},
if>
<if test="record.content != null">
content = #{record.content,jdbcType=VARCHAR},
if>
set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
if>
update>
<update id="updateByExample" parameterType="map">
update book
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
author = #{record.author,jdbcType=VARCHAR},
publish = #{record.publish,jdbcType=VARCHAR},
publishdate = #{record.publishdate,jdbcType=DATE},
page = #{record.page,jdbcType=INTEGER},
price = #{record.price,jdbcType=DECIMAL},
content = #{record.content,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
if>
update>
<update id="updateByPrimaryKeySelective" parameterType="com.neu.bean.Book">
update book
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
if>
<if test="author != null">
author = #{author,jdbcType=VARCHAR},
if>
<if test="publish != null">
publish = #{publish,jdbcType=VARCHAR},
if>
<if test="publishdate != null">
publishdate = #{publishdate,jdbcType=DATE},
if>
<if test="page != null">
page = #{page,jdbcType=INTEGER},
if>
<if test="price != null">
price = #{price,jdbcType=DECIMAL},
if>
<if test="content != null">
content = #{content,jdbcType=VARCHAR},
if>
set>
where id = #{id,jdbcType=INTEGER}
update>
<update id="updateByPrimaryKey" parameterType="com.neu.bean.Book">
update book
set name = #{name,jdbcType=VARCHAR},
author = #{author,jdbcType=VARCHAR},
publish = #{publish,jdbcType=VARCHAR},
publishdate = #{publishdate,jdbcType=DATE},
page = #{page,jdbcType=INTEGER},
price = #{price,jdbcType=DECIMAL},
content = #{content,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
update>
<select id="getPaged" resultMap="BaseResultMap">
select * from book order by id
select>
mapper>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.neu.service,com.neu.mapper">context:component-scan>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}">property>
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
<property name="maxActive" value="20">property>
<property name="maxIdle" value="5">property>
<property name="maxWait" value="1000">property>
bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="classpath:SqlMapConfig.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.neu.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean">property>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.neu.controller">context:component-scan>
<bean id="formattingConversionServiceFactoryBean"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
list>
property>
bean>
<mvc:annotation-driven validator="validator" conversion-service="formattingConversionServiceFactoryBean" >mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:CustomValidationMessagesvalue>
list>
property>
<property name="fileEncodings" value="utf-8">property>
<property name="cacheSeconds" value="120">property>
bean>
<bean
id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator">property>
<property name="validationMessageSource" ref="messageSource">property>
bean>
<mvc:resources location="/js/" mapping="/js/**">mvc:resources>
<mvc:resources location="/css/" mapping="/css/**">mvc:resources>
<mvc:resources location="/images/" mapping="/images/**">mvc:resources>
beans>
<configuration>
configuration>
jdbc.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
#jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.username=scott
#jdbc.password=tiger
# Global logging configuration
#\u751F\u4EA7\u73AF\u5883\u914D\u7F6Einfo ERROR
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
https://download.csdn.net/download/pcbhyy/10767989