sql ,hql 如何实现多个条件查询自由动态拼接 假如我有5个输入框,代表5个查询条件,怎样才能实现根据用户自由输入一个或多个条件的不同而得到不同的查询结果呢?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * @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; }穿个对象进去 问的是:根据不同的输入条件,来实现按不同条件查询,动态的在sql拼接查询条件 +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')";}不过这样要注意注入。 1楼的代码,拼接会有严重的安全隐患!建议使用占位符吧,然后定义一个list来保存条件参数(有位置顺序)。在hibernate的回调函数中再读取参数。 String sql = "select * from table where 1=1";if(name != ""){sql+=" and name="+name;}if(pass != ""){sql += " and pass="+pass;}....执行查询语句。。 首先你的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和一个ListStringBuffer 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);这样就行啦,不知我说明白了没有 11楼正解,1楼的那个思路是对的,不过查询条件要是有转移字符比如单引号就会出现问题。hibernate最好是采用11楼的动态设置参数,可以避免转义字符的问题及sql注入安全问题 ,11楼的那个list大可不不必,可以去掉 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()); 可以写一个sqlcondition model类 给它三个属性 第一个是 name(对象数据库的字段名) 第二个是value(对象要查询的条件值) operate(对应查询操作符:like = >等)在写个类 在select 、、 from tbl 加上这个类处理sqlcondtion model类后返回的where字符串 加在select语句的后面即可! 好像没看懂我提的问题,要的是实现sql或hql的动态查询 求extjs的高人指教 给<s:select/>中的list动态赋值 hibernate双向关联映射 初学Java,哪位大侠帮忙看看 java,struts2小问题 关于spring配置事务代理模板的问题 再次发帖请教一个安全问题(java.net.SocketPermission 异常)(在线等...!) 高手进..... 如何改变resin启动的默认页面?(在线等) session 消失的时候跳转到登录页面 关于JBuilder中jtree的问题,高手请进 有关内部类的 求一份 表单!!在线等!
* @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;
}
穿个对象进去
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')";
}不过这样要注意注入。
if(name != ""){
sql+=" and name="+name;
}
if(pass != ""){
sql += " and pass="+pass;
}
....执行查询语句。。
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和一个ListStringBuffer 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);
这样就行啦,不知我说明白了没有
写法可以参考 《精通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());
好像没看懂我提的问题,要的是实现sql或hql的动态查询