jsp网站,根据书名作者查询,有个查询栏
想把精确查询改为模糊查询,不知道查询语句like的后面该怎么写。。
以下为相关文件
BookHibernateDAO.java
package com.ascent.dao.hibernate;import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;import com.ascent.bean.Book;
import com.ascent.dao.IBookDAO;public class BookHibernateDAO extends HibernateDaoSupport implements IBookDAO { private static final Logger LOGGER = LogManager
.getLogger(BookHibernateDAO.class); private static final String LOAD_ALL = "from Book bk order by bk.bookId desc"; private static final String LOAD_BY_NAME = "from Book bk where bk.bookName = ? "; private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor = ? "; private static final String LOAD_BY_ID = "from Book bk where bk.bookId = ? "; private static final String LOAD_FIVE = "from Book bk order by bk.bookId desc limit 5,5 "; /**
 * 
 */
public BookHibernateDAO() {
super();
} /**
 * 
 * @param Book
 * @return Book
 */
public Book saveBook(Book book) {
try {
this.getHibernateTemplate().save(book);
LOGGER.debug("保存书籍信息到数据库成功!");
return book;
} catch (Exception ex) {
LOGGER.error("保存书籍信息到数据库失败!");
ex.printStackTrace();
return null;
}
} /**
 * 
 * @param id
 *            Integer
 * 
 * @return Book
 */
public Book getBook(Integer id) {
LOGGER.debug("根据书籍ID得到书籍信息!");
return (Book) this.getHibernateTemplate().get(Book.class, id);
} /**
 * 
 * @return List
 */
public List findBookAll() {
try {
LOGGER.debug("得到所有书籍列表!");
return this.getHibernateTemplate().find(LOAD_ALL);
} catch (Exception ex) {
LOGGER.error("获取所有书籍列表失败!");
return new ArrayList();
}
} /**
 * 
 * @param type
 *            String
 * 
 * @return List
 */
public List findBookById(Integer id) {
try {
LOGGER.debug("根据id得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_ID, id);
} catch (Exception ex) {
LOGGER.error("根据id获取书籍信息失败!");
ex.printStackTrace();
return null;
}
} /**
 * 
 * @param type
 *            String
 * 
 * @return List
 */
public List findBookByName(String name) {
try {
LOGGER.debug("根据书籍名得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_NAME, name);
} catch (Exception ex) {
LOGGER.error("根据书籍名获取书籍信息失败!");
ex.printStackTrace();
return null;
}
} /**
 * 
 * @param Book
 * 
 */
public List findBookByAuthor(String author) {
try {
LOGGER.debug("根据作者得到书籍信息!");
return this.getHibernateTemplate().find(LOAD_BY_AUTHOR, author);
} catch (Exception ex) {
LOGGER.error("根据作者获取用户书籍失败!");
ex.printStackTrace();
return null;
}
} /**
 * 
 * @param Book
 * 
 */
public List findBookFive() {
// List l = null;
try {
LOGGER.debug("得到前5本书籍信息!");
// Query q = this.getHibernateTemplate().getSessionFactory()
// .openSession().createQuery(LOAD_ALL);
// q.setMaxResults(5);
// l = q.list();
// return l;
return this.getHibernateTemplate().find(LOAD_FIVE);
} catch (Exception ex) {
LOGGER.error("得到前5本书籍信息失败!");
ex.printStackTrace();
return null;
}
} public Book updateBook(Book book) {
try {
this.getHibernateTemplate().update(book);
LOGGER.debug("更新书籍信息到数据库成功!");
return book;
} catch (Exception ex) {
LOGGER.error("更新书籍信息到数据库失败!");
ex.printStackTrace();
return null;
}
} public void removeBook(Book book) {
LOGGER.debug("从数据库中删除指定书籍信息!");
this.getHibernateTemplate().delete(book);
}}
-------------------------------------------------------------------------------------------
BookQueryForm.java
package com.ascent.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class BookQueryForm extends ActionForm {
/*
 * Generated fields
 */ /** bookAuthor property */
private String bookAuthor; /** bookName property */
private String bookName; private String select; /*
 * Generated Methods
 */ public String getSelect() {
return select;
} public void setSelect(String select) {
this.select = select;
} /**
 * 
 * Method validate
 * 
 * @param mapping
 * @param request
 * @return ActionErrors
 */
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// ActionErrors errors =new ActionErrors();
// if(bookName==null||bookName==""){
// errors.add("bookName", new ActionMessage("error.bookName.required"));
// }
return null;
} /**
 * Method reset
 * 
 * @param mapping
 * @param request
 */
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
} /**
 * Returns the bookAuthor.
 * 
 * @return String
 */
public String getBookAuthor() {
return bookAuthor;
} /**
 * Set the bookAuthor.
 * 
 * @param bookAuthor
 *            The bookAuthor to set
 */
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
} /**
 * Returns the bookName.
 * 
 * @return String
 */
