最近在做数据统计,数据库是mysql ,统计用到 having 函数, 但是 mybatis 中没有having 标签,
刚开始时使用的是 if 标签,但是 if 标签使用太复杂,还得判断 if 标签内的SQL 是否为空等等,
最后想到了使用 trim 标签来模拟 having 的使用。

<sql id="Base_Where">

	<where>  
		<if test='id != null and id != "" '> and t.id = #{ id } </if> 		
	</where>
	
	<!--  使用 trim 标签模拟 having 函数的使用   -->
	<trim prefix="having"  prefixOverrides="and" >
	
		<if test='haveSuccess != null and haveSuccess == "1" '> 
			and yd_succcess_count > 0 
		</if>
		<if test='haveSuccess != null and haveSuccess == "2" '> 
			and lt_succcess_count > 0 
		</if>
		<if test=' repeatTimes1 !=null and repeatTimes1 !="" '> 
			and repeat_times <![CDATA[ >=]]> #{ repeatTimes1 }
		</if>
		<if test=' repeatTimes2 !=null and repeatTimes2 !="" '> 
			and repeat_times <![CDATA[ <=]]> #{ repeatTimes2 }
		</if>
	</trim>
	
</sql>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