spring+mybatis实现图书管理系统

本文为大家分享了spring+mybatis实现的图书管理系统,供大家参考,具体内容如下

一、流程

jsp页面发起请求-->控制器-->控制器通过一个service对象调用service方法-->service中通过xxxMapper对象调用dao中的方法-->查询数据库

二、图书管理系统

1、目录结构

spring+mybatis实现图书管理系统_第1张图片

2、Book实体类

package com.entity;
 
import java.io.Serializable;
import java.util.Map;
import org.apache.ibatis.type.Alias;
 
public class Book implements Serializable{
 
 
 private static final long serialVersionUID = 1L;
 private Integer id;
 private String name;
 private String author;
 private String bookconcern;
 private String date;
 private String synopsis; 
 private String pic;
 
 public String getPic() {
 return pic;
 }
 public void setPic(String pic) {
 this.pic = pic == null ? null : pic.trim();
 }
 
 public Book() {
 super();
 }
 public Book(Integer id, String name, String author,String bookconcern,String date,String synopsis,String pic) {
 super();
 this.id = id;
 this.name = name;
 this.author = author;
 this.bookconcern = bookconcern;
 this.date =date;
 this.synopsis = synopsis;
 this.pic = pic == null ? null : pic.trim();
 }
 
 
 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 getBookconcern() {
 return bookconcern;
 }
 public void setBookconcern(String bookconcern) {
 this.bookconcern = bookconcern;
 }
 public String getDate() {
 return date;
 }
 
 
 public void setDate(String date) {
 this.date = date;
 }
 
 
 public String getSynopsis() {
 return synopsis;
 }
 
 
 public void setSynopsis(String synopsis) {
 this.synopsis = synopsis;
 }
 public String toString() {
 return "Book [id=" + id + ", name=" + name + ", author="
 + author + ", bookconcern=" + bookconcern + ", date=" + date + ", synopsis=" + synopsis +", pic=" + pic+"]";
 }
 
}

3、BookMapper增删改查接口

package com.dao;
 
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.entity.Book;
 
public interface BookMapper {
 
 public Book getBookById(Integer id);
 public List getBooks(); 
 public void insertBook(Book book);
 public void deleteBookById(Integer id);
 public void updateBook(Book book);
 public Book findById(Integer id);
 
}

4、BookMapper.xml实现增删改查操作




 
 
 
 insert into book
 (name,author,bookconcern,date,synopsis,pic)values(#{name},#{author},#{bookconcern},#{date},#{synopsis},#{pic});
 
 
 
 delete from book
 where id=#{id}
 
 
 
 update book set
 name=#{name},author=#{author},bookconcern=#{bookconcern},date=#{date},synopsis=#{synopsis},pic=#{pic} where
 id=#{id} 
 
 
 
 

5、控制类

package com.controller;
 
 
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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.multipart.MultipartFile;
import com.entity.Book;
import com.service.BookService;
 
 
@Controller
public class BookController {
 
 @Autowired
 BookService bookService;
 
 @RequestMapping("/getbooks")
 public String books(Map map){ 
 List books = bookService.getBooks();
 map.put("allBooks", books);
 return "allbooks";
 }
 
 @RequestMapping(value="/insert",produces="text/html;charset=UTF-8")
 public String insert(){ 
 return "insert";
 }
 
 @RequestMapping(value="/insertBook")
 public String insertBook(Book book,MultipartFile book_pic) throws IllegalStateException, IOException{
 
 String originalFilename = book_pic.getOriginalFilename();
 if(book_pic!=null && originalFilename!=null && originalFilename.length()>0){ 
 String pic_path = "E:\\spring_mybatis\\picture\\"; 
 String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
 File newFile = new File(pic_path+newFileName); 
 book_pic.transferTo(newFile); 
 book.setPic(newFileName); 
 }
 bookService.insertBook(book);
 return "redirect:getbooks";
 }
 
 @RequestMapping(value="/deleteBookById")
 public String deleteBookById(Integer id){
 bookService.deleteBookById(id);
 return "redirect:getbooks";
 }
 
 @RequestMapping(value="/findById")
 public String findById(Model model,Integer id){
 Book book=bookService.findById(id);
 model.addAttribute("book",book);
 return "update";
 }
 
 @RequestMapping(value="/updateBook")
 public String updateBook(Book book,MultipartFile book_pic) throws IllegalStateException, IOException{ 
 
 String originalFilename = book_pic.getOriginalFilename();
 if(book_pic!=null && originalFilename!=null && originalFilename.length()>0){
 String pic_path = "E:\\spring_mybatis\\picture\\"; 
 String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
 File newFile = new File(pic_path+newFileName); 
 book_pic.transferTo(newFile); 
 book.setPic(newFileName); 
 } 
 bookService.updateBook(book); 
 return "redirect:getbooks";
 
 }
 
}

6、BookService操作

package com.service;
 
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.dao.BookMapper;
import com.entity.Book;
 
@Service //标识这是一个业务类
public class BookService {
 
 @Autowired //用@Autowired将BookMapper的接口对象注入到spring中
 private BookMapper bookMapper; 
 @Autowired
 private SqlSession sqlSession;
 
 public List getBooks(){
 return bookMapper.getBooks();
 }
 
 public void insertBook(Book book) {
 bookMapper.insertBook(book); 
 }
 
 public void deleteBookById(Integer id) {
 bookMapper.deleteBookById(id);
 }
 
 public void updateBook(Book book) {
 bookMapper.updateBook(book);
 }
 
 public Book findById(Integer id) { 
 return bookMapper.findById(id);
 } 
}

7、mybatis配置文件mybatis-config.xml




 
 
 
  
 
 
 
 
 
 
 
 
 
 

8、spring配置文件applicationContext.xml



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

9、连接数据库信息dbconfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/login?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

10、web.xml配置



 spring_mybatis
 
 
 contextConfigLocation
 classpath:applicationContext.xml
 
 
 
 org.springframework.web.context.ContextLoaderListener
 
 
 
 characterEncodingFilter
 org.springframework.web.filter.CharacterEncodingFilter
 
 encoding
 UTF-8
 
 
 forceEncoding
 true
 
 
 
 characterEncodingFilter
 /*
 
 
 
 spring
 org.springframework.web.servlet.DispatcherServlet
 1
 
 
 spring
 /
 
 

11、spring mvc配置文件spring-servlet.xml



 
 
 
 
 
 
 
 
 
 
 
 
 
 

12、jsp页面

(1)index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




首页
 


 
 
 

(2)allbooks.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




所有图书
 


 
 

所有图书

编号 书名 作者 出版社 出版时间 简介 图书图片 操作
${book.id } ${book.name } ${book.author } ${book.bookconcern } ${book.date } ${book.synopsis }
删除|修改

(3)insert.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


  
 添加图书
 
 
 

添加图书

书籍名称:
书籍作者:
出版社:
出版时间:
简介:
图书图片

(4)update.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


  
  
 修改图书信息
 
 
 
 

修改图书信息

编号:
书名:
作者:
出版社:
出版时间:
简介:
图书图片

13、所需的jar包

spring+mybatis实现图书管理系统_第2张图片

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(spring+mybatis实现图书管理系统)