myibatis3 使用like 模糊查

本文介绍了一种使用MyBatis实现动态SQL的方法。通过示例展示了如何根据不同的条件构造SQL查询语句,包括使用IF标签进行条件判断、绑定变量、分页查询等技巧。这对于提高SQL查询的灵活性和效率非常有帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

方法1:

<sql id="select_where">

<if test="orderid != null">

and om.orderid = #{orderid}

</if>

 

<if test="paytype != null">

and om.paytype = #{paytype}

</if>

 

<if test="customerid != null">

and om.customerid = #{customerid}

</if>

 

<if test="status != null">

and om.status = #{status}

</if>

 

<if test="type != null">

and om.type = #{type}

</if>

 

<if test="proName != null">

and od.productname like 

concat(concat('%',#{proName}),'%')

</if>

 

<if test="proCode != null">

and od.PRODUCT_NO like 

concat(concat('%',#{proCode}),'%')

</if>

</sql>

 

<select id="orderTotalRows" resultType="java.lang.Integer"

parameterType="com.cloud.api.pojo.order.OrderSql">

select count(1)

from OMORDER om,orderdetail od

where om.orderid = od.orderid

 

<include refid="select_where" />

 

</select>

 

 

方法2

 

<sql id="select_where">

<if test="orderid != null">

and om.orderid = #{orderid}

</if>

 

<if test="paytype != null">

and om.paytype = #{paytype}

</if>

 

<if test="customerid != null">

and om.customerid = #{customerid}

</if>

 

<if test="status != null">

and om.status = #{status}

</if>

 

<if test="type != null">

and om.type = #{type}

</if>

 

<if test="proName != null">

and od.productname like ${bname}

</if>

 

<if test="proCode != null">

and od.PRODUCT_NO like 

${bcode}

</if>

</sql>

 

<select id="orderList" resultMap="BaseResultMap"

parameterType="com.cloud.api.pojo.order.OrderSql">

<bind name="bname" value="'%'${proName}'%'"/>

<bind name="bcode" value="'%'${proCode}'%'"/>

 

select *

from (select t_order.*, rownum as odnum

from (

select

<include refid="Base_Column_List" />

from OMORDER om,orderdetail od

where om.orderid = od.orderid

 

<include refid="select_where" />

 

) t_order

<![CDATA[where rownum <=]]>

#{rowEnd} )

<![CDATA[where odnum >]]>

#{rowBegin}

 

</select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值