• Mybatis配置文件中Insert 元素标签添加配置有哪些呢?


    Mybatis配置文件中Insert 元素标签添加配置有哪些呢?

    如需要跟多资料请点击下方图片⬇(扫码加好友→备注66,不备注拒绝添加哦)
    image

    Insert 元素标签添加方式

    • 添加记录不返回主键配置
    • 添加记录返回主键1(属性配置)
    • 添加记录返回主键2(属性配置)
    • 批量添加记录返回影响总记录行数(属性配置)

    案例实操

    添加记录不返回主键配置

    <insert id="addUserNoKey" parameterType="User">        
        insert into  user(userName,userPwd) values(#{userName},#{userPwd})
    </insert>
    

    添加记录返回主键1(属性配置)

    <insert id="addUserHasKey1"  parameterType="user">    
        <selectKey keyProperty="id" order="AFTER" resultType="int">     
            select LAST_INSERT_ID() as id       
        </selectKey>    
        insert into user(userName,userPwd) values(#{userName},#{userPwd})
    </insert>
    

    Oracle 情况

    <selectKey resultType="int" order="BEFORE" keyProperty="id">  
            SELECT LOGS_SEQ.nextval AS ID FROM DUAL  
    </selectKey>  
    

    添加记录返回主键2(属性配置)

    <insert id="addUserHasKey2" parameterType="user" useGeneratedKeys="true"keyProperty="id">    
        insert into user(userName,userPwd) values(#{userName},#{userPwd})
    </insert>
    

    批量添加记录返回影响总记录行数(属性配置)

    <insert id="addUserBatch" parameterType="list" >    
        insert into user(user_name,user_pwd) values    
        <foreach collection="list" item="item" separator=",">     
            (#{item.userName},#{item.userPwd})   
        </foreach> 
    </insert>
    

    扩展

    Update 元素标签使用

    更新单条记录返回影响行数

    <!-- 更新单条 -->
    <update id="updateUser" parameterType="User">
        update user set userName=#{userName} ,userPwd=#{userPwd} where id=#{id}
    </update>
    

    批量更新多条记录 属性配置

    <update id="updateUserBatch" parameterType="java.util.Map">   
        update user set userPwd=#{pwd} where id in      
        <foreach collection="ids" item="item" open="(" separator=","  close=")">   
            #{item}        
        </foreach>
    </update>
    

    Delete 元素标签使用

    删除单条记录

    <delete id="delUserById" parameterType="int">     
        delete from user where  id=#{id}
    </delete>
    

    批量删除多条记录(属性配置)

    <delete id="delUserBatch" parameterType="java.util.Map">     
        delete from user where   id in       
        <foreach collection="ids" item="item" open="(" separator="," close=")"index="index">
            #{item}   
        </foreach>
    </delete>
    

    或下列形式,入参为数组类型

    <delete id="deleteUserBatch2" >
        delete from user where id in 
        <foreach collection="array" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
     </delete>
    

    h collection="array" item="item" open="(" separator="," close=")">
    #{item}

    
    ###
  • 相关阅读:
    Oracle 学习笔记 12 -- 序列、索引、同义词
    解决:设置中打开蓝牙,測试机不会自己主动搜索设备
    mysql 数据库查询最后两条数据
    jQuery中this与$(this)的差别
    Apache http server linux 安装过程说明
    Swift学习——Swift基础具体解释(一)
    CloudFoundry V2 单机版离线安装(伪离线安装)
    CF1119F Niyaz and Small Degrees
    2019-8-31-C#-如何引用-WshShell-类
    2019-8-31-C#-如何引用-WshShell-类
  • 原文地址:https://www.cnblogs.com/lezijie/p/13719932.html
Copyright © 2020-2023  润新知