我正在尝试创建/删除/更新图书的CRUD + spring应用程序。一切正常,但从数据库中搜索一本书。请帮忙。
@Controller
public class BookController {
...................
@RequestMapping("searchBook")
public ModelAndView searchBook(@RequestParam("searchBook") String title){
List<Book> booksList = bookService.getAllBooks(title);
return new ModelAndView("booksearch", "booksList", booksList);
}
}
有一个books.jsp:
...................
返回主菜单
There's a getAllBooks() method from BookDaoimpl:**
@SuppressWarnings("unchecked")
@Override
public List<Book> getAllBooks(String bookName) {
Session session = this.sessionFactory.getCurrentSession();
String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" +
bookName + "%'";
List<Object[]> bookObjects = session.createQuery(query).list();
List<Book> books = new ArrayList<Book>();
for (Object[] bookObject : bookObjects) {
Book book = new Book();
int id = (int) bookObject[0];
String title = (String) bookObject[1];
String description = (String) bookObject[2];
String author = (String) bookObject[3];
int isbn = (int) bookObject[4];
int printYear = (int) bookObject[5];
int readAlready = (int) bookObject[6];
book.setId(id);
book.setBookTitle(title);
book.setDescription(description);
book.setBookAuthor(author);
book.setIsbn(isbn);
book.setPrintYear(printYear);
book.setReadAlready(readAlready);
books.add(book);
}
System.out.println(books);
return books;
}
有一个MySQL数据库:http://clip2net.com/s/3ON4e7o和这个http://clip2net.com/s/3OPyIHL
My Book.class:
@Entity
@Table(name = "books")
public class Book {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "BOOK_TITLE")
private String bookTitle;
@Column(name = "BOOK_AUTHOR")
private String bookAuthor;
@Column(name = "BOOK_DESCRIPTION")
private String description;
@Column(name = "ISBN")
private int isbn;
@Column(name = "PRINTYEAR")
private int printYear;
@Column(name = "READALREADY")
private int readAlready;
......................................
**My mvc-dispatcher-servlet.xml:**
<!-- Specifying base package of the Components like Controller, Service, DAO -->
<context:component-scan base-package="ru.maxim.bookmanager" />
<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- DataBase Information -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bookmanager" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Hibernate & SessionFactory Bean definition-->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>ru.maxim.bookmanager.model.Book</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- BookDao & BookSession beans -->
<bean id="bookDao" class="ru.maxim.bookmanager.dao.BookDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"></property>
</bean>
<bean id="bookService" class="ru.maxim.bookmanager.service.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<context:component-scan base-package="ru.maxim.bookmanager" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
如果我将查询更改为&#34; SELECT Book FROM Book b&#34;,我收到一个新的例外:请求处理失败;嵌套异常是java.lang.ClassCastException:ru.maxim.bookmanager.model.Book无法强制转换为[Ljava.lang.Object;
有什么问题?谢谢!
答案 0 :(得分:0)
我认为你正在以错误的方式从数据库中获取数据。
您有两种方法可以解决该错误: -
使用Sql
Session session = this.sessionFactory.getCurrentSession();
String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" + bookName + "%'";
List<Object[]> bookObjects = session.createSQLQuery(query).list();
List<Book> books = new ArrayList<Book>();
//rest code snippet will be same as your code snippet .
使用原生Hql
String book_title="book title name";
org.hibernate.Query que=session.createQuery("from Book where bookTitle like ?");
que.setString(0,"%"+book_title+"%");
//rest will be same as your code snippet
这里使用Native HQL而不是表名(BOOK_TITLE)使用实体名称(BOOK)和就地名列(BOOK_TITLE)类变量名(bookTitle)将被使用