首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hql in语句传空值如何去除该条件

2012-09-10 
hql in语句传空值怎么去除该条件如:select * from sysuserwhere 11and hospId:hospIdand status in (:st

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迭代+");";
}

就可以了吧.

热点排行