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

来源:互联网 发布:遗传算法与蚁群算法 编辑:程序博客网 时间:2024/06/02 01:58

在使用框架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的方法updateevent<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>whereuser_Id=#{user_id}</update>

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

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



原创粉丝点击