• SQL语句平时不注意的那些小知识点总结


    一.Mybatis 动态sql 之<where>标签和<trim>标签

      首先两个标签都可以实现这样的功能:做一个查询接口,有两个参数,当输入参数无论是一个还是两个或者不输入的时候,sql都不会报错,都会返回相应的结果,实现动态sql的目的。

          方法一:<where>标签

    <select id="selectByModuleUuidAndAppUuid" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from dev_app_res
    <where>
    <if test="appUuid != null" >
    and app_uuid = #{appUuid,jdbcType=VARCHAR}
    </if>
    <if test="modUuid != null" >
    and mod_uuid = #{modUuid,jdbcType=VARCHAR}
    </if>
    </where>
    </select>

    where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句(不输入条件时,查询全部),而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

      方法二:<trim>标签

    <select id="selectByCondition" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from auth_app_res
    <trim prefix="where" prefixOverrides="and">
    <if test="modUuid!='' and modUuid!=null">
    and mod_uuid=#{modUuid}
    </if>
    <if test="appUuid!='' and appUuid!=null">
    and app_uuid=#{appUuid}
    </if>
    <if test="appUuid='' and appUuid=null and modUuid='' and modUuid=null">

    </if>
    </trim>
    </select>

      本人喜欢<where>标签。

    二.数据库的左连接和右连接的写法

    左连接select * from table1 as a left join table2 as b on a.id = b.id
    右连接select * from table1 as a right join table2 as b on a.id = b.id
    as去掉也没有问题

    三.mybatis insert操作 返回主键的方法


    在使用Mybatis做持久层的时候,insert默认返回的是插入记录的条数,如果根据业务需求需要返回记录的主键时,可以这样编写sql语句。

    单条插入:

    <insert id="add" parameterType="vo.Category">

    <selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id">

    SELECT LAST_INSERT_ID() AS id

    </selectKey>

    insert into category (name_zh, parent_id,

    show_order, delete_status, description

    )

    values (#{nameZh,jdbcType=VARCHAR},

    #{parentId,jdbcType=SMALLINT},

    #{showOrder,jdbcType=SMALLINT},

    #{deleteStatus,jdbcType=BIT},

    #{description,jdbcType=VARCHAR}

    )

    </insert>

    批量插入:

    <insert id="batchInsert" parameterType="java.lang.List">

      <selectkey keyproperty="uuid" order="AFTER" resultType="int">

      select Last_Insert_ID()

      </selectkey>

      insert into auth_app_func(uuid,appUuid)values

      <foreach collection="list" item="item" index="index" separator=",">

      (#{item.uuid},#{item.appUuid})

      </foreach>

    </insert>

    
    

     四.mybatis 批量修改

    <update id="batchUpdate" parameterType="cn.lz.authAppFuncFilter">

    update auth_app_func set group_uuid=#{groupUuid}

    where uuid in

    <foreach collection="listUuid" index="index" item="item" separator="," open="(" close=")">

    #{item}

    </foreach>

    </update>

     
  • 相关阅读:
    转:二进制文件和文本文件的区别详解
    python sorted函数,字典排序
    office word交叉引用,文献尾注,公式自动编号
    list作为实参传给函数
    转:python深拷贝和浅拷贝的区别
    文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)
    python遍历文件夹中所有文件夹和文件,os.walk
    cf div3 B Obtaining the String
    cf div3 B Obtaining the String
    poj 3281(最大流)
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/6869014.html
Copyright © 2020-2023  润新知