public String getBookName() {
return bookName;
} /**
 * Set the bookName.
 * 
 * @param bookName
 *            The bookName to set
 */
public void setBookName(String bookName) {
this.bookName = bookName;
}
}
---------------------------------------------------------------------------
BookQueryAction.java
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.ascent.struts.action;import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.ascent.struts.form.BookQueryForm;/**
 * MyEclipse Struts Creation date: 07-12-2006
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/bookQuery" name="bookQueryForm" scope="request"
 *                validate="true"
 */
public class BookQueryAction extends BaseAction {
/*
 * Generated Methods
 */ /**
 * Method execute
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BookQueryForm bookQueryForm = (BookQueryForm) form;
String select = bookQueryForm.getSelect();
/**
 * 未注册用户图书查询
 */
if (select.equals("bkName")) {
String bookName = bookQueryForm.getBookName();
List bookList = this.getBookService().findBookByName(bookName);
request.getSession().setAttribute("books", bookList);
}
if (select.equals("bkAuthor")) {
String bookAuthor = bookQueryForm.getBookName();
List bookList = this.getBookService().findBookByAuthor(bookAuthor);
request.getSession().setAttribute("books", bookList);
}
return mapping.findForward("bookQuery");
}
}

解决方案 »

  1.   

    select * from 书籍表 as book where book.name like '%你传递过来的搜索条件%';
      

  2.   

    饿。。就是不知道like后面的搜索条件该怎么写
    比如我搜索王,作者王X和X王都能出现的条件怎么写。。
      

  3.   

    我现有的想法是private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor = "%+XX+%" "; XX位置不知道该把BookQueryForm.java中的哪个值传过来..
      

  4.   

    private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor like "%+XX+%"; 如果有本书叫王XX,你只需要传进来一个王就可以查到了啊
      

  5.   


    String bookName  =  bookQueryForm.getBookName();
    "from Book bk where bk.bookName like ? "pstmt.setString(1, bookName);
    我简单写的
      

  6.   

    private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor like '%王%'; 
      

  7.   

    将查询语句改为private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor  like '%?%' ";后
    出现以下错误
    2009-03-30 16:16:37,375 [com.ascent.dao.hibernate.BookHibernateDAO]-[DEBUG] 根据作者得到书籍信息!
    2009-03-30 16:16:37,390 [com.ascent.dao.hibernate.BookHibernateDAO]-[ERROR] 根据作者获取用户书籍失败!
    java.lang.IllegalArgumentException: No positional parameters in query: from Book bk where bk.bookAuthor  like '%?%' 
    at net.sf.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:143)
    at net.sf.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:389)
    at org.springframework.orm.hibernate.HibernateTemplate$20.doInHibernate(HibernateTemplate.java:718)
    at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:367)
    at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:708)
    at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:691)
    at com.ascent.dao.hibernate.BookHibernateDAO.findBookByAuthor(BookHibernateDAO.java:123)
    at com.ascent.business.service.BookServiceImpl.findBookByAuthor(BookServiceImpl.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:291)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:180)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:147)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:169)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:169)
    at $Proxy1.findBookByAuthor(Unknown Source)
    at com.ascent.struts.action.BookQueryAction.execute(BookQueryAction.java:54)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.ascent.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:33)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
      

  8.   

    session.createQuery("from Book bk where bk.bookAuthor like :name") 
    .setString("name", "%" + inputKey + "%").list();
      

  9.   

    你贴的代码好长啊
    --查询table表中name字段包含'好'字的数据
    select * from table where name like '%好%'
      

  10.   

    select * from 书籍表 as book where book.name like '%你传递过来的搜索条件%'; 
      

  11.   

    搞清楚了,是gethibernatetemplate.find的模糊查询问题
    private static final String LOAD_BY_AUTHOR = "from Book bk where bk.bookAuthor like ? ";public List findBookByAuthor(String author) {
    try {
    LOGGER.debug("根据作者得到书籍信息!");
    return this.getHibernateTemplate().find(LOAD_BY_AUTHOR, author);//此处author改成"%"+author+"%"就可以了
    } catch (Exception ex) {
    LOGGER.error("根据作者获取用户书籍失败!");
    ex.printStackTrace();
    return null;
    }
    }
      

  12.   

    sql语句如下:
    select * from 书籍表  where book.name like '%王%'