简易图书管理系统
Posted z啵唧啵唧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易图书管理系统相关的知识,希望对你有一定的参考价值。
简易版图书管理系统
项目说明
- 图书管理系统,简单实现书籍的增删改查功能
- 基于狂神的ssm整合技术视频 地址:【狂神说Java】SSM框架最新整合教学IDEA版_哔哩哔哩_bilibili
- 用于ssm技术复习回顾
使用技术
ssm框架技术
环境
- IDEA
- jdk1.8
- mysql5.5
- tomcat 9
- maven 3.6
创建数据库环境
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
基本环境搭建
- 创建maven项目
- 添加web支持
- 导入相关依赖
pom.xml中相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ssmbulid</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依赖问题: junit ,数据库驱动 , 连接池 ,servlet,jsp,mybatis,mybatis-spring,spring-->
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--Spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<!--静态资源导出问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
创建项目基本结构
-
com.zb.pojo
pojo层是实体类,用作对实体属性的封装
Books类
使用lombok插件!
package com.zb.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data //get,set,tostring等方法 @AllArgsConstructor //有参构造 @NoArgsConstructor //无参构造 public class Books { private int bookID; //书籍编号 private String bookName;//书籍名称 private int bookCounts;//书籍数量 private String detail;//书籍详情 }
-
com.zb.dao
dao层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此。
BookMapper接口
系统需要的一些功能
package com.zb.dao; import com.zb.pojo.Books; import org.apache.ibatis.annotatio ns.Param; import java.util.List; public interface BookMapper { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId")int id); //查询全部书 List<Books> queryAllBook(); //根据名称查询书籍 Books queryBookByName(@Param("bookName")String bookName); }
BookerMapper.xml
这里面这要写的就是需要执行的sql语句
<?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.zb.dao.BookMapper"> <!--插入书籍--> <insert id="addBook" parameterType="Books"> insert into books(bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <!--通过书籍编号删除书籍--> <delete id="deleteBookById" parameterType="int"> delete from books where bookID=#{bookId} </delete> <!--修改书籍信息--> <update id="updateBook" parameterType="Books"> update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID}; </update> <!--通过编号查询书籍--> <select id="queryBookById" resultType="Books"> select * from books where bookID=#{bookId}; </select> <!--查询所有书籍--> <select id="queryAllBook" resultType="Books"> select * from books; </select> <!--通过书籍名称查询书籍--> <select id="queryBookByName" resultType="Books" parameterType="String"> select * from books where bookName=#{bookName} </select> </mapper>
-
com.zb.service
主要负责业务模块的逻辑应用设计,Service层的业务实现,具体要调用到已定义的DAO层的接口,封装Service层的业务逻辑有利于通用的业务逻辑的独立性和重复利用性,程序显得非常简洁。
BookService 接口
package com.zb.service; import com.zb.pojo.Books; import java.util.List; public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById( int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(int id); //查询全部书 List<Books> queryAllBook(); //根据名称查询书籍 Books queryBookByName(String bookName); }
BookServiceImpl作为BookService的接口
package com.zb.service; import com.zb.dao.BookMapper; import com.zb.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService { //service层掉dao层 :组合Dao private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper=bookMapper; } public int addBook(Books books) { return bookMapper.addBook(books); } public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } public int updateBook(Books books) { return bookMapper.updateBook(books); } public Books queryBookById(int id) { return bookMapper.queryBookById(id); } public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); } }
-
com.zb.controller
controller层为控制层,主要处理外部请求。调用service层,将service层返回的BO/DO转化为DTO/VO并封装成统一返回对象返回给调用方。如果返回数据用于前端模版渲染则返回VO,否则一般返回DTO。
BookController类用来调用service层
package com.zb.controller; import com.zb.pojo.Books; import com.zb.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.util.ArrayList; import java.util.List; @Controller @RequestMapping("/book") public class BookController { //使用controller调service层 @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查询全部书籍 @RequestMapping("/allBook") public String list(Model model) { List<Books> 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("/allBook") } //跳转到修改页面 @RequestMapping("/toUpdata") public String toUpdata(int id,Model model) { Books books = bookService.queryBookById(id); model.addAttribute("Qbooks",books); return "upDataBook"; } //修改书籍功能 @RequestMapping("/Updatebook") public String Updatebook(Books books) { System.out.println("addBook"+books); bookService.updateBook(books); return "redirect:/book/allBook";//重定向到@RequestMapping("/allBook") } //跳转到删除页面 @RequestMapping("/toDelete") public String toDelete(int id,Model model) { Books books = bookService.queryBookById(id); model.addAttribute("Dbooks",books); return "DeleteBook"; } //删除书籍功能 @RequestMapping("/DeleteBook") public String DeleteBook(int id) { bookService.deleteBookById(id); return "redirect:/book/allBook";//重定向到@RequestMapping("/allBook") } //查询书籍功能 @RequestMapping("/queryBook") public String queryBook(Model model,String queryBookName) { Books books = bookService.queryBookByName(queryBookName); List<Books> list = new ArrayList<Books>(); list.add(books); model.addAttribute("list",list); return "allBook"; } }
spring层
- 1.配置spring整合mybatis,数据库源采用c3p0连接池
- 2.编写spring整合mybatis相关的配置文件
spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns44.Python实现简易的图书管理系统
基于SpringBoot 2.x开发的简易版图书管理系统(实现对图书的CRUD)