mybatis提取<where><if></if><where>

本文介绍了如何在MyBatis的mapper.xml文件中通过提取公共查询条件并封装成sql id来提高代码的复用性和可维护性。通过创建一个`common_where_if`的sql片段,将多个查询条件如productNo、productName、istatus和时间范围等进行整合,然后在实际的select查询中通过`<include>`标签引用这个片段,从而简化了SQL语句,减少了代码重复。

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

在mapper.xml文件中如果有多个查询条件是相同的,可以提取处相同部分,封装起来,哪里使用哪里调用

eg:

select * from product where 1=1 

        <if test="productNo != null and productNo != ''">
                and p.productNo like '%${productNo}%'
        </if>
        <if test="productName != null and productName != ''">
            and p.productName like '%${productName}%'
        </if>
        <if test="istatus == null">
            and o.istatus >= 0
        </if>
        <if test="startDate != null">
            and o.CreateTime >= #{startDate}
        </if>
        <if test="endDate != null">
    		and o.CreateTime < #{endDate};




select count(*) from product where 1=1 

        <if test="productNo != null and productNo != ''">
                and p.productNo like '%${productNo}%'
        </if>
        <if test="productName != null and productName != ''">
            and p.productName like '%${productName}%'
        </if>
        <if test="istatus == null">
            and o.istatus >= 0
        </if>
        <if test="startDate != null">
            and o.CreateTime >= #{startDate}
        </if>
        <if test="endDate != null">
    		and o.CreateTime < #{endDate};

上面两个sql中用到的查询条件相同,所以我们提取相同查询条件



<sql id="common_where_if">
        <if test="productNo != null and productNo != ''">
            and p.productNo like '%${productNo}%'
        </if>
        <if test="productName != null and productName != ''">
            and p.productName like '%${productName}%'
        </if>
        <if test="istatus == null">
            and o.istatus >= 0
        </if>
        <if test="startDate != null">
            and o.CreateTime >= #{startDate}
        </if>
        <if test="endDate != null">
    		and o.CreateTime < #{endDate}
    	
</sql>

在sql中使用查询条件

<select id="selectList" resultType="com.dims.mes.order.vo.MopOrderVo" 
     parameterType="java.util.Map">
        select * from product
        <where>
            <include refid="common_where_if"/>
        </where>
        
</select>

