Mybatis动态SQL

可以根据传人的SQL参数不同,生成不同的SQL语句
MyBatis提供了一套标签,用于在XML中动态拼凑SQL语句

select * from emp
<if test="xxx">...</if>

<chose>
    <when test="xxx">...</when>
    <when test="xxx">...</when>
    <otherwise>...</otherwise>
</choose>

<foreach></foreach>
<where>
<set>

组合查询功能
a.笔记:标题,状态,开始日期,结束日期
根据上述条件,用户可以随意输入信息,按信息搜索

<!-- 组合查询 -->
    <select id="hightSearch" parameterType="map" resultType="org.tarena.note.entity.Note">
        select 
            cn_note_id,
            cn_note_title,
            cn_note_create_time 
        from cn_note
        <where>
            <if test="title!=null">
                cn_note_title like #{title}
            </if>
            <if test="status!=null">
                and cn_note_status_id = #{status}
            </if>
            <if test="beginDate!=null">
                and cn_note_create_time >= #{beginDate}
            </if>
            <if test="endDate!=null">
                and cn_note_create_time &lt;= #{endDate}
            </if>
        </where>
    </select>

b.懂爱更新SQL
笔记标题,创建时间,所属笔记本。ajax将这三值传入
如果3值不为空,则update传入

<!-- 动态更新,将一些不为null的属性更新到数据库 -->
    <update id="dynamicUpdate" parameterType="org.tarena.note.entity.Note">
        update cn_note
        <set>
            <if test="cn_notebook_id != null">
                cn_notebook_id = #{cn_notebook_id},
            </if>
            <if test="cn_user_id != null">
                cn_user_id = #{cn_user_id},
            </if>
            <if test="cn_note_status_id != null">
                cn_note_status_id = #{cn_note_status_id},
            </if>
            <if test="cn_note_type_id != null">
                cn_note_type_id = #{cn_note_type_id},
            </if>
            <if test="cn_note_title != null">
                cn_note_title = #{cn_note_title},
            </if>
            <if test="cn_note_body != null">
                cn_note_body = #{cn_note_body},
            </if>
            <if test="cn_note_create_time != null">
                cn_note_create_time = #{cn_note_create_time},
            </if>
            <if test="cn_note_last_modify_time != null">
                cn_note_last_modify_time = #{cn_note_last_modify_time}
            </if>
        </set>
        where
            cn_note_id = #{cn_note_id}
    </update>

c.批量删除
delete from cn_note where cn_note_id = ?

<delete id="deleteNotes">
        delete from cn_note
        where cn_note_id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值