首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在我的java书店应用程序中编辑

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

Stack Overflow用户
提问于 2022-02-14 15:09:35
回答 1查看 90关注 0票数 0

我正试图在我的java书店中加入一个编辑功能,但我无法让它开始工作。我很确定我很接近,只是没有正确的逻辑

下面是一些错误代码:

创建名为“bookstoreController”的org.springframework.beans.factory.UnsatisfiedDependencyException:错误:通过字段“存储库”表示的不满意的依赖关系;

导致: org.springframework.data.repository.query.QueryCreationException:无法为公共抽象java.util.List hh.swd20.Bookstore.domain.BookRepository.findOne(java.lang.Long)!创建查询原因:未能为方法公共抽象hh.swd20.Bookstore.domain.BookRepository.findOne(java.lang.Long)!创建查询

由: java.lang.IllegalArgumentException:未能为方法公共抽象java.util.List java.util.List创建查询引起没有找到属性findOne类型的图书!

原因: org.springframework.data.mapping.PropertyReferenceException:没有找到findOne类型的属性!

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

有很多不同的-java文件和大量di,这是我的代码:

BookstoreController.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
}

BookstoreApplication.java

代码语言:javascript
复制
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());
            }
        };

}
}

Book.java

代码语言:javascript
复制
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 + "]";
    }
}

booklist.html

代码语言:javascript
复制
<!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>

addbook.html

代码语言:javascript
复制
<!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

代码语言:javascript
复制
<!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>
EN

回答 1

Stack Overflow用户

发布于 2022-02-14 15:24:36

JPA基于方法名生成查询.findByTitle(字符串t) -> select *其中标题= t;但是findOne(Long id)不能翻译。因此,JPA假设这是一个自定义查询,但是您既不覆盖它,也不通过注释添加自定义查询,所以它失败了。有关更多细节,请参见文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71114003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档