zoukankan      html  css  js  c++  java
  • mybatis中useGeneratedKeys和keyProperty的作用

    在使用mybatis时,常常会出现这种需求:

    当主键是自增的情况下,添加一条记录的同时,其主键是不能使用的,但是有时我们需要该主键,这时我们该如何处理呢?这时我们只需要在其对应xml中加入以下属性即可:

    useGeneratedKeys="true"  keyProperty="对应的主键的对象"。

    如下例:

    <!--   主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名-->
        <insert id="saveMsg" parameterType="cn.com.hyddl.smarthome.notice.core.nano.Notice"
            useGeneratedKeys="true" keyProperty="msgId">
            insert into notice(msg_type,title,content,rec_time,send_time,user_id,deleted,viewed)
            values(#{msgType,jdbcType=INTEGER},#{title,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},
                   #{recTime,jdbcType=BIGINT},#{sendTime,jdbcType=BIGINT},#{userId,jdbcType=VARCHAR},
                   #{deleted,jdbcType=TINYINT},#{viewed,jdbcType=INTEGER})
        </insert>
    

      这样在之后的java代码中我们就可以获取该主键对应的对象的属性值(msgId)。

    每日积累一点,都是进步的见证。
  • 相关阅读:
    hdu4472-Count
    Codeforces Beta Round #55 (Div. 2)
    优化素数表
    Codeforces Beta Round #49 (Div. 2)-D. Physical Education
    Codeforces Beta Round #49 (Div. 2)-C. Little Frog
    一些不知道的函数
    kmp算法详解
    STL之accumulate用法小结
    STL——list学习笔记
    maven打war包包含源文件-eclipse
  • 原文地址:https://www.cnblogs.com/smart-hwt/p/8136188.html
Copyright © 2011-2022 走看看