多条件查询

多条件查询就是将sql语句进行拼接,在这里需要用到一个where 1=1 表示条件永远成立,这样就少了对where和and的处理

上代码,一目了然

String sql="select * from product where 1=1 ";
			if (proprice!=0) {		
				sql+="and proprice between "+(proprice-1)*100+" and "+((proprice-1)*100+99);
			}
			if (protype.length()>0) {
				sql+=" and protype='"+protype+"' ";
			}
			if (prostyate!=0) {
					sql+=" and prostyate="+prostyate;
			}
			if (prosort!=2) {
					sql+=" and prosort="+prosort;
			}
			if (keywords.length()>0) {
				sql+=" and proname like \'%"+keywords+"%\' or procompany like \'%"+keywords+"%\' ";
			}
			sql+=" limit "+(page-1)*5+",5";

最后一句追加的是分页查询

猜你喜欢

转载自blog.csdn.net/qq_41534115/article/details/83272333
今日推荐