环境:
IDEA最新版
MySQL 5.7.19
Tomcat 9
Maven 3.6
创建一个存放书籍数据的数据库表
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
4.0.0
org.westos
xiangmu
1.0-SNAPSHOT
war
xiangmu Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
mysql
mysql-connector-java
5.1.47
org.mybatis
mybatis
3.4.6
org.mybatis
mybatis-spring
1.3.1
com.mchange
c3p0
0.9.5.2
org.springframework
spring-webmvc
4.3.24.RELEASE
org.springframework
spring-jdbc
4.3.24.RELEASE
javax.servlet
javax.servlet-api
3.0.1
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
src/main/java
**/*.xml
false
src/main/resources
**/*.xml
**/*.properties
false
SSM-build1
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
package org.westos.pojo;
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
public Books() {
}
public Books(int bookID, String bookName, int bookCounts, String detail) {
this.bookID = bookID;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Books{" +
"bookID=" + bookID +
", bookName='" + bookName + '\'' +
", bookCounts=" + bookCounts +
", detail='" + detail + '\'' +
'}';
}
}
package org.westos.dao;
import org.apache.ibatis.annotations.Param;
import org.westos.pojo.Books;
import java.util.List;
public interface BookDao {
//增加一个Books
int addBook(Books books);
//根据id删除Books
int deleteBookByID(@Param("bookID") int id);
//更新Books
int updateBook(Books books);
//根据id查询Book,返回一个book
Books queryBookByID(@Param("bookID") int id);
//查看全部的Book 返回list集合
List queryAllBook();
}
insert into ssmbuild.books (bookID,bookName,bookCounts,detail) values
(#{bookID},#{bookName},#{bookCounts},#{detail})
delete from ssmbuild.books where bookID = #{bookID}
update ssmbuild.books
set bookName = #{bookName},bookCounts=#{bookCounts},detail = #{detail}
where bookID = #{bookID}
接口
package org.westos.service;
import org.westos.pojo.Books;
import java.util.List;
public interface BookService {
//增加一个Book
int addBook(Books book);
//根据id删除一个Book
int deleteBookByID(int id);
//更新Book
int updateBook(Books books);
//根据id查询,返回一个Book
Books queryBookByID(int id);
//查询全部Book,返回list集合
List queryAllBook();
}
package org.westos.service;
import org.westos.dao.BookDao;
import org.westos.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
//调用dao层的操作,设置一个set接口,方便Spring管理
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
//增加一个Book
@Override
public int addBook(Books book) {
return bookDao.addBook(book);
}
//根据id删除一个Book
@Override
public int deleteBookByID(int id) {
return bookDao.deleteBookByID(id);
}
//更新Book
@Override
public int updateBook(Books books) {
return bookDao.updateBook(books);
}
//根据id查询,返回一个Book
@Override
public Books queryBookByID(int id) {
return bookDao.queryBookByID(id);
}
//查询全部Book,返回list集合
@Override
public List queryAllBook() {
return bookDao.queryAllBook();
}
}
web.xml
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
package org.westos.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.westos.pojo.Books;
import org.westos.service.BookService;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model) {
List list = bookService.queryAllBook();
model.addAttribute("list", list);
return "allBook";
}
//跳转到新增书籍页面
@RequestMapping("/toAddBook")
public String toAddBook(){
return "addBook";
}
//增加书籍页面
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println("调试信息addBook===="+books);
bookService.addBook(books);
return "redirect:/book/allBook"; //重定向到首页
}
//跳转到修改页面
@RequestMapping("/toUpdateBook")
public String toUpdateBook(int id,Model model){
Books books = bookService.queryBookByID(id);
System.out.println("调试信息toUpdateBook===="+books);
model.addAttribute("book",books);
return "updateBook";
}
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books books,Model model){
System.out.println("调试信息updateBook===="+books);
bookService.updateBook(books);
//更新最新的书籍
Books books1 = bookService.queryBookByID(books.getBookID());
model.addAttribute("books",books1);
System.out.println("修改后的书籍===="+books1);
return "redirect:/book/allBook"; //重定向到首页
}
//删除书籍,请使用restful风格
@RequestMapping("/del/{bookID}")
public String deleteBook(@PathVariable("bookID") int id){
bookService.deleteBookByID(id);
return "redirect:/book/allBook"; //重定向到首页
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
进入书籍列表
allbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--使用JSTL标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
全部书籍
<%--引用BootStarp--%>
addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增书籍
<%--引用BootStarp--%>
<%--显示要增加的书籍信息--%>
增加书籍
updateBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改书籍
<%--书籍原来的信息,修改按钮--%>
修改书籍信息