基于spring,SpringMVC,Mybatis的图书管理系统,增删改查以及登录,拦截器功能。
介绍:首先介绍一下需要的工具与环境配置等等:
我们需要的有:工具IDEA,jdk,maven,Tomcat,mysql数据库(使用Navicat工具管理方便)
接下这个图书馆管理系统是本人学习ssm框架所搭建。
一:下面是SQL表
二:下创建一个maven项目:目录如下
三:导入相关的依赖
4.0.0
org.example
ssmbuild
1.0-SNAPSHOT
junit
junit
4.12
test
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
provided
javax.servlet.jsp.jstl
jstl-api
1.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.3.16
org.aspectj
aspectjweaver
1.8.13
org.jetbrains
annotations
RELEASE
compile
taglibs
standard
1.1.2
org.webjars.bower
jquery
3.2.1
src/main/resources
**/*.properties
**/*.xml
true
src/main/java
**/*.properties
**/*.xml
true
8
8
四:在resources创建如下,代码直接给大家了有不理解的地方欢迎评论咨询
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
五:写接口
创建实体类Books 要添加setget方法有参无参构造器等等就自己添加不截下面的代码了。
在Dao层写接口以及mapper.xml
package com.okchan.dao;
import com.okchan.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author Ok chan
*/
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
// 你可以使用@Param注解改成("bookId") 为了可读性
int deleteBookId(@Param("bookId") int id);
//修改一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(@Param("bookId")int id);
//查询全部
List queryAllBook();
//搜索功能
Books queryBookByName(@Param("bookName") String bookName);
}
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into ssmbuild.books (bookName, bookCounts, detail)
VALUES (#{bookName},#{bookCounts},#{detail})
DELETE FROM ssmbuild.books WHERE bookID=#{bookId}
UPDATE ssmbuild.books
SET bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
WHERE bookID=#{bookID}
SELECT * FROM ssmbuild.books WHERE bookID=#{bookId}
SELECT * FROM ssmbuild.books
SELECT * FROM ssmbuild.books WHERE bookName=#{bookName}
在service层写一个接口然后实现方法
package com.okchan.service;
import com.okchan.pojo.Books;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Ok chan
* 业务层
*/
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
// 你可以使用@Param注解改成("bookId") 为了可读性
int deleteBookId(int id);
//修改一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部
List queryAllBook();
//搜索功能
Books queryBookByName(@Param("bookName") String bookName);
}
BookServiceImpl:
package com.okchan.service;
import com.okchan.dao.BookMapper;
import com.okchan.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Ok chan
* 业务层掉Dao层
*/
@Service //这里在spring-service.xml 中用配置代替了
public class BookServiceImpl implements BookService {
//组合dao
@Autowired //这里在spring-service.xml 中用配置代替了
private BookMapper bookMapper;
//给一个set方法 让spring托管他
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}
@Override
public int deleteBookId(int id) {
return bookMapper.deleteBookId(id);
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List queryAllBook() {
return bookMapper.queryAllBook();
}
@Override
public Books queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
}
接下来在控制层写一个实现类如下:
package com.okchan.controller;
import com.okchan.pojo.Books;
import com.okchan.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.RequestMapping;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
//调用service层
@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 toAddPaper(){
return "addBook";
}
//添加书籍的请求
@RequestMapping("addBook")
public String addBook(Books books){
System.out.println("addBook=>"+books);
bookService.addBook(books);
return "redirect:/book/allBook";//重定向到我们的@RequestMapping("/allBook")
}
//跳转到修改页面
@RequestMapping("/toUpdate")
public String toUpdatePaper(int id,Model model){
Books books = bookService.queryBookById(id);
model.addAttribute("QBooks",books);
return "updateBook";
}
//修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books books){
System.out.println("updateBook=>"+books);
int book = bookService.updateBook(books);
return "redirect:/book/allBook";
}
//删除图书
@RequestMapping("/deleteBook")
public String deleteBook(int id){
int bookId = bookService.deleteBookId(id);
return "redirect:/book/allBook";
}
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
Books books = bookService.queryBookByName(queryBookName);
List list = new ArrayList<>();
list.add(books);
if (books==null){
list = bookService.queryAllBook();
model.addAttribute("error","未查到");
}
model.addAttribute("list",list);
return "allBook";
}
}
然后在config层写拦截器:
package com.okchan.config;
import com.okchan.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author Ok chan
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//获取请求的RUi:去除http:localhost:8080这部分剩下的
String uri = request.getRequestURI();
//UTL:除了login.jsp是可以公开访问的,其他的URL都进行拦截控制
if (uri.indexOf("/login") >= 0) {
return true;
}
//获取session
HttpSession session = request.getSession();
User user = (User) session.getAttribute("USER_SESSION");
//判断session中是否有用户数据,如果有,则返回true,继续向下执行
if (user != null) {
return true;
}
//不符合条件的给出提示信息,并转发到登录页面
request.setAttribute("msg", "您还没有登录,请先登录!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception {
}
}
在controller层写一个登陆方法这里直接写死登陆:
package com.okchan.controller;
import com.okchan.pojo.User;
import com.okchan.service.UserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
/**
* @author Ok chan
* //调用service层
*/
@Controller
public class UserController {
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String toLogin(){
return "login";
}
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(User user, Model model, HttpSession session){
//获取用户名和密码
String username=user.getUserName();
String password=user.getUserPassword();
if(username!=null&&username.equals("写你的登陆名字")&&password!=null&&password.equals("写你的登陆密码")){
//将用户对象添加到Session中
session.setAttribute("USER_SESSION",user);
//重定向到主页面的跳转方法
return "redirect:book/allBook";
}
model.addAttribute("msg","用户名或密码错误,请重新登录!");
return "login";
}
@RequestMapping(value = "/logout")
public String logout(HttpSession session){
//清除session
session.invalidate();
//重定向到登录页面的跳转方法
return "redirect:login";
}
}
接下来是jsp代码:
目录如下:
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
springmvc
org.springframework.web.servlet.DispatcherServlet
classpath:applicationContext.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encodingFilter
/*
15
addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--Bootstrap--%>
allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
书籍展示
书籍编号
书籍名称
书籍数量
书籍详情
操作
${book.bookID}
${book.bookName}
${book.bookCounts}
${book.detail}
修改
|
删除
login.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录页面
<%--BootStrap美化界面--%>
updateBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改书籍页面
<%--Bootstrap--%>
上面就是基于ssm框架搭建的图书管理系统希望对大家有帮助。