001 图书增删改查 SSM MySQL

发布时间:2023年12月23日

技术框架:Spring + SpringMVC + Mybatis + JSP + MySQL

package com.demo.controller;

import com.demo.pojo.Book;
import com.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    //查询全部的书籍,并且返回到一个书籍展示
    @RequestMapping("allBook")
    public String list(Model model,Integer bookId) {
        List<Book> list = bookService.queryAllBooks(bookId);
        model.addAttribute("bookList", list);
        return "book";
    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPager() {
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Book books) {
        bookService.addBook(books);
        return "redirect:/books/allBook";//重定向allBook请求
    }

    //跳转到修改书籍页面
    @RequestMapping("/toUpdate")
    public String toUpdatePager(int id, Model model) {
        Book books = bookService.findBookById(id);
        model.addAttribute("book", books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Book books) {

        int i = bookService.updateBook(books);
        if (i > 0) {
            System.out.println("添加成功" + books);
        }
        return "redirect:/books/allBook";
    }

    //删除数据
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBook(id);
        return "redirect:/books/allBook";
    }

}
package com.demo.dao;

import com.demo.pojo.Book;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
    int addBook(Book book);
    int deleteBook( int id);
    int updateBook(Book book);
    Book findBookById(Integer id);
    List<Book> queryAllBook(@Param("bookId") Integer bookId);
}
package com.demo.pojo;

import lombok.Data;

@Data
public class Book {

    private Integer id;	//图书id

    private String name;		//图书名称

    private String press;		//出版社

    private String author;		//作者

}
package com.demo.service;
import com.demo.pojo.Book;

import java.util.List;

public interface BookService {
    Book findBookById(Integer id);
    List<Book> queryAllBooks(Integer bookId);
    int addBook(Book book);
    int updateBook(Book book);
    int deleteBook(Integer id);
}
package com.demo.service.impl;

import com.demo.dao.BookMapper;
import com.demo.pojo.Book;
import com.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    private final BookMapper bookMapper;

    @Autowired
    public BookServiceImpl(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public Book findBookById(Integer id) {
        return bookMapper.findBookById(id);
    }

    @Override
    public List<Book> queryAllBooks(Integer bookId) {
        return bookMapper.queryAllBook(bookId);
    }

    @Override
    public int addBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }

    @Override
    public int deleteBook(Integer id) {
        return bookMapper.deleteBook(id);
    }
}
/*
 Navicat Premium Data Transfer

 Source Server         : 127.0.0.1(5.7.40)
 Source Server Type    : MySQL
 Source Server Version : 50740
 Source Host           : localhost:3305
 Source Schema         : ssm

 Target Server Type    : MySQL
 Target Server Version : 50740
 File Encoding         : 65001

 Date: 18/12/2023 21:10:14
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_book
-- ----------------------------
DROP TABLE IF EXISTS `tb_book`;
CREATE TABLE `tb_book`  (
  `id` int(2) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `press` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `author` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of tb_book
-- ----------------------------
INSERT INTO `tb_book` VALUES (1, '12121', '212', '12121');

SET FOREIGN_KEY_CHECKS = 1;
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.demo.dao.BookMapper">
    <!-- 添加图书信息 -->
    <insert id="addBook" parameterType="com.demo.pojo.Book">
        insert into tb_book(name, press, author) values (#{name}, #{press}, #{author})
    </insert>

    <!-- 删除图书信息 -->
    <delete id="deleteBook" parameterType="int">
        delete from tb_book where id = #{id}
    </delete>

    <!-- 更新图书信息 -->
    <update id="updateBook" parameterType="com.demo.pojo.Book">
        update tb_book set name=#{name}, press=#{press}, author=#{author} where id=#{id}
    </update>

    <!-- 根据ID查询图书信息 -->
    <select id="findBookById" resultType="com.demo.pojo.Book">
        select * from tb_book where id = #{id}
    </select>

    <!-- 查询所有图书信息 -->
    <select id="queryAllBook" resultType="com.demo.pojo.Book">
        select * from tb_book
        <where>
            <if test="bookId != null">
                AND id = #{bookId}
            </if>
        </where>
    </select>
</mapper>
文章来源:https://blog.csdn.net/weixin_43329821/article/details/135171088
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。