写这个小练习的目的是巩固最近所学的知识,它的功能是将数据库的数据通过前端展示出来,并且在页面上有增、删、改的操作。界面如下所示:
注意:为了方便理解,这里所有配置文件和Java类中的代码都是一次性的展示完,并要仔细观看理解代码中的注释部分。
实现需要软件:
MySQL
sqlyog
IDEA
Tomcat
项目分布:
1.首先在sqlyog中创建数据库
打开sqlyog,创建数据库,创建表books,表数据如下:
2.打开IDEA,创建一个maven的新项目
在pom.xml中需要导入一系列的包
4.0.0
com.muzili
SSMbuild1
1.0-SNAPSHOT
war
SSMbuild1 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
SSMbuild1
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
然后在IDEA中连接刚创建的数据库;
创建database.properties文件
jdbc.Driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
创建mybatis的配置文件mybatis-config.xml文件
创建spring的相关配置文件,这里为了方便理解,分别创建了spring-dao.xml、spring-service.xml、spring-mvc.xml文件,最后将这三合并到applicationContext.xml中。
aplicationContext.xml文件:
spring-dao.xml
spring-service.xml文件:
spring-mvc.xml文件:
配置web下的web.xml文件:
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
配置文件编写完,下面创建一个Books实体类(这里的属性名最好与数据库中的保持一致)
package com.muzili.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 + '\'' +
'}';
}
}
在dao层创建BookDao和BookMapper.xml
package com.muzili.dao;
import com.muzili.pojo.Books;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
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
Books queryBookByID(@Param("bookID") int id);
//查看全部的Book
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}
编写业务层service中的BookService和BookServiceImpl:
public interface BookService {
//增加一个Books
int addBook(Books books);
//根据id删除Books
int deleteBookByID(int id);
//更新Books
int updateBook(Books books);
//根据id查询Book
Books queryBookByID(int id);
//查看全部的Book
List queryAllBook();
}
package com.muzili.service;
import com.muzili.dao.BookDao;
import com.muzili.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService {
//组合Dao
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public int addBook(Books books) {
return bookDao.addBook(books);
}
@Override
public int deleteBookByID(int id) {
return bookDao.deleteBookByID(id);
}
@Override
public int updateBook(Books books) {
return bookDao.updateBook(books);
}
@Override
public Books queryBookByID(int id) {
return bookDao.queryBookByID(id);
}
@Override
public List queryAllBook(){
return bookDao.queryAllBook();
}
}
编写控制类BookController:
package com.muzili.controller;
import com.muzili.pojo.Books;
import com.muzili.service.BookService;
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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" %>
首页
进入书籍展示页面
前端展示所有书籍页面:addBook.jsp (这里引用了bookstarp中的样式)
<%@ 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" %>
修改书籍
<%--书籍原来的信息,修改按钮--%>
修改书籍信息
所有文件编写完毕,关联tomcat后,便可以运行。