spring整合mybitas中的sql标签使用案例

在使用框架ssm的时候往往dao的实现是mapper.xml,而使用mapper实现sql的时候会用到各种标签,类似<where><trim><choose><foreach><set>之类的,往往刚开始使用spring的时候不太习惯,很多时候传的值是null,会导致整条sql语句无法使用,实际使用的案例很多,我结合自己的一些使用情况来讲一下

select a.class,a.age from student a where a.id=1 and a.teacherid=2 and a.name=hello

普通的sql语句拿到mapper里面的使用

select a.class,a.age from student a 
<where> 
<if test="id!=null">
a.id=#{id}
</if>
<if test="teacherid!=null">
and a.teacherid=#{teacherid} 
</if>
<if test="name!=null">
and a.name=#{name}
</if>
</where>

使用了where标签后如果其中一个值为null也不会因为多了一个and而影响到使用,因为标签会剔除多余的and

同样set也是这样使用的,只是set标签是在update时候使用

<!--事件审核event表一一 -->
	<update id="ModifyEvent" parameterType="map">//id是dao的方法
		update
		event
		<set>
		<if test="event_examine!=null">
		event_examine=#{event_examine}
		</if>
		<if test="event_title!=null">
		and event_title=#{event_title}
		</if>
		<if test="event_type!=null">
		and event_type=#{event_type}
		</if>
		</set>
		where
		user_Id=#{user_id}
	</update>

我只是列举了比较常用的两个标签,其他如choose等标签功能也是涵盖比较全的,可能使用关了hibernate的朋友去使用mapper会不习惯,我刚开始也是

用多了就可以了,如果有哪些地方要补充或者不对的,欢迎大家来指正啊



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值