sql ,hql 如何实现多个条件查询自由动态拼接

等待的猫 2011-01-16 03:26:14
假如我有5个输入框,代表5个查询条件,怎样才能实现根据用户自由输入一个或多个条件的不同而得到不同的查询结果呢??
...全文
2242 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Vale.du 2012-04-12
  • 打赏
  • 举报
回复
有left join 怎么办?
pcnsh003 2011-02-15
  • 打赏
  • 举报
回复
万一有join怎么办。。。
等待的猫 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jilong75273 的回复:]
首先你的DAO查找的操作要这样写:
public List findObjectByWhere(String sql_param,List param){
StringBuffer sql = new StringBuffer("select * from tablet_name where 1=1 ");
........
.........

}

if(sql_p……
[/Quote]
嗯嗯。。。明白
等待的猫 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jackliuwenjie 的回复:]
写5个不同的查询方法,5个方框分别调用不同的方法就行
[/Quote]

好像没看懂我提的问题,要的是实现sql或hql的动态查询
xwygn 2011-01-19
  • 打赏
  • 举报
回复
可以写一个sqlcondition model类 给它三个属性 第一个是 name(对象数据库的字段名) 第二个是value(对象要查询的条件值) operate(对应查询操作符:like = >等)在写个类 在select 、、 from tbl 加上这个类处理sqlcondtion model类后返回的where字符串 加在select语句的后面即可!
热带鱼2020 2011-01-19
  • 打赏
  • 举报
回复
lz 问的可是一到面试的笔试题呀。我当时没写好,结果被pass了。
写法可以参考 《精通Hibernate》-- 11.5.1 动态查询 p332

或者
StringBuffer hql = new StringBuffer ("select from User u where 1=1");
if (user.getTitle() != null && !user.getTitle().equals("")) {
hql.append(" and u.title like '%" + user.getTitle() + "%'");
}
if (user.getAge() != null && !user.getAge().equals("")) {
hql.append(" and u.age ='" + user.getAge() + "'");
}
return getSession().createQuery(hql.toString());
UPC_思念 2011-01-19
  • 打赏
  • 举报
回复
。。。,11楼的那个list大可不不必,可以去掉
UPC_思念 2011-01-19
  • 打赏
  • 举报
回复
11楼正解,1楼的那个思路是对的,不过查询条件要是有转移字符比如单引号就会出现问题。hibernate最好是采用11楼的动态设置参数,可以避免转义字符的问题及sql注入安全问题
MichaelLiang1989 2011-01-19
  • 打赏
  • 举报
回复
首先你的DAO查找的操作要这样写:
public List findObjectByWhere(String sql_param,List param){
StringBuffer sql = new StringBuffer("select * from tablet_name where 1=1 ");
........
.........

}

if(sql_param!=null && !sql_param.equals("")){
sql.append(sql_param);
}
if(param!=null && param.size()!=0){
//下面就是设置?号参数啦
....
}

拼接的地方就这样行啦
先定义一个StringBuffer和一个List

StringBuffer buff = new StringBuffer();
List list = new ArrayList();
if(name!=null && !name.equals("")){
buff.append(" and name like ? ");
list.add("%"+name+"%");//这个是模糊查询
}
if(age!=null && !age.equals("")){
buff.append(" and age=? ");
list.add(age);
}
...
...
...


之后就调用DAO的方法

findObjectByWhere(buff.toString(),list);
这样就行啦,不知我说明白了没有
fczfr 2011-01-18
  • 打赏
  • 举报
回复
调用不同的方法执行
等待的猫 2011-01-17
  • 打赏
  • 举报
回复
是因为要到春节了吗?怎么没人来回答,给力呀!
wpffeihuwpf 2011-01-17
  • 打赏
  • 举报
回复
顶,楼上的已经给出了
哒哒路 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mystrjiang 的回复:]

