sql=" select * from table1 where 1=1 ";if(条件){ sql=sql+" and a =? "; } if(条件){ sql=sql+" and b =? "; } if(条件){ sql=sql+" and c=? "; }
StringBuilder sb = new StringBuilder("select * from table1 where 1=1 "); if (a condition) {sb.append("and a = '' ");} if (b condition) {sb.append("and b = '' ");} if (c condition) {sb.append("and c = '' ");} String sql = sb.toString();
额。。用的ibatis。。sql都是提前在配置文件里写好的.难道要在配置文件里写多个sql?
拼SQL语句就好,select * from table where 1=1 and *** 条件为空就不加上去就好了嘛
在程序中定义三个变量a、b、c,如果页面选中了某几项的话,就给他们赋值,否则就设置为'',然后sql语句就用下面的就可以了:select * from table1 where a like '%'+a+'%' and b like '%'+b+'%' and c like '%'+c+'%'
sql=sql+" and a =? ";
}
if(条件){
sql=sql+" and b =? ";
}
if(条件){
sql=sql+" and c=? ";
}
if (a condition) {sb.append("and a = '' ");}
if (b condition) {sb.append("and b = '' ");}
if (c condition) {sb.append("and c = '' ");}
String sql = sb.toString();
条件为空就不加上去就好了嘛
<isNotEmpty prepend="and" property="a_value">
<![CDATA[
a=#a_value#
]]>
</isNotEmpty>
</dynamic>
备注:a_value 是传入的参数名称,其他类同
<select id="goldsale.distributor.queryAllDistributor" parameterClass="com.distributor.dto.DistributorDTO" resultClass="java.util.HashMap" remapResults="true">
<include refid="global.prefixSql"/>
<![CDATA[
select * from agent t
]]>
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="agent_name">
<![CDATA[
t.AGENT_Name = #agent_name# ]]>
isNotEmpty>
</dynamic>
<![CDATA[
order by t.rescission_date desc
]]>
<include refid="global.prefixSql"/>
</select>
StringBuffer 然后append比较好
select * from table where 1 = 1
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="name">
</isNotEmpty>
</select>