如何使用@ModelAttribute批注将对象传递给数据库?

时间:2015-02-28 16:56:45

标签: spring spring-mvc

我将使用@ModelAttribute而不是@RequestParam来绑定用户输入并将其写入数据库。当@ModelAttribute将我的请求处理程序方法(@ModelAttribute book Book)中的数据绑定为book对象时,我感到困惑,那么我应该如何将此对象传递给数据库?通常使用@RequestParam我根据我的模型类用变量绑定用户输入变量,然后使用相关的DAO方法将它们发送到数据库。我在下面展示我的课程。任何人都可以说我的请求处理程序方法应该如何使用@ModelAttribute

模特课程:

@Component
public class Book {
int bookId;
String title;
Author author;
Publisher publisher;

public int getBookId() {
    return bookId;
}

public void setBookId(int bookId) {
    this.bookId = bookId;
}

public String getTitle() {
    return title;
}

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

public Author getAuthor() {
    return author;
}

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

public Publisher getPublisher() {
    return publisher;
}

public void setPublisher(Publisher publisher) {
    this.publisher = publisher;
}
}

DAO:

public class BookDAO extends JdbcDaoSupport {
@Autowired
AuthorDAO authorDAO;
@Autowired
PublisherDAO publisherDAO;

public void addBook(String title, int authorId, int publisherId)
        throws ClassNotFoundException, SQLException {

    String sql = "insert into tbl_book (title, authId, pubId) values (?, ?, ?)";
    this.getJdbcTemplate().update(sql, new Object[]{title, authorId, publisherId});
}
}

服务:

@Service
public class BookService {

@Autowired
BookDAO bookDAO;

public Book getBookById(int bookId) throws ClassNotFoundException,
        SQLException {

    return bookDAO.getBookById(bookId);

}

public List<Book> getAllBooks() throws ClassNotFoundException,
        SQLException {

    List<Book> bookList = bookDAO.getAllBooks();
    return bookList;

}

public void addBook(String title, int authorId, int publisherId) throws ClassNotFoundException,
        SQLException {

    bookDAO.addBook(title, authorId, publisherId);
}
}

控制器:

@Controller

public class BookController {

@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@RequestParam String title,
        @RequestParam int authorId, @RequestParam int blisherId)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(title, authorId, publisherId);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}
}

1 个答案:

答案 0 :(得分:-1)

您的表单应将参数名称作为书籍对象,请查看以下示例代码

<form >
<input type="text" name="authorId"/>
<input type="text" name="authorName"/>
etc...
</form>


Book.java

class Book{
  Integer authorId;
  String authorName;
  etc..
}
@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@ModelAttribute Book book)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(book);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}