/**
* @param服务分页查询
* @author 徐江为
*/
public List<CstService> getCstServiceByPage(int page, int everypage,
CstService ser, String d1, String d2, String status) {
String hql = "select s,c.custName……
[/Quote]
拼装的太不安全了
jackliuwenjie 2011-01-17
  • 打赏
  • 举报
回复
写5个不同的查询方法,5个方框分别调用不同的方法就行
行云之云 2011-01-17
  • 打赏
  • 举报
回复
String sql = "select * from table where 1=1";
if(name != ""){
sql+=" and name="+name;
}
if(pass != ""){
sql += " and pass="+pass;
}
....

执行查询语句。。
magicluo 2011-01-17
  • 打赏
  • 举报
回复
1楼的代码,拼接会有严重的安全隐患!

建议使用占位符吧,然后定义一个list来保存条件参数(有位置顺序)。

在hibernate的回调函数中再读取参数。
liuchao1989 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mystrjiang 的回复:]
/**
* @param服务分页查询
* @author 徐江为
*/
public List<CstService> getCstServiceByPage(int page, int everypage,
CstService ser, String d1, String d2, String status) {
String hql = "select s,c.custName ……
[/Quote]+1 这个是根据条件查询的呀!
String hql = "select s,c.custName from CstService s,CstCustomer c where s.cstCustomer.custNo=c.custNo and s.svrStatus='"
+ status + "'";
if (ser.getSvrCustName() != null && !ser.getSvrCustName().equals("")) {
hql += " and s.svrCustName like '%" + ser.getSvrCustName() + "%'";
}
if (ser.getSvrTitle() != null && !ser.getSvrTitle().equals("")) {
hql += " and s.svrTitle='" + ser.getSvrTitle() + "'";
}
if (ser.getSvrType() != null && !ser.getSvrType().equals("")) {
if (ser.getSvrType().equals("全部")) {
hql = hql;
} else {
hql += " and s.svrType='" + ser.getSvrType() + "'";
}

}
if (!d1.equals("") && d1 != null && !d2.equals("") && d2 != null) {
hql += " and s.svrCreateDate between to_date('" + d1
+ "','yyyy-mm-dd') and to_date('" + d2 + "','yyyy-mm-dd')";
}

不过这样要注意注入。
等待的猫 2011-01-16
  • 打赏
  • 举报
回复
问的是:根据不同的输入条件,来实现按不同条件查询,动态的在sql拼接查询条件
  • 打赏
  • 举报
回复
/**
* @param服务分页查询
* @author 徐江为
*/
public List<CstService> getCstServiceByPage(int page, int everypage,
CstService ser, String d1, String d2, String status) {
String hql = "select s,c.custName from CstService s,CstCustomer c where s.cstCustomer.custNo=c.custNo and s.svrStatus='"
+ status + "'";
if (ser.getSvrCustName() != null && !ser.getSvrCustName().equals("")) {
hql += " and s.svrCustName like '%" + ser.getSvrCustName() + "%'";
}
if (ser.getSvrTitle() != null && !ser.getSvrTitle().equals("")) {
hql += " and s.svrTitle='" + ser.getSvrTitle() + "'";
}
if (ser.getSvrType() != null && !ser.getSvrType().equals("")) {
if (ser.getSvrType().equals("全部")) {
hql = hql;
} else {
hql += " and s.svrType='" + ser.getSvrType() + "'";
}

}
if (!d1.equals("") && d1 != null && !d2.equals("") && d2 != null) {
hql += " and s.svrCreateDate between to_date('" + d1
+ "','yyyy-mm-dd') and to_date('" + d2 + "','yyyy-mm-dd')";
}
List<Object> list = HibernateBaseDao.bypage(hql, page, everypage);
Iterator<Object> iter = list.iterator();
List<CstService> list2 = new ArrayList<CstService>();
while (iter.hasNext()) {
Object[] obj = (Object[]) iter.next();
CstService service = (CstService) obj[0];
String custName = obj[1].toString();
CstCustomer c = new CstCustomer();
c.setCustName(custName);
service.setCstCustomer(c);
list2.add(service);
}
return list2;
}
穿个对象进去

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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