springboot复习题

更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
本例Spring MVC程序主要功能是使用Thymeleaf模板引擎显示学生成绩表。该程序需要在文件Student.java中定义一个Student模型, 在HomeController.java中定义控制器,在index.html中使用Thymeleaf模板引擎渲染学生成绩信息。请补全各自文件中的代码。 模型文件Student.java public class Student { private String sid; //学号 private String name; //姓名 private float score; //成绩 public ______(String sid, String name, float score) { this.sid = sid; this.name = name; this.score = score; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } } 控制器文件HomeController.java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; @Controller public class HomeController { private List<Student> list; public HomeController() { this.list=new ArrayList<Student>(); this.list.add(new Student("18001","Mr. wang", 90.0f)); this.list.add(new Student("18002","Mrs. Zhang", 80.0f)); this.list.add(new Student("18003","Mr. Liang", 70.0f)); this.list.add(new Student("18004","Mr. Kang", 85.0f)); } @GetMapping("/") public String index(______){ model.addAttribute("caption", "《Java 高级程序设计》成绩单"); model.addAttribute("students", list); return ______; } } 视图模板文件index.html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>学生成绩单</title> <style> caption{ font-size:20px; } </style> </head> <body> <div style="width:75%; margin:10px auto 10px auto"> <table border="1" width="100%"> <caption ______></caption> <thead> <tr> <th width="10%">学号</th> <th>姓名</th> <th>成绩</th> </tr> </thead> <tbody> <tr th:each="student:${______}"> <td ______>学号</td> <td ______>姓名</td> <td ______>成绩</td> </tr> </tbody> </table> </div> </body> </html>
本例程序使用Spring Data JPA访问MySQL数据库,实现图书管理系统的RESTful API。该程序需要编写图书模型文件Book.java,图书仓储接口文件BookRepository.java,控制器文件BookController.java。 MySQL数据库表设计如下: drop database if exists books_db; create database books_db default charset=utf8; use books_db; create table books(id integer auto_increment primary key, isbn varchar(15) not null unique, //书号,非空,不能重复 name varchar(50) not null, //书名,非空 press varchar(50) not null //出版社,非空 ); //表名为books //Book.java文件内容如下,请补全空格处的代码: package cn.anntek; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="books") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(nullable = false, unique = true) private String isbn;//书号 @Column(nullable = false) private String name;//书名 @Column(nullable = false) private String press;//出版社 public Book() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } } //BookRepository.java文件如下,请补全空格处的代码。 package cn.anntek; import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository<Book, Integer> { } //BookController.java文件如下,请补全空格处的代码。 package cn.anntek; import jdk.nashorn.internal.runtime.options.Option; import org.springframework.beans.factory.annotation.Autowired; 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.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; import java.util.Optional; @RestController public class BookController { @Autowired private BookRepository bookRepository; // 获取书籍列表 @GetMapping("/books") public List<Book> getBooks(){ return bookRepository.findAll(); } // 增加书籍 @PostMapping("/books") public Book saveBook(@RequestBody Book book){ System.out.println(book.getName()); bookRepository.save(book); return book; } // 修改书籍 @PutMapping("/books/{id}") public Book updateBook(@RequestBody Book book, @PathVariable Integer id){ Optional<Book> op=bookRepository.findById(id); if(op.isPresent()){ Book book_fd=op.get(); book_fd.setIsbn(book.getIsbn()); book_fd.setName(book.getName()); book_fd.setPress(book.getPress()); bookRepository.save(book_fd); return book_fd; }else{ return null; } } @DeleteMapping("/books/{id}") public void deleteBook(@PathVariable Integer id){ bookRepository.deleteById(id); }
1 2