zoukankan      html  css  js  c++  java
  • mybatis主键返回的实现

    数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键。主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A、B中插入数据(A的主键是B的外键),向A表中插入数据之后,向B表中插入数据时需要用到A的主键。

      比如添加一个用户,同时返回插入用户后得到的用户id:

    1. /** * 添加用户信息 * @param user * @throws Exception */ public intinsertUser(User user) throws Exception { SqlSession session=sqlSessionFactory.openSession(); session.insert("com.danny.mybatis.insertUser", user); session.commit(); return user.getUserId();//返回插入数据库后得到的用户id }

      这里总结一下mybatis插入数据时返回主键的4种情况:MySQL环境下主键自增、mysql环境下主键为uuid、mysql环境下主键自增、mysql环境下主键为uuid。

      以下全文均以User实体来举例说明,字段有userId、userName、sex、birthday、address 五个属性,其中userId有可能是int类型,也有可能是String类型。


    数据库为mysql


    主键为自增时(主键为数值类型且自增)

      利用mysql的LAST_INSERT_ID()方法获取插入记录的主键,select LAST_INSERT_ID()可以在插入数据后,查询并返回刚插入数据的主键(但是单独执行这条语句只会返回0)。

    <insert id="insertUser" parameterType="com.danny.mybatis.po.User">
            <selectKey keyProperty="userId" order="AFTER" resultType="java.lang.Integer">
                select LAST_INSERT_ID()
            </selectKey>
            insert into T_USER(userName,birthday,sex,address) values (#{userName},#{birthday},#{sex},#{address})
        </insert>

      parameterType:指定insert执行语句接收的参数类型为pojo(这里的user)。

      keyProperty:将查询到的主键值设置到parameterType指定对象的哪个属性。

      order:<selectKey> 标签内的sql语句相对于insert语句的执行顺序,AFTER表示select LAST_INSERT_ID() 这个语句将在insert语句之后执行。


    主键为UUID时(主键必须为字符类型)

      使用mysql的方法UUID()方法获取随机的UUID作为主键,select UUID()可以在插入数据前,生成随机的UUID并通过keyProperty赋值给将要插入记录的主键。

    <insert id="insertPerson" parameterType="com.danny.mybatis.po.User">
            <selectKey keyProperty="userId" order="BEFORE" resultType="java.lang.String">
                select UUID()
            </selectKey>
            insert into user(userId,userName,birthday,sex,address) values (#{id},#{userName},#{birthday},#{sex},#{address})
        </insert>

      在上述代码中可以看到order 的属性值为BEFORE ,说明在插入之前就已经生成了UUID,并且已经把UUID赋值给user的id。


    背 景数据库为oracle:

    主键为自增时(主键为数值类型):

      在oracle中实现主键自增,需要先创建序列,相当于创建一个全局变量,用来存储对应表的主键的当前最大值(主键为数值类型时)。

      先为user表创建一个序列:

    CREATE SEQUENCE USER_ID_SEQ

    INCREMENT BY 1 -- 每次递增1

    START WITH 1 -- 从1开始

    MINVALUE 1 -- 最小值=1

    NOCYCLE; -- 不循环

      利用USER_ID_SEQ.NEXTVAL 获得要插入数据的主键:

    <insert id="insertUser" parameterType="com.danny.mybatis.po.User">
            <selectKey keyProperty="userId" order="BEFORE" resultType="java.lang.Integer">
                select USER_ID_SEQ.NEXTVAL as userId from DUAL
            </selectKey>
            insert into T_USER(userId,userName,birthday,sex,address) values (#{userId},#{userName},#{birthday},#{sex},#{address})
        </insert>


    主键为UUID时

      用oracle自带的SYS_GUID()方法获得随机的GUID作为主键:

    <insert id="insertUser" parameterType="com.danny.mybatis.po.User">
            <selectKey keyProperty="userId" order="BEFORE" resultType="java.lang.Integer">
                select SYS_GUID() as userId from DUAL
            </selectKey>
            insert into T_USER(userId,userName,birthday,sex,address) values (#{userId},#{userName},#{birthday},#{sex},#{address})
        </insert>
  • 相关阅读:
    CAGradientLayer
    AndroidStudio -- AndroidStuido中找不到cache.properties文件
    logcat -- 基本用法
    UiAutomator -- UiObject2 API
    Android UiAutomator UiDevice API
    Ubuntu 连接手机 不识别设备 -- 解决办法
    Ubuntu Cannot run program "../SDK/build-tools/xxx/aapt": erro = 2 No such file or directory
    Junit4
    Android Studio下运行UiAutomator
    Gradle sync failed: failed to find Build Tools revision 21.1.2
  • 原文地址:https://www.cnblogs.com/fengli9998/p/6723059.html
Copyright © 2011-2022 走看看