多条件查询的sql语句组成解决方案

用于有多个条件但是又不确定用户会输入哪些查询!
如果一种一种可能写,那将是exponential的,灾难~
这个方法不错,条件越多越能显出他的优势:

拼接SQL,依靠StringBuffer 这个动态字符串来完成sql语句
StringBuffer buffer=new StringBuffer(“select * from 表 where 1=1 “);
if(条件1满足){
buffer.append(” and 条件1″)
}
if(条件2满足){
buffer.append(” and 条件2″)
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s