日期:2014-05-19  浏览次数:20771 次

hql in语句传空值怎么去除该条件
如:
select * from sysuser 
where 1=1 
and hospId=:hospId
and status in (:status);

我想要的是,如果:status传入的是空值则and status in (:status)整句删除。
就像如果:hospId传入的是空值,and hospId=:hospId会自动删除。

或者可不可以直接在SQL语句里进行判断。

------解决方案--------------------
String sql = "select * from sysuser where 1=1 "

if(hospId!= null){
sql += " and hospId="+hospId ; 
}
if(status != null ){
sql += " and status in ("+status迭代+");";
}

是不是这样否?


------解决方案--------------------
单纯sql语句实现好像不太现实,至少也要写成存储过程。。。
------解决方案--------------------
探讨
String sql = "select * from sysuser where 1=1 "

if(hospId!= null){
sql += " and hospId="+hospId ;
}
if(status != null ){
sql += " and status in ("+status迭代+");";
}

是不是这样否?

------解决方案--------------------
用xml文件的话,应该有标签可以实现拼sql的,比如ibatis中的xml文件有标签<isNotNull>
------解决方案--------------------
写存储过程吧,然后在XML里调用存储过程。

------解决方案--------------------
if(status != null &&!status.equals("")){
sql += " and status in ("+status迭代+");";
}

就可以了吧.