Spring boot 基于web的增删改查

1、新建一个controller层,主要功能是负责页面跳转和数据交互,在上篇文章的基础上,通过界面增删改查。这里的 @Resource 是获取我具体实例的BookService bookService = new BookService();


package com.example.Spring.Boot1.controller;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.Spring.Boot1.Service.BookService;
import com.example.Spring.Boot1.entity.Book;

@Controller // Controller负责接收请求,处理完后将页面内容返回给前端。
public class bookController {

	@Resource
	BookService bookService = new BookService();
	// book的主界面 这里会从controler层model set的对象去获取相关的内容,th:each表示会循环遍历对象内容。

	@RequestMapping("/Bookdemo")
	public String list(Model model) {
		List<Book> book = bookService.getbookList();
		model.addAttribute("book", book);
		return "/Bookdemo";
	}

	// Bookupdate 修改界面
	@RequestMapping("/Bookupdate")
	public String toEdit(Model model, int id, String reader, String title) {
        System.out.println("update界面" + "reader=  " + reader + "  title=" + title);
		Book book = bookService.updBookById(id, reader, title);
		model.addAttribute("book", book);
		return "/Bookupdate";
	}

	
	// deleteBook 删除界面
	@RequestMapping("/deleteBook")
	public String delete(int id) {
		bookService.deleteById(id);
		return "/deleteBook";
	}

	// add 新增界面
	@RequestMapping("/insertBook")
	public String add(String reader, String title) {
			if (reader!= null && reader != ""  &&  title != ""  && title != null) {
				System.out.println("新增界面" + "reader=  " + reader + "  title=" + title);
				bookService.addBook(reader, title);
			}	
			return "/addBook";
	}

}

2、bookService类的代码,具体如下:

package com.example.Spring.Boot1.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.Spring.Boot1.JpaRepository.BookRepository;
import com.example.Spring.Boot1.entity.Book;

@Service
@RestController // Spring mvc的控制层 现在要返回的是一个页面所以不能再用@RestController,而用普通的@Controller
public class BookService {
	@Autowired
	private BookRepository bookRepository; // 定义book的数据仓库

	/** query 查询所有book列表 */
	@GetMapping(value = "/book")
	public List<Book> getbookList() {
		return bookRepository.findAll(); // 这个是book的仓库中自带的方法

	}

	/** insert 新增book表数据 这个需要用到postman调用测试 */
	@PostMapping(value = "/book/add")
	public Book addBook(@RequestParam("reader") String reader, @RequestParam("title") String title) {
		Book book = new Book();
		book.setReader(reader);
		book.setTitle(title);
		return bookRepository.save(book);
	}

	/**
	 * 通过id查询
	 * 
	 * @param id
	 * @return
	 */

	@GetMapping(value = "/book/getdata/{id}")
	public List getBookById(@PathVariable("id") int id) {
		return bookRepository.findAllById(id);
	}

	/*
	 * 
	 * 删除book by id
	 */

	@DeleteMapping(value = "/book/deleteBook/{id}")
	public void deleteById(@PathVariable("id") int id) {
		bookRepository.delete(bookRepository.findDataById(id));
	}

	/**
	 * update更新一个book by id
	 * 
	 * @return
	 * @return
	 */

	@PutMapping(value = "/book/updateBook/{id}")
	public Book updBookById(@PathVariable("id") int id, @RequestParam("reader") String reader,
			@RequestParam("title") String title) {
		    System.out.println("reader" + reader + "title" + title);

		Book book = new Book();
		book.setReader(reader);
		book.setTitle(title);
		book.setId(id);
		return bookRepository.save(book);
	}

}

最后通过controller类中的方法,返回的都是HTML名,通过相关请求地址可以做出相应的作用

@RequestMapping("/Bookdemo")代表的是输入地址   return "/Bookdemo";返回的html的名字

3、具体HTML的代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>BOOK</title>
</head>
<body class="container">
	<br />
	<h1>新增BOOK</h1>
	<br />
	<br />
	 <div class="with:80%">
		<form  th:action="@{/insertBook}" method="post">
			<input type="hidden" name="id" th:value="*{id}" />
			<div>
				<label for="reader">reader</label>
				<div>
					<input type="text" class="form-control" name="reader" id="reader" th:value="*{reader}"
						placeholder="reader" />
				</div>
			</div>
			<div>
				<label for="title">title</label>
				<div>
					<input type="text" class="form-control" name="title" id="title" th:value="*{title}"
						placeholder="title" />
				</div>
			</div>
			<div>
				<div>
					<input type="submit" value="Submit"  />       <a
						th:href="@{/Bookdemo}">Back</a>
				</div>
			</div>
		</form>
	</div> 
</body>
</html>
最后显示的结果:






阅读更多

没有更多推荐了,返回首页