Mybatis中提取<where><if>公共查询条件

本文介绍了在Mybatis中如何提取并封装SQL查询中的公共条件,以减少重复代码,提高代码可维护性。通过实例展示了如何在<where><if>标签中引用共用条件,实现多个SQL语句的复用。
摘要由CSDN通过智能技术生成

我们在使用mybatis写sql语句的时候,经常会遇到很多sql都会有拥有某些相同的查询条件,如果只是一个两个还感觉不到什么,可是如果查询语句特别多,而且又都拥有共同的查询条件的时候,我们可以考虑将中相同的条件抽取出来,然后封装一下,在需要的时候直接引用就可以了。
以我最近接手的代码为例:
1.未抽取前

<select id="selectVtRecordPage" parameterType="com.webest.guestRegistration.entity.ReqInfo"
			resultType="com.webest.guestRegistration.entity.ReqInfo">
		SELECT
		vi.vtIdentifyNO vtIdentifyNO,vr.vtIdentifyNO vtNO,
		vtID,vtName,vtSex,vtAddress,vtNativePlace,vtBirthday,vtIdentifyImgURL,
		vtScanImgURL,vtTelephone,vtRank,vtOfficerPost,vtNation,expirationStart,expirationEnd,departmentTelephone,
		visitID,vtEventID,vtIdentifyType,vtProperty,vtPeriod,vtSummary,vtDetail,enterpriseID,enterpriseName,departmentID,
		departmentName,personID,personName,vtImgURL,examineNO,examineTime,tmpCardNO,tmpInTime,tmpOutTime,
		vtStatus,isAttend,deviceID,vtRegisterTime,regionID,regionName,isAbnormal,handlingSuggestion,userID,isRecognition
		FROM vis_visitor_info vi RIGHT JOIN vis_visit_record vr
		ON vi.vtIdentifyNO = vr.vtIdentifyNO
		<where>
			<if test="vtName != null and vtName.length()>0">
				AND vi.vtName like concat('',#{vtName},'%')
			</if>
			<if test="vtIdentifyNO != null and vtIdentifyNO.length()>0">
				AND vi.vtIdentifyNO = #{vtIdentifyNO}
			</if>
			<if test="vtIdentifyType != null and vtIdentifyType.length()>0">
				AND vi.vtIdentifyType = #{vtIdentifyType}
			</if>
			<if test="isRecognition != null and isRecognition.length()>0">
				AND vr.isRecognition = #{isRecognition}
			</if>
			<if test="vtPeriod != null and vtPeriod.leng
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值