前后端增删改查分析CV模板

前言:在页面展示后端的效果其实就是做增删改查的功能,这其中关乎到数据库结构,实现的手法决定页面效果如何...

目录

Mapper接口 

MybatisSql

mybatis模板

Controller层

ajax模板!

添加数据


Mapper接口 

要实现一个功能要思考这个sql是什么样子,需要什么条件?对数据库的数据做了什么操作,来决定接口是什么样子。

       // 增加图书  
  int insertBook(BookLibrary bookLibrary);
       //修改图书
  int updateBookLibary(BookLibrary book);
    //  根据id删除表数据
    int deleteBookById(int id);
     // 查询图书
    List selectBookData();

MybatisSql

mybatis模板








这是增删改查的

sql语法









    



    
        INSERT INTO book_library (name, author, publish, isbn, introduction, language, price, pubdate, class_id,
                                  pressmark)
        VALUES (#{name}, #{author}, #{publish}, #{isbn}, #{introduction}, #{language}, #{price}, #{pubdate}, #{classId},
                #{pressmark})
    

    
        UPDATE book_library
        SET name         = #{name},
            author       = #{author},
            publish      = #{publish},
            introduction = #{introduction},
            language     = #{language},
            price        = #{price},
            isbn         = #{isbn},
            pubdate      = #{pubdate},
            class_id     = #{classId},
            pressmark    = #{pressmark}
        WHERE book_id = #{bookId}
    

    
        DELETE
        FROM book_library
        WHERE book_id = #{id}
    

Controller层

ajax模板!

$(document).ready(function () {
    $('#updateBookLibary').click(function () {
        let name = document.getElementById("updatebookname").value;
        var book = {
            name: name,
        
        };

        $.ajax({
            url: '/books/updateBookLibary/' + id,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            headers: {
                'Authorization': 'Bearer ' + token
            },
            data: JSON.stringify(book),
            success: function (res) {
                if (res.code === '0') {
                    alert("失败")
                }
            },
            error: function (error) {
                console.log(error);  // 打印错误信息
                // 处理更新失败的操作
            }
        });
    });
});

查询全部(根据名字分页查询)

let name = "1l"; // 查询全部
let pageNum = 1;
let pageSize = 5;
function findBookByName(name, pageNum) {
    let url = '/booklibrary/getBooksByPage/' + encodeURIComponent(name) + '?pageNum=' + pageNum + '&pageSize=' + pageSize;
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        headers: {
            'Authorization': 'Bearer ' + token
        },
    @GetMapping("/booklibrary/getBooksByPage/{name}")
    public R getBooksByPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, @PathVariable String name) {

        List bookByName = bookLibaryService.findBookByName(pageNum, pageSize, name);

        PageInfo pageInfo = new PageInfo<>(bookByName);
        PageResult pageResult = new PageResult<>();

        pageResult.setData(pageInfo.getList());
        pageResult.setTotalRecords(pageInfo.getTotal());
        pageResult.setCurrentPageNum(pageInfo.getPageNum());
        pageResult.setRecordsPerPage(pageInfo.getPageSize());
        pageResult.setTotalPages(pageInfo.getPages());

        return R.success(pageResult);
    }

根据id删除表数据

function deleteBookById(id, currentPage) {
    $.ajax({
        url: '/booklibrary/deleteBook/' + id,
        type: 'DELETE',
        dataType: 'json',
        contentType: 'application/json',
        headers: {
            'Authorization': 'Bearer ' + token
        },
    @DeleteMapping("/booklibrary/deleteBook/{id}")
    public R deleteBook(@PathVariable int id) {
        int result = bookLibaryService.deleteBookById(id);
        if (result > 0) {
            return R.success(result);
        } else {
            return R.error("删除书籍失败");
        }
    }

根据id去修改数据

      $.ajax({
            url: '/books/updateBookLibary/' + id,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            headers: {
                'Authorization': 'Bearer ' + token
            },
    @PostMapping("/books/updateBookLibary/{id}")
    public R updateBookLibary(@PathVariable Integer id, @RequestBody BookLibrary bookLibrary) {
        bookLibrary.setBookId(id);

        boolean book = bookLibaryService.updateBookLibary(bookLibrary);
        if (book != false) {
            return R.success(book);
        } else {
            return R.error("更新失败");
        }
    }

添加数据

    @PostMapping("/booklibary/insertBook")
    public R insertBook(@RequestBody BookLibrary bookLibrary) {
        int i = bookLibaryService.insertBook(bookLibrary);
        return R.success(i);
    }
  $.ajax({
            url: '/booklibary/insertBook',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(book),
            contentType: 'application/json',
            headers: {
                'Authorization': 'Bearer ' + token
            },

你可能感兴趣的:(项目工具类,数学建模)