当前位置: 首页 > 知识库问答 >
问题:

无法在我的java书店应用程序中进行编辑

徐昕
2023-03-14

我正试图将editbook功能添加到我的java bookstore asignment中,但我无法让它工作。我很确定我很接近,只是逻辑不太正确

以下是我收到的一些错误代码:

org.springframework.beans.factory.不满意依赖异常:创建名为bookstore Controller的bean时出错:通过字段存储库表示不满意的依赖关系;

原因:org。springframework。数据存储库。查询QueryCreationException:无法为公共抽象java创建查询。util。列表hh。swd20。书店领域书库。findOne(java.lang.Long)!原因:无法为公共抽象java方法创建查询。util。列表hh。swd20。书店领域书库。findOne(java.lang.Long)!

原因:java。lang.IllegalArgumentException:未能为公共抽象java方法创建查询。util。列表hh。swd20。书店领域书库。findOne(java.lang.Long)!找不到类型簿的属性findOne!

引起:org.springframework.data.mapping.属性引用异常:未找到类型Book的属性findOne!

我不太擅长分析错误,因为我不太确定它们到底是什么意思。但我知道我的findOne变量有问题。

这里有很多不同的java文件和大量di。以下是我的代码:

书店管理员。JAVA

package hh.swd20.Bookstore.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import hh.swd20.Bookstore.domain.Book;
import hh.swd20.Bookstore.domain.BookRepository;

@Controller
public class BookstoreController {
            @Autowired
            private BookRepository repository;
            
            @RequestMapping("/booklist")
            public String booklist(Model model) {       
                model.addAttribute("books", repository.findAll());
                return "booklist";
            }
            
            @RequestMapping(value = "/add")
            public String addBook(Model model){
                model.addAttribute("book", new Book());
                return "addbook";
            } 
            
            @RequestMapping(value = "/savebook", method = RequestMethod.POST)
            public String save(Book book){
                repository.save(book);
                return "redirect:booklist";
            }
            
            @RequestMapping(value="/books", method = RequestMethod.GET)
            public @ResponseBody List<Book> bookListRest() {
                return (List<Book>) repository.findAll();
            }
            
            @RequestMapping(value="/books/{id}", method = RequestMethod.GET)
            public @ResponseBody Book findbookRest(@PathVariable("id") Long bookId) {
                return (Book) repository.findOne(bookId);
            }
            
            @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
            public String editStudent(@PathVariable("id") Long bookId, Model model){
                model.addAttribute("book", repository.findOne(bookId));
                return "editbook";
            }

            @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
            public String deleteBook(@PathVariable("id") Long bookId, Model model) {
                repository.deleteById(bookId);
                return "redirect:../booklist";
            }
}

BookRepository.java

package hh.swd20.Bookstore.domain;

import java.util.ArrayList;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface BookRepository extends CrudRepository<Book, Long> {
    
    List<Book> findByTitle(String title);

    List<Book> findOne(Long id);
}

书店Application.java

package hh.swd20.Bookstore;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import hh.swd20.Bookstore.domain.Book;
import hh.swd20.Bookstore.domain.BookRepository;

@SpringBootApplication
public class BookstoreApplication {
    private static final Logger log = LoggerFactory.getLogger(BookstoreApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(BookstoreApplication.class, args);
    }
    
    @Bean
    public CommandLineRunner bookDemo(BookRepository repository) {
        return (args) -> {
            log.info("save a couple of books");
            repository.save(new Book("Shadow of the Conqueror", "Shad M. Brooks", 2019, "945-3-16-14-15", 32));
            repository.save(new Book("The Way of Kings", "Brandon Sanderson", 2010, "9564-17-15-16", 35));  
            
            log.info("fetch all books");
            for (Book book : repository.findAll()) {
                log.info(book.toString());
            }
        };

}
}

书JAVA

package hh.swd20.Bookstore.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private int year;
    private String isbn;
    private int price;
    
    public Book(String title, String author, int year, String isbn, int price) {
        super();
        this.title = title;
        this.author = author;
        this.year = year;
        this.isbn = isbn;
        this.price = price;
    }
    
    public Book(Long id, String title, String author, int year, String isbn, int price) {
        super();
        this.title = title;
        this.author = author;
        this.year = year;
        this.isbn = isbn;
        this.price = price;
    }
    
    public Book(Long id) {
        super();
        this.id = id;
    }


    public Book() {
    }

    public Long getId() {
        return id;
    }
    
    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public int getYear() {
        return year;
    }

    public String getIsbn() {
        return isbn;
    }

    public int getPrice() {
        return price;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

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

    @Override
    public String toString() {
        return "Bookstore [id=" + id + ", title=" + title + ", author=" + author + ", year=" + year + ", isbn=" + isbn + ", price="
                + price + "]";
    }
}

