本文数据库使用MySQL 5.6.24,使用SQLyog来进行操作。
首先创建一个名为library的数据库,在数据库中创建一张名为book的表,表结构如下图所示(注意引擎、字符集、核对)。
alimaven
central
aliyun maven
http://maven.aliyun.com/nexus/content/repositories/central/
4. 选择maven的路径,可以选择IDEA自带的maven,也可以选择自己安装的maven;User settings File选择第一步中maven的安装路径下的setting.xml文件。如果C盘的用户名下有.m2文件夹,可以将setting.xml文件复制到.m2文件夹下,在此处选择.m2文件夹下的setting.xml文件。
5. 出现了BUILD SUCCESS即完成了工程的创建。此时右下角可能会出现一个小提示框:Maven projects need to be imported,选择Enable Auto-import。
UTF-8
1.8
1.8
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
接着修改dependencies标签:
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
com.fasterxml.jackson.core
jackson-databind
2.9.4
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
ch.qos.logback
logback-classic
1.1.1
package com.pojo;
public class Book {
private String bookId;
private String bookName;
private String bookAuthor;
private String bookContent;
private String bookAddDate;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookContent() {
return bookContent;
}
public void setBookContent(String bookContent) {
this.bookContent = bookContent;
}
public String getBookAddDate() {
return bookAddDate;
}
public void setBookAddDate(String bookAddDate) {
this.bookAddDate = bookAddDate;
}
}
package com.dao;
import com.pojo.Book;
import java.util.List;
public interface BookDao {
int addBook(Book book);
int deleteBookById(String id);
int updateBook(Book book);
Book queryById(String id);
List queryAllBook();
}
package com.service;
import com.pojo.Book;
import java.util.List;
public interface BookService {
int addBook(Book book);
int deleteBookById(String id);
int updateBook(Book book);
Book queryById(String id);
List queryAllBook();
}
package com.service.impl;
import com.dao.BookDao;
import com.pojo.Book;
import com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public int addBook(Book book) {
return bookDao.addBook(book);
}
@Override
public int deleteBookById(String id) {
return bookDao.deleteBookById(id);
}
@Override
public int updateBook(Book book) {
return bookDao.updateBook(book);
}
@Override
public Book queryById(String id) {
return bookDao.queryById(id);
}
@Override
public List queryAllBook() {
return bookDao.queryAllBook();
}
}
package com.controller;
import com.pojo.Book;
import com.service.BookService;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
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(Book book) {
bookService.addBook(book);
return "redirect:/book/allBook";
}
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") String id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
@RequestMapping("toUpdateBook")
public String toUpdateBook(Model model, String id) {
model.addAttribute("book", bookService.queryById(id));
return "updateBook";
}
@RequestMapping("/updateBook")
public String updateBook(Model model, Book book) {
bookService.updateBook(book);
book = bookService.queryById(book.getBookId());
model.addAttribute("book", book);
return "redirect:/book/allBook";
}
}
INSERT INTO book(id,name,author,content,add_date) VALUE (#{bookId},#{bookName}, #{bookAuthor}, #{bookContent}, #{bookAddDate})
DELETE FROM book WHERE id=#{bookId}
UPDATE book
SET name = #{bookName}, author = #{bookAuthor}, content = #{bookContent}
WHERE id = #{bookId}
spring-mvc.xml(spring mvc配置文件)文件内容:
spring-service.xml文件内容:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/library?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Archetype Created Web Application
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
dispatcher
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
新增图书
图书管理系统
新增图书
allBook.jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String appPath = request.getContextPath(); %>
Book列表
updateBook.jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
修改图书信息
图书管理系统
修改图书信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
首页
图书管理系统
点击进入管理页面
https://blog.csdn.net/weixin_42747657/article/details/81160501