怎么样能实现模糊查询(在jsp中)

listlisc 2011-08-03 10:00:13
书籍名称,书籍类型,书籍出版社,如果都为空全查询出来,有一个为空根据那两个查询出来,有两个为空,根据那一个查询出来
...全文
418 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
伍文 2012-01-13
  • 打赏
  • 举报
回复
luence
小成故事 2012-01-13
  • 打赏
  • 举报
回复
12楼的方法可取
wadesoftware 2012-01-13
  • 打赏
  • 举报
回复
写SQL就行了!
lijie_ai 2011-08-03
  • 打赏
  • 举报
回复
拼SQL语句
tiantianxiu 2011-08-03
  • 打赏
  • 举报
回复
先判断后拼SQL呗!
休谱诺斯 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hxysjm520 的回复:]
直接用hibernate。传对象,什么都不用
[/Quote]
貌似人家用的就是简单的jsp,要那么复杂干啥,框架有时太复杂反而不好。。。
5楼的就可以,要学会举一反三。。。
hxysjm520 2011-08-03
  • 打赏
  • 举报
回复
直接用hibernate。传对象,什么都不用
acertang 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ajun_studio 的回复:]
你说的是查询条件吧
你可以在后台的判断 你的条件是否为空,来拼装你的sql 就可以了
如:
if(bookName!=null && !“”.equals(bookName)){
sql = sql+" and name like '%"+bookName+"%'";
}
[/Quote]

正解
EverWHL 2011-08-03
  • 打赏
  • 举报
回复
楼主,你可以看看这个,我们以前做过很多类似你这样的
/**
* 根据条件显示客户交往记录信息
*/
public List find_allAffiliateWithByConditon(AffiliateWith affiliateWith,
PageResult pageResult) {
String hql = " from AffiliateWith where 1=1";
//如果客户名称不为空
if(isNotNull(affiliateWith.getClientInfo().getClientName())&&!(affiliateWith.getClientInfo().getClientName().equals("请输入名称")))
{
hql+=" and clientInfo.clientName like '%"+affiliateWith.getClientInfo().getClientName()+"%'";
}
//如果交往地点不为空
if(isNotNull(affiliateWith.getAffiliateWithAddress()))
{
hql+=" and affiliateWithAddress like '%"+affiliateWith.getAffiliateWithAddress()+"%'";
}
//如果交往时间不为空
if(null!=affiliateWith.getAffiliateWithDate())
{
hql+=" and affiliateWithDate = '"+affiliateWith.getAffiliateWithDate()+"'";
}
//排序方式
if(!isNotNull(pageResult.getOrderBy()))
{
hql+=" order by affiliateWithDate desc";
}
List<AffiliateWith> allAffiliateWith_list = this.getDao().listByPage(hql, pageResult);
for(AffiliateWith a:allAffiliateWith_list)
{
a.setClientInfo((ClientInfo)(super.getDao().get(ClientInfo.class,a.getClientInfo().getClientId())));
}
return allAffiliateWith_list;
}我们这里传的是对象,你也可以在biz中传参数都可以,这段代码思路就是进行判断然后进行sql语句的叠加,希望对楼主你有帮助哦
飓风zj 2011-08-03
  • 打赏
  • 举报
回复
你说的是查询条件吧
你可以在后台的判断 你的条件是否为空,来拼装你的sql 就可以了
如:
if(bookName!=null && !“”.equals(bookName)){
sql = sql+" and name like '%"+bookName+"%'";
}
liushuo1987 2011-08-03
  • 打赏
  • 举报
回复
在写SQL的时候判断一下传进来的是不是空或者空字符串,如果是的话就不添加对应的那个where条件,如果非空就添加where条件用like实现模糊查询。
listlisc 2011-08-03
  • 打赏
  • 举报
回复

谁有个例子给我发下看看
鹏鹏2015 2011-08-03
  • 打赏
  • 举报
回复
跟你的数据库有关!
ace62 2011-08-03
  • 打赏
  • 举报
回复
这与jsp无关。写好SQL语句的where就可以。需要哪个条件就在where中加哪个
  • 打赏
  • 举报
回复
别用拼接字符串的方法,那样防不了sql注入,比如名字含有单引号。
查一下PreparedStatement ,这个就可以。。

String sql = "select * from book where name like ? and type like ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
ps.setString(1, "%" + type + "%");
ps.execute();

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