书单。html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Booklist</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Books</h1>
    <table>
        <tr>
            <th>Title</th>
            <th>Author</th>
        </tr>  
        <tr th:each = "book : ${books}">
            <td th:text="${book.title}"></td>
            <td th:text="${book.author}"></td>      
        <td><a th:href="@{/edit/{id}(id=${book.id})}">Edit</a><a th:href="@{/delete/{id}(id=${book.id})}">Delete</a></td>           
        </tr>
     </table>
     <a href="/add">Add Book</a>
</body>
</html>

地址簿。html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Booklist add</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Add book</h1>
    <div>
        <form th:object="${book}" th:action="@{savebook}" action="#" method="post">
            <label for="booktitle">Title</label>
            <input type="text" id="booktitle" th:field="*{title}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="author">Author</label>
            <input type="text" id="authorname" th:field="*{author}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="bookyear">Year</label>
            <input type="text" th:field="*{year}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="bookisbn">ISBN</label>
            <input type="text" th:field="*{isbn}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="price">Price</label>
            <input type="text" th:field="*{price}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            <input type="submit" value="Save"></input>
        </form>
        <a href="/booklist">Return</a>
    </div>
</body>
</html>

editbook.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Booklist edit</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Edit book</h1>
    <div>
        <form th:object="${book}" th:action="@{savebook}" action="#" method="post">
            <label for="booktitle">Title</label>
            <input type="text" id="booktitle" th:field="*{title}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="author">Author</label>
            <input type="text" id="authorname" th:field="*{author}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="bookyear">Year</label>
            <input type="text" th:field="*{year}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="bookisbn">ISBN</label>
            <input type="text" th:field="*{isbn}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            
            <label for="price">Price</label>
            <input type="text" th:field="*{price}" />
            <div style="clear: both; display: block; height: 10px;"></div>
            <input type="submit" value="Save Book"></input>
        </form>
        <a href="/booklist">Return</a>
    </div>
</body>
</html>

共有1个答案

秦学林
2023-03-14

JPA根据方法名生成查询...... findByTitle(String t)-

我建议使用每个CRUD存储库附带的现有函数findById()。

另外,@Kayaman是对的,为什么findOne()函数返回列表。

 类似资料:
  • 我刚刚安装了NetBeans 7.3.1。我只是试图创建一个新的Java应用程序,但每当我通过向导时,它会显示“Project folder exists and is not Empty”(项目文件夹存在且不是空的),不会再继续了。这是一个新项目,所以在NetBeans创建文件夹之前,文件夹是不存在的。 有什么想法吗?

  • 我无法在我的应用程序中使用proguard。当我将minify启用为true时,布局屏幕中的textinputlayout工作正常,但我有一个alertdialog,其中包含一个膨胀的XML,该XML没有膨胀(该XML包含一个textinputlayout。请帮助大家。这是代码片段。 PS:我正在使用手机应用程序发帖,请不要抨击对齐不当。我真的需要帮助。 建筑渐变条目-- XML屏幕中的TextI

  • 为了在Eclipse中创建我的第一个简单Kotlin项目,我遵循了从Kotlin官方网站开始使用Eclipse Luna教程的步骤,即: 从Eclipse市场安装适用于Eclipse的Kotlin插件 但是,我在Eclipse控制台中经常遇到以下错误: 错误:无法找到或加载主类HelloKt 我仔细检查了我的项目的运行配置,它确实将“Main class”设置为(我100%确定它不存在)。另外,当

  • 问题内容: 我想实现一个Java应用程序(服务器应用程序),该应用程序可以从给定的URL下载新版本(.jar文件),然后在运行时进行自身更新。 最好的方法是什么,有可能吗? 我猜该应用程序可以下载一个新的.jar文件并启动它。但是我应该如何进行切换,例如知道何时启动新应用程序然后退出。还是有更好的方法来做到这一点? 问题答案: 解决方案的基本结构如下: 有一个主循环负责重复加载应用程序的最新版本(

  • 我使用Hibernate将Roo生成的SpringMVC应用程序连接到PostgreSQL。我试图将Flyway1.6作为一个bean集成到Spring应用程序上下文中。“有什么东西阻止”迁移的执行,我在上下文初始化时遇到了由Hibernate元数据验证引起的错误。使用Flyway 1.6 Maven插件(clean、init、migrate)执行迁移没有问题。 Flyway 1.5(早期版本)的