说明:( Spring Boot CRUD )

  • 主要实现了图书的增删改查、管理员登录验证,其他还在开发。
  • Spring Boot主要整合了Mybatis(注解方式)、Web、Thymeleaf
  • 项目地址,点击前往

效果:

1.查询所有的书本

2.点击编辑,修改书籍信息

3.提交后反馈

4.返回查看信息已经被修改

5.删除点击按钮直接删除

6.点击添加书本按钮,添加信息

7.输入完成后提交,反馈信息

8.返回主界面,查看添加已经成功

核心代码:

Model:

User:

package com.springboot.book.model;

public class Book {
    private int id;

    private String name;

    private String author;

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    private Double price;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    private String image;

    private String description;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }



    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price='" + price + '\'' +
                ", image='" + image + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

Book:

package com.springboot.book.model;

public class User {

    private int id;

    private String name;

    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Dao:

Userdao(现在只有登录时使用了)

package com.springboot.book.dao;

import com.springboot.book.model.User;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {
    String table_name="user";
    String insert_filed="id,name,email";
    String select_field = " id, " + insert_filed;

@Select({"select * from ",table_name})
    List<User> selectAll();
//    添加user
    @Insert({"inset into",table_name,"values(#{name},#{email)"})
    User addUser(User user);

//    根据id查询用户
    @Select({"select",select_field,"from",table_name,"where id=#{id}"})
    User selectById(int id);

//    根据用户姓名查询用户信息
    @Select({"select",select_field,"from",table_name,"where name=#{name}"})
    User   selectByName(String name);

//    根据用户邮箱查询用户信息
@Select({"select",select_field,"from",table_name,"where email=#{email}"})
    User selectByEmail(String email);
}

BookDao

package com.springboot.book.dao;

import com.springboot.book.model.Book;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.*;
import org.springframework.context.annotation.Bean;

import java.util.List;

@Mapper
public interface BookDao {
    String table_name="book";
    String insert_filed="name,author,price,image,description";
    String select_field = " id, status, " + insert_filed;
//查询全部书籍
    @Select({"select * from",table_name})
    List<Book> selectAll();
//根据name查找id
    @Select({"select id from",table_name,"where name=#{name}"})
    Book selectBookByName(String Name);
//根据id查找全部信息
    @Select({"select * from",table_name,"where id=#{id}"})
    Book selectBookById(Integer id);
//根据id删除指定书籍
    @Delete({"delete ","from",table_name,"where id=#{id}"})
    void deleteBookById(int id);
//根据指定的书名删除书籍
    @Delete({"delete ","from",table_name,"where name=#{name}"})
    void deleteBookByName(String name);
//添加新的书籍
    @Insert({"insert into",table_name,"(",insert_filed,")","values(#{name},#{author},#{price},#{image},#{description})"})
    void addBook(Book book);
    @Update({"update ", table_name, " set name=#{name},author=#{author},price=#{price},image=#{image},description=#{description} where id=#{id}"})
    void updateBookById(@Param("id") int id,@Param("name") String name,@Param("author") String author,
                        @Param("price") Double price,@Param("image") String image,@Param("description") String description);
}

Controller:

bookController:

package com.springboot.book.controller.modelcontroller;

import com.springboot.book.dao.BookDao;
import com.springboot.book.model.Book;
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
public class bookController {
    @Autowired
    private BookDao bookDao;

    @RequestMapping("/book")
   public String queryAll(Model model){
        List<Book> books =bookDao.selectAll();
        model.addAttribute("books" ,books);
        return "booklist";
    }

    @GetMapping("/book/del/{id}")
    public String deleteBook(@PathVariable("id") Integer Id,Model model){
       bookDao.deleteBookById(Id);
        List<Book> books =bookDao.selectAll();
        model.addAttribute("books" ,books);
        return "redirect:/book";
    }

    @GetMapping("/book/predit/{id}")
    public String preUpdateBook(@PathVariable("id") Integer Id,Model model){
        Book book =bookDao.selectBookById(Id);
        model.addAttribute("book" ,book);
        return "editbook";
    }

    @PostMapping("/book/editok/{id}")
    public String UpdateBook(@PathVariable("id") Integer Id,
                             @RequestParam("name") String name,
                             @RequestParam("author") String author,
                             @RequestParam("price")String price,
                             @RequestParam("image") String image,
                             @RequestParam("description") String description){
        System.out.println(Id+name+author+price+image+description);
        Double Price = Double.parseDouble(price);
        bookDao.updateBookById(Id,name,author,Price,image,description);
        return "updatesuccess";
    }
    @RequestMapping("/book/look/return")
    public String reBack()
    {
        return "redirect:/book";
    }

    @RequestMapping("/book/addbook")
    public String openAddBook(){
        return "newbook";
    }

    @PostMapping("/book/newbook")
    public String addBook(
            @RequestParam("name") String name,
            @RequestParam("author") String author,
            @RequestParam("price") String price,
            @RequestParam("image") String image,
            @RequestParam("description") String description)
    {
        Double Price =Double.parseDouble(price);
        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(Price);
        book.setImage(image);
        book.setDescription(description);
        bookDao.addBook(book);
        return "addsuccess";
    }

}

LoginController:

package com.springboot.book.controller.pagecontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
   @RequestMapping(value = "user/login",method = RequestMethod.POST)
//    @PostMapping(value = "user/login")
   public String login(@RequestParam("username")String username,
                       @RequestParam("password")String password,
                       Map<String, Object> map, HttpSession httpSession){
       if(!StringUtils.isEmpty(username)&&"123456".equals(password))
       {httpSession.setAttribute("user",username);
           return "redirect:/mainui.html";
       }
       else
       {
           map.put("mes","用户名或者密码错误");
           return "login";

       }
    }
}

总结:

上面就是主要代码,我没有放前端页面代码,需要的点击项目地址可以下载全部代码。

在写的过程中遇到了不少问题,基本都是百度解决的,遇到问题多百度一下,基本都能解决。

标签云

ajax AOP Bootstrap cdn Chevereto CSS Docker Editormd Hexo IDEA JavaScript jsDeliver JS樱花特效 Linux markdown Maven MyBatis MyBatis-plus MySQL Pictures Sakura SEO Spring Boot SpringMVC SSR Thymeleaf V2ray Vue Web WebSocket Wechat Social WordPress Yoast SEO 代理 分页 图床 小幸运