``` <?xml version="1.0" encoding="UTF-8"?> <!D0CTYPE mapper PuBLIc "-//mybatis.org//0To Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper,dtd"s <mapper namespace="cn.com.cnnp erdb.rmr.conm.psatype.mapper .PsaTypeMapper"> 通用查询映射结果--> <resultMap id="psaTpeResultMap" type="cn.com.cnnp.endb.rmr.conm.psatype. vo .PsaTypeV0"> <result column="id" property="id"/> <result column="code" property="code"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <result column="remark" property="remark"/> <result column="plant_id" property="plantId"/> <result column="unit_id” property="unitId"/> <result column="update_user" property="updateUser"/> <result column="update_time" property="updateTime"/> <result column="create_user" property="createUser"/> <result column="create_time" property="createTime"/> <result column="is_deleted" property="isDeleted"/> </resultMap> <select id="selectPsaTypePage" resultType="cn .com.cnnp.erdb.rmr.conm.psatype. vo.PsaTypeVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, ifnull(parent.code,")as parentCode, ifnull(parent.name,"')as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id left join erm_dm.dm_eg_unit u on a.unit_id = u.id <where> a.is_deleted = 0 <if test="vo.plantId != null and vo.plantId != "> and a.plant_id =#{vo.plantId} </if> sif test="vo,unitId != null and vo.unitId !=!"> and a.unit id= #{vo.unitId} </1f> </where> ORDER BY a.'plant_id’,(u.*unit_no*+0),parent.code, a.code </select> <select id="spaqe resultType="cn.com,cnnp.epdb.rmn.conm.psatype.vo.PsaTypeselectVo": select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id <where> a.is_deleted= 0 <choose> <when test="parentId != null and parentId != '""> and a.parent_id = #{parentId} </when> <otherwise> and(a.parent_id is null or a.parent_id ='') </otherwise> </choose> <if test="plantId != null and plantId != "'"> and a.plant_id = #{plantId} </if> <if test="unitId != null and unitId !="'"> and a.unit_id = #{unitId} </if> </where> 0RDER BY a.plant_id,parent.code, a.code </select> <select id="spages" resultType="cn.com .cnnp.endb.rmr conm.psatype. vo .PsaTypeSelectVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time. a is deleted parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id cwhere> a.is_deleted= 0 <choose> swhen test="parentId != null and parentId != '!"> and a.parent_id = #{parentId} <when test="parentId != null and parentId != ''"> and a.parent_id = #{parentId} </when> <otherwise> AND a.1d NOT IN (SELEcT parent_id FRoM rmr_cf_psa_type WHERE is_deleted = '@' AND parent_1d IS NOT NULL) </otherwise> </choose> <if test="plantId != null and plantId !=''"> and a.plant_id =#{plantId} </if> <if test="unitId != null and unitId != '·“> and a.unit_id = #{unitId} </if> </where> ORDER BY a.`plant_id`,parent.code, a.code </select> <select id="stdDeviceType" resultType="cn.com.gnnp.ardb.rmr.gon.psatype.vo.stdDeviceTypelistVo"> select id, name , code, fid, level from erdb_device_type <where> is_deleted =0 <choose> <when test="level != null and level != '"> <choose> <when test="level =='',toString()"> and fid in(select id from erdb_device_type where fid = #{fid}) and level = 3 </when> <otherwise> and level = #{level} <if test="fid != null and fid != ""> and fid = #{fid} </if> </otherwise> </choose> </when> <otherwise> and level = 3 <if test="fid != null and fid != ''"> and fid = #{fid} </if> </otherwise> </choose> </where> </select> /mapper>```代码所实现的功能详情解析
最新发布
04-02
Config <?xml version="1.0"?> <Root Ver="1.0" YIBAO="" NoDialog="" Preview="11111111111100011"><Config TimeOut="15" BrowMode="0"><DBLink Mode="1"></DBLink><Chart HasChart="0" ViewFirst="0"/><BrowUrl User="" Pass=""></BrowUrl><CTI Enabled="0" Tel1="" Tel2="" Tel3=""/><Print Edit="0" ExecSql="" Select="0" SelectDefault="1" Mode="0"/><OtherReport><File></File><Fun></Fun></OtherReport></Config><DTS><Script>declare @SCF01 varchar(2560) set @SCF01 = :SCF01</Script><Ie Name="检查照片" Fixed="0" PivotGrid="0" Visible="1" OrderBy="" OrderType="" Bands="0" UseBCD="0"><Descrption></Descrption><Sql>declare @SCF01 varchar(2560) set @SCF01 = :SCF01 select 0</Sql><Links/></Ie><Ie Name="客户信息" Fixed="0" PivotGrid="0" Visible="1" OrderBy="" OrderType="0" Bands="0" UseBCD="0"><Descrption></Descrption><Sql>declare @SCF01 varchar(2560) set @SCF01 = :SCF01 select b.scf01, a.sca01,a.sca06 ,b.scf04,g.abw02, a.sca12 , j.aau02, e.peb03 ,a.sca22,b.scf04,b.scf10,isnull(b.scf16,ped14) scf16,a.sca25,a.sca12 ,GETDATE() Ptime from SCA1 A left join SCF1 B on A.SCA01 = B.SCA01 LEFT JOIN PED1 C ON B.SCF01 = C.SCF01 left join PEC1 D ON C.PEC01 = D.PEC01 LEFT JOIN PEB1 E ON C.PEB01 = E.PEB01 LEFT JOIN SCAE F ON A.SCA01 = F.SCA01 left join ABW1 G on A.ABW01 = G.ABW01 left join PEG1 H ON B.SCF01 = H.SCF01 LEFT JOIN ACK1 I ON I.ACK01 = B.ACK01 LEFT JOIN AAU1 J ON J.AAU01 = B.AAU01 WHERE B.SCF01 in (select * from SPLIT(@SCF01,',')) order by B.SCF01</Sql><Links/><DTS><Script>declare @SCF01 varchar(2560) set @SCF01 = :SCF01</Script><Ie Name="体检结果" Fixed="0" PivotGrid="0" Visible="1" OrderBy="" OrderType="0" Bands="0" UseBCD="0" MField="SCF01" SField="SCF01"><Descrption></Descrption><Sql>declare @SCF01 varchar(2560) set @SCF01 = :SCF01 --select * from PEN1 a --left join bbx1 b on a.bbx01b = b.bbx01 --join PEJ1 C ON C.PEJ01= A.PEJ01 --left join PEP1 E ON A.PEJ01 = E.PEJ01 --WHERE a.SCF01 in (select * from SPLIT(@SCF01,',')) and c.DFLAG = 0 --order by a.SCF01 sele
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值