spring+mybatis实现图书管理系统

时间:2021-05-19

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

一、流程

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

二、图书管理系统

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<Book> getBooks(); public void insertBook(Book book); public void deleteBookById(Integer id); public void updateBook(Book book); public Book findById(Integer id); }

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

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.dao.BookMapper"> <select id="getBooks" resultType="com.entity.Book"> select * from book </select> <insert id="insertBook" > insert into book (name,author,bookconcern,date,synopsis,pic)values(#{name},#{author},#{bookconcern},#{date},#{synopsis},#{pic}); </insert> <delete id="deleteBookById" > delete from book where id=#{id} </delete> <update id="updateBook"> update book set name=#{name},author=#{author},bookconcern=#{bookconcern},date=#{date},synopsis=#{synopsis},pic=#{pic} where id=#{id} </update> <select id="findById" parameterType="int" resultType="com.entity.Book"> select * from book where id=#{id}; </select> </mapper>

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; @Controllerpublic class BookController { @Autowired BookService bookService; @RequestMapping("/getbooks") public String books(Map<String,Object> map){ List<Book> 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<Book> 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

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="jdbcTypeForNull" value="NULL"/> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> <databaseIdProvider type="DB_VENDOR"> <property name="MySQL" value="mysql"/> <property name="SQL Server" value="sqlserver"/> </databaseIdProvider> </configuration>

8、spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http:///jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>修改图书信息</title> </head> <body> <form action="updateBook" method="post" enctype="multipart/form-data" > <table border="5" align="center" width=70% height=60%> <tr> <td colspan="2"><h1>修改图书信息</h1></td> </tr> <tr> <td>编号:</td> <td><input type="text" name="id" value="${book.id}" readonly="readonly" size=85/></td> </tr> <tr> <td>书名:</td> <td><input type="text" name="name" value="${book.name}" size=85/></td> </tr> <tr> <td>作者:</td> <td><input type="text" name="author" value="${book.author}" size=85/></td> </tr> <tr> <td>出版社:</td> <td><input type="text" name="bookconcern" value="${book.bookconcern}" size=85/></td> </tr> <tr> <td>出版时间:</td> <td><input type="text" name="date" value="${book.date}" size=85/></td> </tr> <tr> <td>简介:</td> <td><input type="text" name="synopsis" value="${book.synopsis}" size=85/></td> </tr> <tr><td>图书图片</td><td><c:if test="${book.pic !=null}"><img src="/pic/${book.pic}" width=100 height=100/><br/></c:if><input type="file" name="book_pic"/> </td></tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> <input type="button" value="返回首页" οnclick="javascript:window.location.href ='index.jsp';"/> </td> </tr> </table> </form> </body></html>

13、所需的jar包

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

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章