001 图书增删改查 SSM MySQL

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

001 图书增删改查 SSM 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>
相关推荐
初晴~13 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding2 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
V+zmm101343 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm
文大。3 小时前
2024年广西职工职业技能大赛-Spring
java·spring·网络安全
一只小小翠3 小时前
EasyExcel 模板+公式填充
java·easyexcel
m0_748256344 小时前
QWebChannel实现与JS的交互
java·javascript·交互
Jelena技术达人4 小时前
Java爬虫获取1688关键字 item_search接口返回值详细解析
java·开发语言·爬虫